-
Notifications
You must be signed in to change notification settings - Fork 57
/
model.go
268 lines (225 loc) · 5.75 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package dingtalk
import (
"encoding/json"
"fmt"
"strings"
)
type iDingMsg interface {
Marshaler() []byte
}
type atOption interface {
apply(model *atModel)
}
type funcAtOption struct {
f func(model *atModel)
}
func (fdo *funcAtOption) apply(do *atModel) {
fdo.f(do)
}
func newFuncAtOption(f func(model *atModel)) *funcAtOption {
return &funcAtOption{f: f}
}
func WithAtAll() atOption {
return newFuncAtOption(func(o *atModel) {
o.IsAtAll = true
})
}
func WithAtMobiles(mobiles []string) atOption {
return newFuncAtOption(func(o *atModel) {
o.AtMobiles = mobiles
})
}
func WithAtUserIds(userids []string) atOption {
return newFuncAtOption(func(o *atModel) {
o.AtUserIds = userids
})
}
type textMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
Text textModel `json:"text,omitempty"`
At atModel `json:"at,omitempty"`
}
func (t textMsg) Marshaler() []byte {
b, _ := json.Marshal(t)
return b
}
func NewTextMsg(content string, opts ...atOption) *textMsg {
msg := &textMsg{MsgType: TEXT, Text: textModel{Content: content}}
for _, opt := range opts {
opt.apply(&msg.At)
}
return msg
}
type linkMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
Link linkModel `json:"link,omitempty"`
}
func (l linkMsg) Marshaler() []byte {
b, _ := json.Marshal(l)
return b
}
func NewLinkMsg(title, text, picUrl, msgUrl string) *linkMsg {
return &linkMsg{MsgType: LINK, Link: linkModel{
Text: text,
Title: title,
PicUrl: picUrl,
MessageUrl: msgUrl,
}}
}
type markDownMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
Markdown markDownModel `json:"markdown,omitempty"`
At atModel `json:"at,omitempty"`
}
func (m markDownMsg) Marshaler() []byte {
b, _ := json.Marshal(m)
return b
}
func NewDTMDMsg(title string, dtmdMap *dingMap, opts ...atOption) *markDownMsg {
text := ""
for _, v := range dtmdMap.l {
text = text + "\n - " + fmt.Sprintf(dtmdFormat, v, dtmdMap.m[v])
}
return NewMarkDownMsg(title, text, opts...)
}
func NewMarkDownMsg(title string, text interface{}, opts ...atOption) *markDownMsg {
msg := &markDownMsg{MsgType: MARKDOWN, Markdown: markDownModel{Title: title, Text: text.(string)}}
for _, opt := range opts {
opt.apply(&msg.At)
}
// markdown格式需要在文本内写入被at的人
if len(msg.At.AtMobiles) > 0 {
var atStr = "\n -"
for _, mobile := range msg.At.AtMobiles {
// 为@设置默认颜色
atStr = fmt.Sprintf("<font color=#0089ff>%s @%s</font>", atStr, mobile)
}
msg.Markdown.Text = msg.Markdown.Text + atStr
}
if len(msg.At.AtUserIds) > 0 {
var atStr = "\n -"
for _, userid := range msg.At.AtUserIds {
// 为@设置默认颜色
atStr = fmt.Sprintf("<font color=#0089ff>%s @%s</font>", atStr, userid)
}
msg.Markdown.Text = msg.Markdown.Text + atStr
}
return msg
}
type actionCardOption interface {
apply(model *actionCardModel)
}
type funcActionCardOption struct {
f func(model *actionCardModel)
}
func (fdo *funcActionCardOption) apply(do *actionCardModel) {
fdo.f(do)
}
func newFuncActionCardOption(f func(model *actionCardModel)) *funcActionCardOption {
return &funcActionCardOption{f: f}
}
func WithCardBtnVertical() actionCardOption {
return newFuncActionCardOption(func(o *actionCardModel) {
o.BtnOrientation = vertical
})
}
func WithCardSingleTitle(title string) actionCardOption {
return newFuncActionCardOption(func(o *actionCardModel) {
o.SingleTitle = title
})
}
func WithCardSingleURL(url string) actionCardOption {
return newFuncActionCardOption(func(o *actionCardModel) {
o.SingleURL = url
})
}
func WithCardBtns(btns []ActionCardMultiBtnModel) actionCardOption {
return newFuncActionCardOption(func(o *actionCardModel) {
o.Btns = btns
})
}
type actionCardMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
ActionCard actionCardModel `json:"actionCard,omitempty"`
}
func (a actionCardMsg) Marshaler() []byte {
b, _ := json.Marshal(a)
return b
}
func NewActionCardMsg(title, text string, opts ...actionCardOption) *actionCardMsg {
card := &actionCardMsg{MsgType: ACTION_CARD, ActionCard: actionCardModel{
Title: title,
Text: text,
BtnOrientation: horizontal,
}}
for _, opt := range opts {
opt.apply(&card.ActionCard)
}
return card
}
type feedCardMsg struct {
MsgType msgTypeType `json:"msgtype,omitempty"`
FeedCard feedCardModel `json:"feedCard,omitempty"`
}
func (f feedCardMsg) Marshaler() []byte {
b, _ := json.Marshal(f)
return b
}
func NewFeedCardMsg(feedCard []FeedCardLinkModel) *feedCardMsg {
return &feedCardMsg{MsgType: FEED_CARD, FeedCard: feedCardModel{Links: feedCard}}
}
type MarkType string
// 有序map
type dingMap struct {
m map[string]MarkType
l []string
}
func DingMap() *dingMap {
return &dingMap{m: make(map[string]MarkType), l: make([]string, 0, 0)}
}
func (d *dingMap) Set(val string, t MarkType) *dingMap {
d.l = append(d.l, val)
d.m[val] = t
return d
}
func (d *dingMap) Remove(val string) {
if _, ok := d.m[val]; ok {
for i, v := range d.l {
if v == val {
d.l = append(d.l[:i], d.l[i+1:]...)
break
}
}
delete(d.m, val)
}
}
func (d *dingMap) Slice() []string {
resList := make([]string, 0, len(d.l))
for _, val := range d.l {
content := d.formatVal(val, d.m[val])
resList = append(resList, content)
}
return resList
}
func (d *dingMap) formatVal(val string, t MarkType) (res string) {
var ok bool
if res, ok = hMap[t]; ok {
vl := strings.Split(val, formatSpliter)
if len(vl) == 3 {
res = fmt.Sprintf(res, vl[1])
res = vl[0] + res + vl[2]
} else {
res = fmt.Sprintf(res, val)
}
} else {
res = val
}
if !strings.HasPrefix(res, "- ") && !strings.HasPrefix(res, "#") {
res = "- " + res
}
return
}
type responseMsg struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
}