Commit 3126ec25 by yemin

no message

parent a440b760
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions">
<TaskOptions isEnabled="true">
<TaskOptions isEnabled="false">
<option name="arguments" value="fmt $FilePath$" />
<option name="checkSyntaxErrors" value="true" />
<option name="description" />
......
module git.168cad.top/go/wechat-client-sdk
go 1.17
go 1.18
require github.com/wechatpay-apiv3/wechatpay-go v0.2.16 // indirect
package subscribe
import (
"encoding/json"
"errors"
"fmt"
"git.168cad.top/go/wechat-client-sdk/core/response"
"git.168cad.top/go/wechat-client-sdk/httpclient"
"git.168cad.top/go/wechat-client-sdk/util"
)
type SendSubscribeMsgParam struct {
AccessToken string //接口调用凭证。使用access_token或者authorizer_access_token
TemplateId string //所需下发的订阅模板id
Page string //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转
Touser string //接收者(用户)的 openid
Data []*SendSubscribeMsgDataParam //模板内容
MiniprogramState string //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
Lang string //进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
}
type SendSubscribeMsgDataParam struct {
Name string //
Value string //
}
type subscribeMsgParam struct {
TemplateId string `json:"template_id"` //所需下发的订阅模板id
Page string `json:"page"` //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转
Touser string `json:"touser"` //接收者(用户)的 openid
Data map[string]map[string]string `json:"data"` //模板内容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }的object
MiniprogramState string `json:"miniprogram_state"` //跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
Lang string `json:"lang"` //进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
}
// SendMeg 发送订阅消息
func SendMeg(p *SendSubscribeMsgParam) error {
url := "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + p.AccessToken
d := make(map[string]map[string]string, len(p.Page))
for _, datum := range p.Data {
v := make(map[string]string, 1)
v["value"] = datum.Value
d[datum.Name] = v
}
bp := subscribeMsgParam{
TemplateId: p.TemplateId,
Page: p.Page,
Touser: p.Touser,
Data: d,
MiniprogramState: util.IF(p.MiniprogramState == "", "formal", p.MiniprogramState),
Lang: util.IF(p.Lang == "", "zh_CN", p.Lang),
}
sss, _ := json.Marshal(bp)
fmt.Println("发送请求内容:" + string(sss))
bs, err := httpclient.Post(url, nil, bp)
if err != nil {
return err
}
r := response.MiniProgramResponse{}
err = json.Unmarshal(bs, &r)
if err != nil {
return err
}
if r.ErrCode != 0 {
return errors.New(r.ErrMSG)
}
return nil
}
package util
// IF 三元运算
func IF[T any](t bool, a T, b T) T {
if t {
return a
} else {
return b
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment