From 942b4a627a5a6b02f728bd4facb514589bdd3c02 Mon Sep 17 00:00:00 2001 From: wenzl Date: Wed, 25 Oct 2017 19:10:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=A8=A1=E6=9D=BF=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=8F=91=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/template.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ user/user.go | 4 +-- wechat.go | 6 ++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 template/template.go diff --git a/template/template.go b/template/template.go new file mode 100644 index 0000000..cbd07bb --- /dev/null +++ b/template/template.go @@ -0,0 +1,74 @@ +package template + +import ( + "encoding/json" + "fmt" + + "github.com/silenceper/wechat/context" + "github.com/silenceper/wechat/util" +) + +const ( + templateSendURL = "https://api.weixin.qq.com/cgi-bin/message/template/send" +) + +//Template 模板消息 +type Template struct { + *context.Context +} + +//NewTemplate 实例化 +func NewTemplate(context *context.Context) *Template { + tpl := new(Template) + tpl.Context = context + return tpl +} + +//Message 发送的模板消息内容 +type Message struct { + ToUser string `json:"touser"` // 必须, 接受者OpenID + TemplateID string `json:"template_id"` // 必须, 模版ID + URL string `json:"url,omitempty"` // 可选, 用户点击后跳转的URL, 该URL必须处于开发者在公众平台网站中设置的域中 + Color string `json:"color,omitempty"` // 可选, 整个消息的颜色, 可以不设置 + Data map[string]*DataItem `json:"data"` // 必须, 模板数据 + + MiniProgram struct { + AppID string `json:"appid"` //所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系) + PagePath string `json:"pagepath"` //所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar) + } `json:"miniprogram"` //可选,跳转至小程序地址 +} + +//DataItem 模版内某个 .DATA 的值 +type DataItem struct { + Value string `json:"value"` + Color string `json:"color,omitempty"` +} + +type resTemplateSend struct { + util.CommonError + + MsgID int32 `json:"msgid"` +} + +//Send 发送模板消息 +func (tpl *Template) Send(msg *Message) (msgID int32, err error) { + var accessToken string + accessToken, err = tpl.GetAccessToken() + if err != nil { + return + } + uri := fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken) + response, err := util.PostJSON(uri, msg) + + var result resTemplateSend + err = json.Unmarshal(response, &result) + if err != nil { + return + } + if result.ErrCode != 0 { + err = fmt.Errorf("template msg send error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg) + return + } + msgID = result.MsgID + return +} diff --git a/user/user.go b/user/user.go index 4b00ed3..73f127c 100644 --- a/user/user.go +++ b/user/user.go @@ -9,7 +9,7 @@ import ( ) const ( - userInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN" + userInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info" ) //User 用户管理 @@ -52,7 +52,7 @@ func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) { return } - uri := fmt.Sprintf(userInfoURL, accessToken, openID) + uri := fmt.Sprintf("%s?access_token=%s&openid=%s&lang=zh_CN", userInfoURL, accessToken, openID) var response []byte response, err = util.HTTPGet(uri) if err != nil { diff --git a/wechat.go b/wechat.go index 9a810f7..e48de19 100644 --- a/wechat.go +++ b/wechat.go @@ -11,6 +11,7 @@ import ( "github.com/silenceper/wechat/menu" "github.com/silenceper/wechat/oauth" "github.com/silenceper/wechat/server" + "github.com/silenceper/wechat/template" "github.com/silenceper/wechat/user" ) @@ -81,3 +82,8 @@ func (wc *Wechat) GetMenu() *menu.Menu { func (wc *Wechat) GetUser() *user.User { return user.NewUser(wc.Context) } + +// GetTemplate 模板消息接口 +func (wc *Wechat) GetTemplate() *template.Template { + return template.NewTemplate(wc.Context) +}