-
Notifications
You must be signed in to change notification settings - Fork 0
/
appchatupdate.go
132 lines (117 loc) · 3.71 KB
/
appchatupdate.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
/**
* Created by GoLand.
* User: 姜伟
* Date: 2020/1/7 0007
* Time: 16:51
*/
package message
import (
"regexp"
"github.com/a07061625/gompf/mpf"
"github.com/a07061625/gompf/mpf/api"
"github.com/a07061625/gompf/mpf/api/wx"
"github.com/a07061625/gompf/mpf/mpconstant/errorcode"
"github.com/a07061625/gompf/mpf/mpconstant/project"
"github.com/a07061625/gompf/mpf/mperr"
"github.com/valyala/fasthttp"
)
// 修改群聊会话
type appChatUpdate struct {
wx.BaseWxCorp
corpId string
agentTag string
chatId string // 群id
name string // 群聊名
owner string // 群主id
addUserList []string // 添加成员列表
delUserList []string // 踢出成员列表
}
func (acu *appChatUpdate) SetChatId(chatId string) {
match, _ := regexp.MatchString(`^[0-9a-zA-Z]{1,32}$`, chatId)
if match {
acu.chatId = chatId
} else {
panic(mperr.NewWxCorp(errorcode.WxCorpParam, "群id不合法", nil))
}
}
func (acu *appChatUpdate) SetName(name string) {
if len(name) > 0 {
trueName := []rune(name)
acu.name = string(trueName[:25])
} else {
panic(mperr.NewWxCorp(errorcode.WxCorpParam, "群聊名不合法", nil))
}
}
func (acu *appChatUpdate) SetOwner(owner string) {
match, _ := regexp.MatchString(project.RegexDigitLower, owner)
if match {
acu.owner = owner
} else {
panic(mperr.NewWxCorp(errorcode.WxCorpParam, "群主id不合法", nil))
}
}
func (acu *appChatUpdate) SetAddUserList(addUserList []string) {
acu.addUserList = make([]string, 0)
for _, v := range addUserList {
match, _ := regexp.MatchString(project.RegexDigitLower, v)
if match {
acu.addUserList = append(acu.addUserList, v)
}
}
}
func (acu *appChatUpdate) SetDelUserList(delUserList []string) {
acu.delUserList = make([]string, 0)
for _, v := range delUserList {
match, _ := regexp.MatchString(project.RegexDigitLower, v)
if match {
acu.delUserList = append(acu.delUserList, v)
}
}
}
func (acu *appChatUpdate) checkData() {
if len(acu.chatId) == 0 {
panic(mperr.NewWxCorp(errorcode.WxCorpParam, "群聊名不能为空", nil))
}
}
func (acu *appChatUpdate) SendRequest() api.APIResult {
acu.checkData()
reqData := make(map[string]interface{})
reqData["chatid"] = acu.chatId
if len(acu.name) > 0 {
reqData["name"] = acu.name
}
if len(acu.owner) > 0 {
reqData["owner"] = acu.owner
}
if len(acu.addUserList) > 0 {
reqData["add_user_list"] = acu.addUserList
}
if len(acu.delUserList) > 0 {
reqData["del_user_list"] = acu.delUserList
}
reqBody := mpf.JSONMarshal(reqData)
acu.ReqURI = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update?access_token=" + wx.NewUtilWx().GetCorpAccessToken(acu.corpId, acu.agentTag)
client, req := acu.GetRequest()
req.SetBody([]byte(reqBody))
resp, result := acu.SendInner(client, req, errorcode.WxCorpRequestPost)
if resp.RespCode > 0 {
return result
}
respData, _ := mpf.JSONUnmarshalMap(resp.Content)
errCode, ok := respData["errcode"]
if ok && (errCode.(int) == 0) {
result.Data = respData
} else {
result.Code = errorcode.WxCorpRequestPost
result.Msg = respData["errmsg"].(string)
}
return result
}
func NewAppChatUpdate(corpId, agentTag string) *appChatUpdate {
acu := &appChatUpdate{wx.NewBaseWxCorp(), "", "", "", "", "", make([]string, 0), make([]string, 0)}
acu.corpId = corpId
acu.agentTag = agentTag
acu.ReqContentType = project.HTTPContentTypeJSON
acu.ReqMethod = fasthttp.MethodPost
return acu
}