|
|
@@ -0,0 +1,125 @@
|
|
|
+package message
|
|
|
+
|
|
|
+import (
|
|
|
+ "dashoo.cn/opms_libary/plugin/wechat/base"
|
|
|
+ "dashoo.cn/opms_libary/plugin/wechat/context"
|
|
|
+ "encoding/json"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ customerSendMessage = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
|
|
|
+)
|
|
|
+
|
|
|
+// MiniMessage 消息管理者,可以发送消息
|
|
|
+type MiniMessage struct {
|
|
|
+ base.WechatBase
|
|
|
+}
|
|
|
+
|
|
|
+// NewMiniMessage 实例化消息管理者
|
|
|
+func NewMiniMessage(context *context.Context) *MiniMessage {
|
|
|
+ msg := new(MiniMessage)
|
|
|
+ msg.Context = context
|
|
|
+ return msg
|
|
|
+}
|
|
|
+
|
|
|
+// MediaText 文本消息的文字
|
|
|
+type MediaText struct {
|
|
|
+ Content string `json:"content"`
|
|
|
+}
|
|
|
+
|
|
|
+// MediaResource 消息使用的临时素材id
|
|
|
+type MediaResource struct {
|
|
|
+ MediaID string `json:"media_id"`
|
|
|
+}
|
|
|
+
|
|
|
+// MediaMiniprogrampage 小程序卡片
|
|
|
+type MediaMiniprogrampage struct {
|
|
|
+ Title string `json:"title"`
|
|
|
+ Appid string `json:"appid"`
|
|
|
+ Pagepath string `json:"pagepath"`
|
|
|
+ ThumbMediaID string `json:"thumb_media_id"`
|
|
|
+}
|
|
|
+
|
|
|
+// MediaLink 发送图文链接
|
|
|
+type MediaLink struct {
|
|
|
+ Title string `json:"title"`
|
|
|
+ Description string `json:"description"`
|
|
|
+ URL string `json:"url"`
|
|
|
+ ThumbURL string `json:"thumb_url"`
|
|
|
+}
|
|
|
+
|
|
|
+// CustomerMessage 客服消息
|
|
|
+type CustomerMessage struct {
|
|
|
+ ToUser string `json:"touser"` // 接受者OpenID
|
|
|
+ Msgtype MsgType `json:"msgtype"` // 客服消息类型
|
|
|
+ Text *MediaText `json:"text,omitempty"` // 可选
|
|
|
+ Image *MediaResource `json:"image,omitempty"` // 可选
|
|
|
+ Link *MediaLink `json:"link,omitempty"` // 可选
|
|
|
+ Miniprogrampage *MediaMiniprogrampage `json:"miniprogrampage,omitempty"` // 可选
|
|
|
+}
|
|
|
+
|
|
|
+// NewTextMessage 文本消息结构体构造方法
|
|
|
+func NewTextMessage(toUser, text string) *CustomerMessage {
|
|
|
+ return &CustomerMessage{
|
|
|
+ ToUser: toUser,
|
|
|
+ Msgtype: MsgTypeText,
|
|
|
+ Text: &MediaText{
|
|
|
+ Content: text,
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// NewImgMessage 图片消息的构造方法
|
|
|
+func NewImgMessage(toUser, mediaID string) *CustomerMessage {
|
|
|
+ return &CustomerMessage{
|
|
|
+ ToUser: toUser,
|
|
|
+ Msgtype: MsgTypeImage,
|
|
|
+ Image: &MediaResource{
|
|
|
+ MediaID: mediaID,
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// NewLinkMessage 图文链接消息的构造方法
|
|
|
+func NewLinkMessage(toUser, title, description, url, thumbURL string) *CustomerMessage {
|
|
|
+ return &CustomerMessage{
|
|
|
+ ToUser: toUser,
|
|
|
+ Msgtype: MsgTypeLink,
|
|
|
+ Link: &MediaLink{
|
|
|
+ Title: title,
|
|
|
+ Description: description,
|
|
|
+ URL: url,
|
|
|
+ ThumbURL: thumbURL,
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// NewMiniprogrampageMessage 小程序卡片消息的构造方法
|
|
|
+func NewMiniprogrampageMessage(toUser, title, pagepath, thumbMediaID string) *CustomerMessage {
|
|
|
+ return &CustomerMessage{
|
|
|
+ ToUser: toUser,
|
|
|
+ Msgtype: MsgTypeMiniProgramPage,
|
|
|
+ Miniprogrampage: &MediaMiniprogrampage{
|
|
|
+ Title: title,
|
|
|
+ Pagepath: pagepath,
|
|
|
+ ThumbMediaID: thumbMediaID,
|
|
|
+ },
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Send 发送客服消息
|
|
|
+func (m *MiniMessage) Send(msg *CustomerMessage) (err error) {
|
|
|
+ response, err := m.HTTPPostJSONWithAccessToken(customerSendMessage, msg)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ var result context.WxError
|
|
|
+ err = json.Unmarshal(response, &result)
|
|
|
+ if err != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if result.Code != 0 {
|
|
|
+ return &result
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|