微信相关接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.3 KiB

package wechat
import (
"errors"
"fmt"
)
type wx_template_msg_res struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
msgid int `json:"msgid"`
}
func SendTemplateMessage(access_token string, touser, template_id, url string, data map[string]interface{}, client_msg_id, miniapp_appid, miniapp_pagepath string) (wx_template_msg_res, error) {
var res wx_template_msg_res
if touser == "" {
return res, errors.New("缺少用户openid")
}
if template_id == "" {
return res, errors.New("缺少消息模板")
}
if access_token == "" {
return res, errors.New("缺少access token")
}
api_url := fmt.Sprintf(MP_TEMPLATE_MESSAGE_API, access_token)
msg := map[string]interface{}{
"touser": touser,
"template_id": template_id,
"url": url,
"data": data,
}
if client_msg_id != "" {
msg["client_msg_id"] = client_msg_id
}
if miniapp_appid != "" {
msg["miniprogram"] = map[string]string{
"appid": miniapp_appid,
"pagepath": miniapp_pagepath,
}
}
msg_json, err := json.Marshal(msg)
if err != nil {
return res, err
}
data_byte, err := PostJson(api_url, msg_json)
if err == nil {
err = json.Unmarshal(data_byte, &res)
}
return res, err
}
func SendTemplateMessageApplet(access_token string, touser, template_id string, miniprogram map[string]string, data map[string]interface{}, client_msg_id, miniapp_appid, miniapp_pagepath string) (wx_template_msg_res, error) {
var res wx_template_msg_res
if touser == "" {
return res, errors.New("缺少用户openid")
}
if template_id == "" {
return res, errors.New("缺少消息模板")
}
if access_token == "" {
return res, errors.New("缺少access token")
}
api_url := fmt.Sprintf(MP_TEMPLATE_MESSAGE_API, access_token)
msg := map[string]interface{}{
"touser": touser,
"template_id": template_id,
"data": data,
}
if len(miniprogram) > 0 {
msg["miniprogram"] = miniprogram
}
if client_msg_id != "" {
msg["client_msg_id"] = client_msg_id
}
if miniapp_appid != "" {
msg["miniprogram"] = map[string]string{
"appid": miniapp_appid,
"pagepath": miniapp_pagepath,
}
}
msg_json, err := json.Marshal(msg)
if err != nil {
return res, err
}
data_byte, err := PostJson(api_url, msg_json)
if err == nil {
err = json.Unmarshal(data_byte, &res)
}
return res, err
}