-
-
Notifications
You must be signed in to change notification settings - Fork 961
/
client.go
1223 lines (1124 loc) · 34.2 KB
/
client.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package openwechat
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// HttpHook 请求上下文钩子
type HttpHook interface {
// BeforeRequest 将在请求之前调用
BeforeRequest(req *http.Request)
// AfterRequest 将在请求之后调用,无论请求成功与否
AfterRequest(response *http.Response, err error)
}
// HttpHooks 请求上下文钩子列表
type HttpHooks []HttpHook
// BeforeRequest 将在请求之前调用
func (h HttpHooks) BeforeRequest(req *http.Request) {
if len(h) == 0 {
return
}
for _, hook := range h {
hook.BeforeRequest(req)
}
}
// AfterRequest 将在请求之后调用,无论请求成功与否
func (h HttpHooks) AfterRequest(response *http.Response, err error) {
if len(h) == 0 {
return
}
for _, hook := range h {
hook.AfterRequest(response, err)
}
}
type UserAgentHook struct {
UserAgent string
}
func (u UserAgentHook) BeforeRequest(req *http.Request) {
req.Header.Set("User-Agent", u.UserAgent)
}
func (u UserAgentHook) AfterRequest(_ *http.Response, _ error) {}
// defaultUserAgentHook 默认的User-Agent钩子
var defaultUserAgentHook = UserAgentHook{"Mozilla/5.0 (Linux; U; UOS x86_64; zh-cn) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 UOSBrowser/6.0.1.1001"}
// Client http请求客户端
// 客户端需要维持Session会话
type Client struct {
// 设置一些client的请求行为
// see normalMode desktopMode
mode Mode
// client http客户端
client *http.Client
// Domain 微信服务器请求域名
// 这个参数会在登录成功后被赋值
// 之后所有的请求都会使用这个域名
// 在登录热登录和扫码登录时会被重新赋值
Domain WechatDomain
// HttpHooks 请求上下文钩子
HttpHooks HttpHooks
// MaxRetryTimes 最大重试次数
MaxRetryTimes int
}
// NewClient 创建一个新的客户端
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
panic("http client is nil")
}
client := &Client{client: httpClient}
client.MaxRetryTimes = 1
client.SetCookieJar(NewJar())
return client
}
// DefaultClient 自动存储cookie
// 设置客户端不自动跳转
func DefaultClient() *Client {
httpClient := &http.Client{
// 设置客户端不自动跳转
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
// 设置30秒超时
// 因为微信同步消息时是一个时间长达25秒的长轮训
Timeout: 30 * time.Second,
}
client := NewClient(httpClient)
client.AddHttpHook(defaultUserAgentHook)
client.MaxRetryTimes = 5
return client
}
// AddHttpHook 添加一个请求上下文钩子
func (c *Client) AddHttpHook(hooks ...HttpHook) {
c.HttpHooks = append(c.HttpHooks, hooks...)
}
func (c *Client) do(req *http.Request) (*http.Response, error) {
// 确保请求能够被执行
if c.MaxRetryTimes <= 0 {
c.MaxRetryTimes = 1
}
var (
resp *http.Response
err error
requestBody *bytes.Reader
)
c.HttpHooks.BeforeRequest(req)
defer func() { c.HttpHooks.AfterRequest(resp, err) }()
if req.Body != nil {
rawBody, err := io.ReadAll(req.Body)
if err != nil {
return nil, fmt.Errorf("io.ReadAll: %w", err)
}
requestBody = bytes.NewReader(rawBody)
}
for i := 0; i < c.MaxRetryTimes; i++ {
if requestBody != nil {
_, err := requestBody.Seek(0, io.SeekStart)
if err != nil {
return nil, fmt.Errorf("requestBody.Seek: %w", err)
}
req.Body = io.NopCloser(requestBody)
}
resp, err = c.client.Do(req)
if err == nil {
break
}
}
if err != nil {
err = errors.Join(NetworkErr, err)
}
return resp, err
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
return c.do(req)
}
// Jar 返回当前client的 http.CookieJar
// this http.CookieJar must be *Jar type
func (c *Client) Jar() *Jar {
return c.client.Jar.(*Jar)
}
// SetCookieJar 设置cookieJar
// 这里限制了cookieJar必须是Jar类型
// 否则进行cookie序列化的时候因为字段的私有性无法进行所有字段的导出
func (c *Client) SetCookieJar(jar *Jar) {
c.client.Jar = jar
}
// HTTPClient 返回http.Client
// 用于自定义http.Client的行为,如设置超时时间、设置代理、设置TLS配置等
func (c *Client) HTTPClient() *http.Client {
return c.client
}
// GetLoginUUID 获取登录的uuid
func (c *Client) GetLoginUUID(ctx context.Context) (*http.Response, error) {
req, err := c.mode.BuildGetLoginUUIDRequest(ctx)
if err != nil {
return nil, err
}
return c.Do(req)
}
// GetLoginQrcode 获取登录的二维吗
func (c *Client) GetLoginQrcode(ctx context.Context, uuid string) (*http.Response, error) {
path := qrcode + uuid
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
return c.client.Do(req)
}
// CheckLogin 检查是否登录
func (c *Client) CheckLogin(ctx context.Context, uuid, tip string) (*http.Response, error) {
path, err := url.Parse(login)
if err != nil {
return nil, err
}
now := time.Now().Unix()
params := url.Values{}
params.Add("r", strconv.FormatInt(now/1579, 10))
params.Add("_", strconv.FormatInt(now, 10))
params.Add("loginicon", "true")
params.Add("uuid", uuid)
params.Add("tip", tip)
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// GetLoginInfo 请求获取LoginInfo
func (c *Client) GetLoginInfo(ctx context.Context, path *url.URL) (*http.Response, error) {
req, err := c.mode.BuildGetLoginInfoRequest(ctx, path.String())
if err != nil {
return nil, err
}
return c.Do(req)
}
// WebInit 请求获取初始化信息
func (c *Client) WebInit(ctx context.Context, request *BaseRequest) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxinit)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("_", fmt.Sprintf("%d", time.Now().Unix()))
path.RawQuery = params.Encode()
content := struct{ BaseRequest *BaseRequest }{BaseRequest: request}
body, err := jsonEncode(content)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", jsonContentType)
return c.Do(req)
}
type ClientCommonOptions struct {
BaseRequest *BaseRequest
WebInitResponse *WebInitResponse
LoginInfo *LoginInfo
}
type ClientWebWxStatusNotifyOptions ClientCommonOptions
// WebWxStatusNotify 通知手机已登录
func (c *Client) WebWxStatusNotify(ctx context.Context, opt *ClientWebWxStatusNotifyOptions) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxstatusnotify)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("lang", "zh_CN")
params.Add("pass_ticket", opt.LoginInfo.PassTicket)
username := opt.WebInitResponse.User.UserName
content := map[string]interface{}{
"BaseRequest": opt.BaseRequest,
"ClientMsgId": time.Now().Unix(),
"Code": 3,
"FromUserName": username,
"ToUserName": username,
}
path.RawQuery = params.Encode()
buffer, err := jsonEncode(content)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), buffer)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", jsonContentType)
return c.Do(req)
}
type ClientSyncCheckOptions ClientCommonOptions
// SyncCheck 异步检查是否有新的消息返回
func (c *Client) SyncCheck(ctx context.Context, opt *ClientSyncCheckOptions) (*http.Response, error) {
path, err := url.Parse(c.Domain.SyncHost() + synccheck)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("r", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
params.Add("skey", opt.LoginInfo.SKey)
params.Add("sid", opt.LoginInfo.WxSid)
params.Add("uin", strconv.FormatInt(opt.LoginInfo.WxUin, 10))
params.Add("deviceid", opt.BaseRequest.DeviceID)
params.Add("_", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
var syncKeyStringSlice = make([]string, opt.WebInitResponse.SyncKey.Count)
// 将SyncKey里面的元素按照特定的格式拼接起来
for index, item := range opt.WebInitResponse.SyncKey.List {
i := fmt.Sprintf("%d_%d", item.Key, item.Val)
syncKeyStringSlice[index] = i
}
syncKey := strings.Join(syncKeyStringSlice, "|")
params.Add("synckey", syncKey)
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// WebWxGetContact 获取联系人信息
func (c *Client) WebWxGetContact(ctx context.Context, sKey string, reqs int64) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxgetcontact)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("r", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
params.Add("skey", sKey)
params.Add("seq", strconv.FormatInt(reqs, 10))
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// WebWxBatchGetContact 获取联系人详情
func (c *Client) WebWxBatchGetContact(ctx context.Context, members Members, request *BaseRequest) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxbatchgetcontact)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("type", "ex")
params.Add("r", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
path.RawQuery = params.Encode()
list := NewUserDetailItemList(members)
content := map[string]interface{}{
"BaseRequest": request,
"Count": members.Count(),
"List": list,
}
body, err := jsonEncode(content)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", jsonContentType)
return c.Do(req)
}
type ClientWebWxSyncOptions ClientCommonOptions
// WebWxSync 获取消息接口
func (c *Client) WebWxSync(ctx context.Context, opt *ClientWebWxSyncOptions) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxsync)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("sid", opt.LoginInfo.WxSid)
params.Add("skey", opt.LoginInfo.SKey)
params.Add("pass_ticket", opt.LoginInfo.PassTicket)
path.RawQuery = params.Encode()
content := map[string]interface{}{
"BaseRequest": opt.BaseRequest,
"SyncKey": opt.WebInitResponse.SyncKey,
"rr": strconv.FormatInt(time.Now().Unix(), 10),
}
reader, err := jsonEncode(content)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), reader)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", jsonContentType)
return c.Do(req)
}
// 发送消息
func (c *Client) sendMessage(ctx context.Context, request *BaseRequest, url string, msg *SendMessage) (*http.Response, error) {
content := map[string]interface{}{
"BaseRequest": request,
"Msg": msg,
"Scene": 0,
}
body, err := jsonEncode(content)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", jsonContentType)
return c.Do(req)
}
type ClientWebWxSendMsgOptions struct {
LoginInfo *LoginInfo
BaseRequest *BaseRequest
Message *SendMessage
}
// WebWxSendMsg 发送文本消息
func (c *Client) WebWxSendMsg(ctx context.Context, opt *ClientWebWxSendMsgOptions) (*http.Response, error) {
opt.Message.Type = MsgTypeText
path, err := url.Parse(c.Domain.BaseHost() + webwxsendmsg)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("lang", "zh_CN")
params.Add("pass_ticket", opt.LoginInfo.PassTicket)
path.RawQuery = params.Encode()
return c.sendMessage(ctx, opt.BaseRequest, path.String(), opt.Message)
}
// WebWxSendMsg 发送表情消息
func (c *Client) WebWxSendEmoticon(ctx context.Context, opt *ClientWebWxSendMsgOptions) (*http.Response, error) {
opt.Message.Type = MsgTypeText
path, err := url.Parse(c.Domain.BaseHost() + webwxsendemoticon)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("fun", "sys")
params.Add("lang", "zh_CN")
params.Add("pass_ticket", opt.LoginInfo.PassTicket)
path.RawQuery = params.Encode()
return c.sendMessage(ctx, opt.BaseRequest, path.String(), opt.Message)
}
// WebWxGetHeadImg 获取用户的头像
func (c *Client) WebWxGetHeadImg(ctx context.Context, user *User) (*http.Response, error) {
var path string
if user.HeadImgUrl != "" {
path = c.Domain.BaseHost() + user.HeadImgUrl
} else {
params := url.Values{}
params.Add("username", user.UserName)
params.Add("skey", user.self.bot.Storage.Request.Skey)
params.Add("type", "big")
params.Add("chatroomid", user.EncryChatRoomId)
params.Add("seq", "0")
URL, err := url.Parse(c.Domain.BaseHost() + webwxgeticon)
if err != nil {
return nil, err
}
URL.RawQuery = params.Encode()
path = URL.String()
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
type webWxCheckUploadRequest struct {
BaseRequest *BaseRequest `json:"BaseRequest"`
FileMd5 string `json:"FileMd5"`
FileName string `json:"FileName"`
FileSize int64 `json:"FileSize"`
FileType uint8 `json:"FileType"`
FromUserName string `json:"FromUserName"`
ToUserName string `json:"ToUserName"`
}
type webWxCheckUploadResponse struct {
BaseResponse BaseResponse `json:"BaseResponse"`
MediaId string `json:"MediaId"`
AESKey string `json:"AESKey"`
Signature string `json:"Signature"`
EntryFileName string `json:"EncryFileName"`
}
func (c *Client) webWxCheckUploadRequest(ctx context.Context, req webWxCheckUploadRequest) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxcheckupload)
if err != nil {
return nil, err
}
body, err := jsonEncode(req)
if err != nil {
return nil, err
}
reqs, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), body)
if err != nil {
return nil, err
}
reqs.Header.Add("Content-Type", jsonContentType)
return c.Do(reqs)
}
type uploadMediaRequest struct {
UploadType uint8 `json:"UploadType"`
BaseRequest *BaseRequest `json:"BaseRequest"`
ClientMediaId int64 `json:"ClientMediaId"`
TotalLen int64 `json:"TotalLen"`
StartPos int `json:"StartPos"`
DataLen int64 `json:"DataLen"`
MediaType uint8 `json:"MediaType"`
FromUserName string `json:"FromUserName"`
ToUserName string `json:"ToUserName"`
FileMd5 string `json:"FileMd5"`
AESKey string `json:"AESKey,omitempty"`
Signature string `json:"Signature,omitempty"`
}
type ClientWebWxUploadMediaByChunkOptions struct {
FromUserName string
ToUserName string
BaseRequest *BaseRequest
LoginInfo *LoginInfo
Filename string
FileMD5 string
FileSize int64
LastModifiedDate time.Time
AESKey string
Signature string
}
type UploadFile interface {
io.ReaderAt
io.ReadSeeker
}
// WebWxUploadMediaByChunk 分块上传文件
// TODO 优化掉这个函数
func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file UploadFile, opt *ClientWebWxUploadMediaByChunkOptions) (*http.Response, error) {
// 获取文件上传的类型
if _, err := file.Seek(0, io.SeekStart); err != nil {
return nil, err
}
contentType, err := GetFileContentType(file)
if err != nil {
return nil, err
}
filename := opt.Filename
if ext := filepath.Ext(filename); ext == "" {
names := strings.Split(contentType, "/")
filename = filename + "." + names[len(names)-1]
}
// 获取文件的类型
mediaType := messageType(filename)
path, err := url.Parse(c.Domain.FileHost() + webwxuploadmedia)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("f", "json")
path.RawQuery = params.Encode()
cookies := c.Jar().Cookies(path)
webWxDataTicket, err := wxDataTicket(cookies)
if err != nil {
return nil, err
}
uploadMediaRequest := &uploadMediaRequest{
UploadType: 2,
BaseRequest: opt.BaseRequest,
ClientMediaId: time.Now().Unix() * 1e4,
TotalLen: opt.FileSize,
StartPos: 0,
DataLen: opt.FileSize,
MediaType: 4,
FromUserName: opt.FromUserName,
ToUserName: opt.ToUserName,
FileMd5: opt.FileMD5,
AESKey: opt.AESKey,
Signature: opt.Signature,
}
uploadMediaRequestByte, err := json.Marshal(uploadMediaRequest)
if err != nil {
return nil, err
}
// 计算上传文件的次数
chunks := int((opt.FileSize + chunkSize - 1) / chunkSize)
content := map[string]string{
"id": "WU_FILE_0",
"name": filename,
"type": contentType,
"lastModifiedDate": opt.LastModifiedDate.Format(TimeFormat),
"size": strconv.FormatInt(opt.FileSize, 10),
"mediatype": mediaType,
"webwx_data_ticket": webWxDataTicket,
"pass_ticket": opt.LoginInfo.PassTicket,
}
if chunks > 1 {
content["chunks"] = strconv.Itoa(chunks)
}
var (
resp *http.Response
formBuffer = bytes.NewBuffer(nil)
)
upload := func(chunk int, fileReader io.Reader) error {
if chunks > 1 {
content["chunk"] = strconv.Itoa(chunk)
}
formBuffer.Reset()
writer := multipart.NewWriter(formBuffer)
// write form data
{
if err = writer.WriteField("uploadmediarequest", string(uploadMediaRequestByte)); err != nil {
return err
}
for k, v := range content {
if err := writer.WriteField(k, v); err != nil {
return err
}
}
// create form file
fileWriter, err := writer.CreateFormFile("filename", filename)
if err != nil {
return err
}
if _, err = io.Copy(fileWriter, fileReader); err != nil {
return err
}
if err = writer.Close(); err != nil {
return err
}
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), formBuffer)
if err != nil {
return err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err = c.Do(req)
if err != nil {
return err
}
// parse response error
{
isLastTime := chunk+1 == chunks
if !isLastTime {
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{Reader: resp.Body}
err = parser.Err()
}
}
return err
}
// 分块上传
for chunk := 0; chunk < chunks; chunk++ {
// chunk reader
selectionReader := io.NewSectionReader(file, int64(chunk)*chunkSize, chunkSize)
// try to upload
if err = upload(chunk, selectionReader); err != nil {
return nil, err
}
}
// 将最后一次携带文件信息的response返回
return resp, err
}
// WebWxSendMsgImg 发送图片
// 这个接口依赖上传文件的接口
// 发送的图片必须是已经成功上传的图片
func (c *Client) WebWxSendMsgImg(ctx context.Context, opt *ClientWebWxSendMsgOptions) (*http.Response, error) {
opt.Message.Type = MsgTypeImage
path, err := url.Parse(c.Domain.BaseHost() + webwxsendmsgimg)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("fun", "async")
params.Add("f", "json")
params.Add("lang", "zh_CN")
params.Add("pass_ticket", opt.LoginInfo.PassTicket)
path.RawQuery = params.Encode()
return c.sendMessage(ctx, opt.BaseRequest, path.String(), opt.Message)
}
// WebWxSendAppMsg 发送文件信息
func (c *Client) WebWxSendAppMsg(ctx context.Context, msg *SendMessage, request *BaseRequest) (*http.Response, error) {
msg.Type = AppMessage
path, err := url.Parse(c.Domain.BaseHost() + webwxsendappmsg)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("fun", "async")
params.Add("f", "json")
params.Add("lang", "zh_CN")
path.RawQuery = params.Encode()
return c.sendMessage(ctx, request, path.String(), msg)
}
type ClientWebWxOplogOption struct {
RemarkName string
UserName string
BaseRequest *BaseRequest
}
// WebWxOplog 用户重命名接口
func (c *Client) WebWxOplog(ctx context.Context, opt *ClientWebWxOplogOption) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxoplog)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("lang", "zh_CN")
path.RawQuery = params.Encode()
content := map[string]interface{}{
"BaseRequest": opt.BaseRequest,
"CmdId": 2,
"RemarkName": opt.RemarkName,
"UserName": opt.UserName,
}
body, err := jsonEncode(content)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", jsonContentType)
return c.Do(req)
}
type ClientWebWxVerifyUserOption struct {
RecommendInfo RecommendInfo
VerifyContent string
BaseRequest *BaseRequest
LoginInfo *LoginInfo
}
// WebWxVerifyUser 添加用户为好友接口
func (c *Client) WebWxVerifyUser(ctx context.Context, opt *ClientWebWxVerifyUserOption) (*http.Response, error) {
loginInfo := opt.LoginInfo
path, err := url.Parse(c.Domain.BaseHost() + webwxverifyuser)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("r", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
params.Add("lang", "zh_CN")
params.Add("pass_ticket", loginInfo.PassTicket)
path.RawQuery = params.Encode()
content := map[string]interface{}{
"BaseRequest": opt.BaseRequest,
"Opcode": 3,
"SceneList": [1]int{33},
"SceneListCount": 1,
"VerifyContent": opt.VerifyContent,
"VerifyUserList": []interface{}{map[string]string{
"Value": opt.RecommendInfo.UserName,
"VerifyUserTicket": opt.RecommendInfo.Ticket,
}},
"VerifyUserListSize": 1,
"skey": opt.BaseRequest.Skey,
}
body, err := jsonEncode(content)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", jsonContentType)
return c.Do(req)
}
// WebWxGetMsgImg 获取图片消息的图片响应
func (c *Client) WebWxGetMsgImg(ctx context.Context, msg *Message, info *LoginInfo) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxgetmsgimg)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("MsgID", msg.MsgId)
params.Add("skey", info.SKey)
// params.Add("type", "slave")
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// WebWxGetVoice 获取语音消息的语音响应
func (c *Client) WebWxGetVoice(ctx context.Context, msg *Message, info *LoginInfo) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxgetvoice)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("msgid", msg.MsgId)
params.Add("skey", info.SKey)
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Referer", path.String())
req.Header.Add("Range", "bytes=0-")
return c.Do(req)
}
// WebWxGetVideo 获取视频消息的视频响应
func (c *Client) WebWxGetVideo(ctx context.Context, msg *Message, info *LoginInfo) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxgetvideo)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("msgid", msg.MsgId)
params.Add("skey", info.SKey)
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Referer", path.String())
req.Header.Add("Range", "bytes=0-")
return c.Do(req)
}
// WebWxGetMedia 获取文件消息的文件响应
func (c *Client) WebWxGetMedia(ctx context.Context, msg *Message, info *LoginInfo) (*http.Response, error) {
path, err := url.Parse(c.Domain.FileHost() + webwxgetmedia)
if err != nil {
return nil, err
}
cookies := c.Jar().Cookies(path)
webWxDataTicket, err := wxDataTicket(cookies)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("sender", msg.FromUserName)
params.Add("mediaid", msg.MediaId)
params.Add("encryfilename", msg.EncryFileName)
params.Add("fromuser", strconv.FormatInt(info.WxUin, 10))
params.Add("pass_ticket", info.PassTicket)
params.Add("webwx_data_ticket", webWxDataTicket)
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Referer", c.Domain.BaseHost()+"/")
return c.Do(req)
}
// Logout 用户退出
func (c *Client) Logout(ctx context.Context, info *LoginInfo) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxlogout)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("redirect", "1")
params.Add("type", "1")
params.Add("skey", info.SKey)
path.RawQuery = params.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, path.String(), nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
type ClientAddMemberIntoChatRoomOption struct {
Group string
GroupLength int
InviteMemberList []string
BaseRequest *BaseRequest
LoginInfo *LoginInfo
}
// AddMemberIntoChatRoom 添加用户进群聊
func (c *Client) AddMemberIntoChatRoom(ctx context.Context, opt *ClientAddMemberIntoChatRoomOption) (*http.Response, error) {
if opt.GroupLength >= 40 {
return c.InviteMemberIntoChatRoom(ctx, opt)
}
return c.addMemberIntoChatRoom(ctx, opt)
}
// addMemberIntoChatRoom 添加用户进群聊
func (c *Client) addMemberIntoChatRoom(ctx context.Context, opt *ClientAddMemberIntoChatRoomOption) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxupdatechatroom)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("fun", "addmember")
params.Add("pass_ticket", opt.LoginInfo.PassTicket)
params.Add("lang", "zh_CN")
path.RawQuery = params.Encode()
content := map[string]interface{}{
"ChatRoomName": opt.Group,
"BaseRequest": opt.BaseRequest,
"AddMemberList": strings.Join(opt.InviteMemberList, ","),
}
buffer, err := jsonEncode(content)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), buffer)
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", jsonContentType)
return c.Do(httpReq)
}
// InviteMemberIntoChatRoom 邀请用户进群聊
func (c *Client) InviteMemberIntoChatRoom(ctx context.Context, opt *ClientAddMemberIntoChatRoomOption) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxupdatechatroom)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("fun", "invitemember")
params.Add("pass_ticket", opt.LoginInfo.PassTicket)
params.Add("lang", "zh_CN")
path.RawQuery = params.Encode()
content := map[string]interface{}{
"ChatRoomName": opt.Group,
"BaseRequest": opt.BaseRequest,
"InviteMemberList": strings.Join(opt.InviteMemberList, ","),
}
buffer, err := jsonEncode(content)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, path.String(), buffer)
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", jsonContentType)
return c.Do(httpReq)
}
type ClientRemoveMemberFromChatRoomOption struct {
Group string
DelMemberList []string
BaseRequest *BaseRequest
LoginInfo *LoginInfo
}
// RemoveMemberFromChatRoom 从群聊中移除用户
func (c *Client) RemoveMemberFromChatRoom(ctx context.Context, opt *ClientRemoveMemberFromChatRoomOption) (*http.Response, error) {
path, err := url.Parse(c.Domain.BaseHost() + webwxupdatechatroom)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("fun", "delmember")
params.Add("lang", "zh_CN")
params.Add("pass_ticket", opt.LoginInfo.PassTicket)
content := map[string]interface{}{
"ChatRoomName": opt.Group,
"BaseRequest": opt.BaseRequest,
"DelMemberList": strings.Join(opt.DelMemberList, ","),
}