forked from eatmoreapple/openwechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaller.go
373 lines (335 loc) · 9.89 KB
/
caller.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package openwechat
import (
"bytes"
"errors"
"net/http"
"net/url"
"os"
)
// 调用请求和解析请求
// 上层模块可以直接获取封装后的请求结果
type Caller struct {
Client *Client
path *url.URL
}
// Constructor for Caller
func NewCaller(client *Client) *Caller {
return &Caller{Client: client}
}
// Default Constructor for Caller
func DefaultCaller() *Caller {
return NewCaller(DefaultClient())
}
// 获取登录的uuid
func (c *Caller) GetLoginUUID() (string, error) {
resp, err := c.Client.GetLoginUUID()
if err != nil {
return "", err
}
defer resp.Body.Close()
var buffer bytes.Buffer
if _, err := buffer.ReadFrom(resp.Body); err != nil {
return "", err
}
// 正则匹配uuid字符串
results := uuidRegexp.FindSubmatch(buffer.Bytes())
if len(results) != 2 {
// 如果没有匹配到,可能微信的接口做了修改,或者当前机器的ip被加入了黑名单
return "", errors.New("uuid does not match")
}
return string(results[1]), nil
}
// 检查是否登录成功
func (c *Caller) CheckLogin(uuid string) (*CheckLoginResponse, error) {
resp, err := c.Client.CheckLogin(uuid)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var buffer bytes.Buffer
if _, err := buffer.ReadFrom(resp.Body); err != nil {
return nil, err
}
// 正则匹配检测的code
// 具体code参考global.go
results := statusCodeRegexp.FindSubmatch(buffer.Bytes())
if len(results) != 2 {
return nil, errors.New("error status code match")
}
code := string(results[1])
return &CheckLoginResponse{Code: code, Raw: buffer.Bytes()}, nil
}
// 获取登录信息
func (c *Caller) GetLoginInfo(body []byte) (*LoginInfo, error) {
// 从响应体里面获取需要跳转的url
results := redirectUriRegexp.FindSubmatch(body)
if len(results) != 2 {
return nil, errors.New("redirect url does not match")
}
path, err := url.Parse(string(results[1]))
if err != nil {
return nil, err
}
domain, err := getDomainByHost(path.Host)
if err != nil {
return nil, err
}
c.Client.domain = domain
resp, err := c.Client.GetLoginInfo(path.String())
if err != nil {
uErr, ok := err.(*url.Error)
if ok && (uErr.Err.Error() == missLocationHeader.Error()) {
return nil, loginForbiddenError
}
return nil, err
}
defer resp.Body.Close()
var loginInfo LoginInfo
// xml结构体序列化储存
if err := scanXml(resp, &loginInfo); err != nil {
return nil, err
}
if !loginInfo.Ok() {
return nil, loginInfo
}
return &loginInfo, nil
}
// 获取初始化信息
func (c *Caller) WebInit(request *BaseRequest) (*WebInitResponse, error) {
resp, err := c.Client.WebInit(request)
if err != nil {
return nil, err
}
var webInitResponse WebInitResponse
defer resp.Body.Close()
if err := scanJson(resp, &webInitResponse); err != nil {
return nil, err
}
return &webInitResponse, nil
}
// 通知手机已登录
func (c *Caller) WebWxStatusNotify(request *BaseRequest, response *WebInitResponse, info *LoginInfo) error {
resp, err := c.Client.WebWxStatusNotify(request, response, info)
if err != nil {
return err
}
var item struct{ BaseResponse BaseResponse }
defer resp.Body.Close()
if err := scanJson(resp, &item); err != nil {
return err
}
if !item.BaseResponse.Ok() {
return item.BaseResponse
}
return nil
}
// 异步获取是否有新的消息
func (c *Caller) SyncCheck(info *LoginInfo, response *WebInitResponse) (*SyncCheckResponse, error) {
resp, err := c.Client.SyncCheck(info, response)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var buffer bytes.Buffer
if _, err := buffer.ReadFrom(resp.Body); err != nil {
return nil, err
}
results := syncCheckRegexp.FindSubmatch(buffer.Bytes())
if len(results) != 3 {
return nil, errors.New("parse sync key failed")
}
retCode, selector := string(results[1]), string(results[2])
syncCheckResponse := &SyncCheckResponse{RetCode: retCode, Selector: selector}
return syncCheckResponse, nil
}
// 获取所有的联系人
func (c *Caller) WebWxGetContact(info *LoginInfo) (Members, error) {
resp, err := c.Client.WebWxGetContact(info)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var item WebWxContactResponse
if err := scanJson(resp, &item); err != nil {
return nil, err
}
if !item.BaseResponse.Ok() {
return nil, item.BaseResponse
}
return item.MemberList, nil
}
// 获取联系人的详情
// 注: Members参数的长度不要大于50
func (c *Caller) WebWxBatchGetContact(members Members, request *BaseRequest) (Members, error) {
resp, err := c.Client.WebWxBatchGetContact(members, request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var item WebWxBatchContactResponse
if err := scanJson(resp, &item); err != nil {
return nil, err
}
if !item.BaseResponse.Ok() {
return nil, item.BaseResponse
}
return item.ContactList, nil
}
// 获取新的消息接口
func (c *Caller) WebWxSync(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*WebWxSyncResponse, error) {
resp, err := c.Client.WebWxSync(request, response, info)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var webWxSyncResponse WebWxSyncResponse
if err := scanJson(resp, &webWxSyncResponse); err != nil {
return nil, err
}
return &webWxSyncResponse, nil
}
// 发送消息接口
func (c *Caller) WebWxSendMsg(msg *SendMessage, info *LoginInfo, request *BaseRequest) (*SentMessage, error) {
resp, err := c.Client.WebWxSendMsg(msg, info, request)
return getSuccessSentMessage(msg, resp, err)
}
// 修改用户备注接口
func (c *Caller) WebWxOplog(request *BaseRequest, remarkName, toUserName string) error {
resp, err := c.Client.WebWxOplog(request, remarkName, toUserName)
if err != nil {
return err
}
return parseBaseResponseError(resp)
}
func (c *Caller) UploadMedia(file *os.File, request *BaseRequest, info *LoginInfo, fromUserName, toUserName string) (*UploadResponse, error) {
// 首先尝试上传图片
resp, err := c.Client.WebWxUploadMediaByChunk(file, request, info, fromUserName, toUserName)
// 无错误上传成功之后获取请求结果,判断结果是否正常
if err != nil {
return nil, err
}
defer resp.Body.Close()
var item UploadResponse
if err := scanJson(resp, &item); err != nil {
return &item, err
}
if !item.BaseResponse.Ok() {
return &item, item.BaseResponse
}
if len(item.MediaId) == 0 {
return &item, errors.New("upload failed")
}
return &item, nil
}
// 发送图片消息接口
func (c *Caller) WebWxSendImageMsg(file *os.File, request *BaseRequest, info *LoginInfo, fromUserName, toUserName string) (*SentMessage, error) {
// 首先尝试上传图片
resp, err := c.UploadMedia(file, request, info, fromUserName, toUserName)
if err != nil {
return nil, err
}
// 构造新的图片类型的信息
msg := NewMediaSendMessage(ImageMessage, fromUserName, toUserName, resp.MediaId)
// 发送图片信息
resp1, err := c.Client.WebWxSendMsgImg(msg, request, info)
return getSuccessSentMessage(msg, resp1, err)
}
func (c *Caller) WebWxSendFile(file *os.File, req *BaseRequest, info *LoginInfo, fromUserName, toUserName string) (*SentMessage, error) {
resp, err := c.UploadMedia(file, req, info, fromUserName, toUserName)
if err != nil {
return nil, err
}
// 构造新的文件类型的信息
stat, _ := file.Stat()
appMsg := NewFileAppMessage(stat, resp.MediaId)
content, err := appMsg.XmlByte()
if err != nil {
return nil, err
}
msg := NewSendMessage(AppMessage, string(content), fromUserName, toUserName, "")
return c.WebWxSendAppMsg(msg, req)
}
// 发送媒体消息
func (c *Caller) WebWxSendAppMsg(msg *SendMessage, req *BaseRequest) (*SentMessage, error) {
resp, err := c.Client.WebWxSendAppMsg(msg, req)
return getSuccessSentMessage(msg, resp, err)
}
// 用户退出
func (c *Caller) Logout(info *LoginInfo) error {
resp, err := c.Client.Logout(info)
if err != nil {
return err
}
return parseBaseResponseError(resp)
}
// 拉好友入群
func (c *Caller) AddFriendIntoChatRoom(req *BaseRequest, info *LoginInfo, group *Group, friends ...*Friend) error {
if len(friends) == 0 {
return errors.New("no friends found")
}
resp, err := c.Client.AddMemberIntoChatRoom(req, info, group, friends...)
if err != nil {
return err
}
return parseBaseResponseError(resp)
}
// 从群聊中移除用户
func (c *Caller) RemoveFriendFromChatRoom(req *BaseRequest, info *LoginInfo, group *Group, users ...*User) error {
if len(users) == 0 {
return errors.New("no users found")
}
resp, err := c.Client.RemoveMemberFromChatRoom(req, info, group, users...)
if err != nil {
return err
}
return parseBaseResponseError(resp)
}
// 同意加好友请求
func (c *Caller) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyContent string) error {
resp, err := c.Client.WebWxVerifyUser(storage, info, verifyContent)
if err != nil {
return err
}
return parseBaseResponseError(resp)
}
// 撤回消息操作
func (c *Caller) WebWxRevokeMsg(msg *SentMessage, request *BaseRequest) error {
resp, err := c.Client.WebWxRevokeMsg(msg, request)
if err != nil {
return err
}
return parseBaseResponseError(resp)
}
// 处理响应返回的结果是否正常
func parseBaseResponseError(resp *http.Response) error {
defer resp.Body.Close()
var item struct{ BaseResponse BaseResponse }
if err := scanJson(resp, &item); err != nil {
return err
}
if !item.BaseResponse.Ok() {
return item.BaseResponse
}
return nil
}
func parseMessageResponseError(resp *http.Response, msg *SentMessage) error {
defer resp.Body.Close()
var messageResp MessageResponse
if err := scanJson(resp, &messageResp); err != nil {
return err
}
if !messageResp.BaseResponse.Ok() {
return messageResp.BaseResponse
}
// 发送成功之后将msgId赋值给SendMessage
msg.MsgId = messageResp.MsgID
return nil
}
func getSuccessSentMessage(msg *SendMessage, resp *http.Response, err error) (*SentMessage, error) {
if err != nil {
return nil, err
}
sendSuccessMsg := &SentMessage{SendMessage: msg}
err = parseMessageResponseError(resp, sendSuccessMsg)
return sendSuccessMsg, err
}