|
| 1 | +package mq |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "github.com/silenceper/wechat/v2/officialaccount/message" |
| 7 | +) |
| 8 | + |
| 9 | +type NoticeTemplate struct { |
| 10 | + ctx context.Context |
| 11 | + StaffID []string `json:"StaffID"` |
| 12 | + WeChat *WeChatNoticeBody `json:"template,omitempty"` |
| 13 | + DingTalk *DingTalkNoticeBody `json:"dingtalk,omitempty"` |
| 14 | +} |
| 15 | + |
| 16 | +func New() *NoticeTemplate { |
| 17 | + return &NoticeTemplate{} |
| 18 | +} |
| 19 | + |
| 20 | +func (t *NoticeTemplate) WithContext(ctx context.Context) *NoticeTemplate { |
| 21 | + t.ctx = ctx |
| 22 | + return t |
| 23 | +} |
| 24 | + |
| 25 | +func (t *NoticeTemplate) Receiver(staffId ...string) *NoticeTemplate { |
| 26 | + t.StaffID = append(t.StaffID, staffId...) |
| 27 | + return t |
| 28 | +} |
| 29 | + |
| 30 | +func (t *NoticeTemplate) NewWeChat() *WeChatNoticeBody { |
| 31 | + t.WeChat = &WeChatNoticeBody{} |
| 32 | + return t.WeChat |
| 33 | +} |
| 34 | + |
| 35 | +func (t *NoticeTemplate) NewDingTalk() *DingTalkNoticeBody { |
| 36 | + t.DingTalk = &DingTalkNoticeBody{} |
| 37 | + return t.DingTalk |
| 38 | +} |
| 39 | + |
| 40 | +type WeChatNoticeBody struct { |
| 41 | + Data map[string]*message.TemplateDataItem `json:"data"` |
| 42 | + TemplateID string `json:"template_id"` |
| 43 | + ToUser string `json:"touser"` |
| 44 | + URL string `json:"url"` |
| 45 | +} |
| 46 | + |
| 47 | +func (t *WeChatNoticeBody) SetTmpl(templateId string) *WeChatNoticeBody { |
| 48 | + t.TemplateID = templateId |
| 49 | + return t |
| 50 | +} |
| 51 | + |
| 52 | +func (t *WeChatNoticeBody) SetData(key, value, color string) *WeChatNoticeBody { |
| 53 | + if t.Data == nil { |
| 54 | + t.Data = make(map[string]*message.TemplateDataItem) |
| 55 | + } |
| 56 | + t.Data[key] = &message.TemplateDataItem{Value: value, Color: color} |
| 57 | + return t |
| 58 | +} |
| 59 | + |
| 60 | +func (t *WeChatNoticeBody) Url(url string) *WeChatNoticeBody { |
| 61 | + t.URL = url |
| 62 | + return t |
| 63 | +} |
| 64 | + |
| 65 | +type DingTalkNoticeBody struct { |
| 66 | + MsgType string `json:"msgtype"` |
| 67 | + Text *DingTalkMsgPlainTextInput `json:"text,omitempty"` |
| 68 | + Link *DingTalkMsgLinkInput `json:"link,omitempty"` |
| 69 | + Markdown *DingTalkMsgMarkdownInput `json:"markdown,omitempty"` |
| 70 | +} |
| 71 | + |
| 72 | +func (t *DingTalkNoticeBody) PlainText(content string) { |
| 73 | + t.MsgType = "text" |
| 74 | + t.Text = &DingTalkMsgPlainTextInput{ |
| 75 | + Content: content, |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func (t *DingTalkNoticeBody) LinkMsg(messageUrl, picUrl, title, text string) { |
| 80 | + t.MsgType = "link" |
| 81 | + t.Link = &DingTalkMsgLinkInput{ |
| 82 | + MessageUrl: messageUrl, |
| 83 | + PicUrl: picUrl, |
| 84 | + Title: title, |
| 85 | + Text: text, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func (t *DingTalkNoticeBody) MarkdownMsg(title, text string) { |
| 90 | + t.MsgType = "markdown" |
| 91 | + t.Markdown = &DingTalkMsgMarkdownInput{ |
| 92 | + Title: title, |
| 93 | + Text: text, |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +type DingTalkMsgPlainTextInput struct { |
| 98 | + Content string `json:"content"` |
| 99 | +} |
| 100 | + |
| 101 | +type DingTalkMsgLinkInput struct { |
| 102 | + MessageUrl string `json:"messageUrl"` |
| 103 | + PicUrl string `json:"picUrl"` |
| 104 | + Title string `json:"title"` |
| 105 | + Text string `json:"text"` |
| 106 | +} |
| 107 | + |
| 108 | +type DingTalkMsgMarkdownInput struct { |
| 109 | + Title string `json:"title"` // 这个是点进推送列表前显示的简短的消息(进入后不可见) |
| 110 | + Text string `json:"text"` // 这个是进入后显示的详细的消息 |
| 111 | +} |
| 112 | + |
| 113 | +type UniNotifyInput struct { |
| 114 | + Title string |
| 115 | + SubTitle string |
| 116 | + Content string |
| 117 | + Extra string |
| 118 | +} |
| 119 | + |
| 120 | +type WechatBody struct { |
| 121 | + StaffId string `json:"staffId"` |
| 122 | + *WeChatNoticeBody |
| 123 | + MessageId string `json:"messageId"` |
| 124 | +} |
| 125 | + |
| 126 | +func MarshalWechatBody(staffId string, msg *WeChatNoticeBody) (data []byte, err error) { |
| 127 | + body := &WechatBody{ |
| 128 | + StaffId: staffId, |
| 129 | + WeChatNoticeBody: msg, |
| 130 | + } |
| 131 | + if data, err = json.Marshal(body); err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + return data, nil |
| 135 | +} |
| 136 | + |
| 137 | +func UnmarshalWechatBody(data []byte) (*WechatBody, error) { |
| 138 | + body := &WechatBody{} |
| 139 | + if err := json.Unmarshal(data, body); err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + return body, nil |
| 143 | +} |
| 144 | + |
| 145 | +type DingTalkBody struct { |
| 146 | + StaffId string `json:"staffId"` |
| 147 | + *DingTalkNoticeBody |
| 148 | + MessageId string `json:"messageId"` |
| 149 | +} |
| 150 | + |
| 151 | +func MarshalDingTalkBody(staffId string, msg *DingTalkNoticeBody) (data []byte, err error) { |
| 152 | + body := &DingTalkBody{ |
| 153 | + StaffId: staffId, |
| 154 | + DingTalkNoticeBody: msg, |
| 155 | + } |
| 156 | + if data, err = json.Marshal(body); err != nil { |
| 157 | + return nil, err |
| 158 | + } |
| 159 | + return data, nil |
| 160 | +} |
| 161 | + |
| 162 | +func UnmarshalDingTalkBody(data []byte) (*DingTalkNoticeBody, error) { |
| 163 | + body := &DingTalkNoticeBody{} |
| 164 | + if err := json.Unmarshal(data, body); err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + return body, nil |
| 168 | +} |
0 commit comments