This commit is contained in:
xxj
2021-09-18 15:36:42 +08:00
parent c39c8ac51d
commit f554b1e101
3 changed files with 34 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ const (
_getJsurl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token="
_getToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
_getSubscribe = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="
_getTempMsg = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="
_cacheToken = "wx_access_token"
_cacheTicket = "weixin_card_ticket"
)
@@ -48,6 +49,10 @@ func (_wx *wxTools) GetAccessToken() (accessToken string, err error) {
js, err := simplejson.NewJson(body)
if err == nil {
accessToken, _ = js.Get("access_token").String()
if len(accessToken) == 0 {
mylog.Error(js)
return
}
//保存缓存
cache.Add(_cacheToken, &accessToken, time.Duration(7000)*time.Second)
//------------------end
@@ -150,3 +155,23 @@ func (_wx *wxTools) SendTemplateMsg(msg TempMsg) bool {
json.Unmarshal(resb, &res)
return res.Errcode == 0
}
// SendWebTemplateMsg 发送订阅消息
func (_wx *wxTools) SendWebTemplateMsg(msg TempWebMsg) bool {
accessToken, err := _wx.GetAccessToken()
if err != nil {
mylog.Error(err)
return false
}
bo, _ := json.Marshal(msg)
resb, _ := myhttp.OnPostJSON(_getTempMsg+accessToken, string(bo))
var res ResTempMsg
json.Unmarshal(resb, &res)
b := res.Errcode == 0
if !b {
mylog.Error(res)
}
return b
}

View File

@@ -41,6 +41,14 @@ type TempMsg struct {
Data map[string]map[string]string `json:"data"` //是 模板内容,不填则下发空模板
}
// TempMsg 订阅消息头
type TempWebMsg struct {
Touser string `json:"touser"` // 是 接收者(用户)的 openid
TemplateID string `json:"template_id"` // 是 所需下发的模板消息的id
Page string `json:"url"` // 否 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,示例index?foo=bar。该字段不填则模板无跳转。
Data map[string]map[string]string `json:"data"` //是 模板内容,不填则下发空模板
}
// ResTempMsg 模版消息返回值
type ResTempMsg struct {
Errcode int `json:"errcode"` //

View File

@@ -37,6 +37,7 @@ type WxTools interface {
// --------------------h5------------------------------
GetWebOauth(code string) (*AccessToken, error) // 授权
GetWebUserinfo(openid, accessToken string) (*WxUserinfo, error) // 获取用户信息
SendWebTemplateMsg(msg TempWebMsg) bool // 发送公众号模板消息
// ----------------------------------------------------
}