@@ -3,8 +3,6 @@ package openwechat
3
3
import (
4
4
"bytes"
5
5
"context"
6
- "crypto/md5"
7
- "encoding/hex"
8
6
"encoding/json"
9
7
"errors"
10
8
"fmt"
@@ -479,37 +477,87 @@ func (c *Client) WebWxGetHeadImg(ctx context.Context, user *User) (*http.Respons
479
477
return c .Do (req )
480
478
}
481
479
482
- type ClientWebWxUploadMediaByChunkOptions struct {
483
- FromUserName string
484
- ToUserName string
485
- BaseRequest * BaseRequest
486
- LoginInfo * LoginInfo
480
+ type webWxCheckUploadRequest struct {
481
+ BaseRequest * BaseRequest `json:"BaseRequest"`
482
+ FileMd5 string `json:"FileMd5"`
483
+ FileName string `json:"FileName"`
484
+ FileSize int64 `json:"FileSize"`
485
+ FileType uint8 `json:"FileType"`
486
+ FromUserName string `json:"FromUserName"`
487
+ ToUserName string `json:"ToUserName"`
487
488
}
488
489
489
- // WebWxUploadMediaByChunk 分块上传文件
490
- // TODO 优化掉这个函数
491
- func (c * Client ) WebWxUploadMediaByChunk (ctx context.Context , file * os.File , opt * ClientWebWxUploadMediaByChunkOptions ) (* http.Response , error ) {
492
- // 获取文件上传的类型
493
- contentType , err := GetFileContentType (file )
490
+ type webWxCheckUploadResponse struct {
491
+ BaseResponse BaseResponse `json:"BaseResponse"`
492
+ MediaId string `json:"MediaId"`
493
+ AESKey string `json:"AESKey"`
494
+ Signature string `json:"Signature"`
495
+ EntryFileName string `json:"EncryFileName"`
496
+ }
497
+
498
+ func (c * Client ) webWxCheckUploadRequest (ctx context.Context , req webWxCheckUploadRequest ) (* http.Response , error ) {
499
+ path , err := url .Parse (c .Domain .BaseHost () + webwxcheckupload )
494
500
if err != nil {
495
501
return nil , err
496
502
}
497
- if _ , err = file .Seek (0 , io .SeekStart ); err != nil {
503
+ body , err := jsonEncode (req )
504
+ if err != nil {
498
505
return nil , err
499
506
}
500
-
501
- // 获取文件的md5
502
- h := md5 .New ()
503
- if _ , err = io .Copy (h , file ); err != nil {
507
+ reqs , err := http .NewRequestWithContext (ctx , http .MethodPost , path .String (), body )
508
+ if err != nil {
504
509
return nil , err
505
510
}
506
- fileMd5 := hex .EncodeToString (h .Sum (nil ))
511
+ reqs .Header .Add ("Content-Type" , jsonContentType )
512
+ return c .Do (reqs )
513
+ }
514
+
515
+ type uploadMediaRequest struct {
516
+ UploadType uint8 `json:"UploadType"`
517
+ BaseRequest * BaseRequest `json:"BaseRequest"`
518
+ ClientMediaId int64 `json:"ClientMediaId"`
519
+ TotalLen int64 `json:"TotalLen"`
520
+ StartPos int `json:"StartPos"`
521
+ DataLen int64 `json:"DataLen"`
522
+ MediaType uint8 `json:"MediaType"`
523
+ FromUserName string `json:"FromUserName"`
524
+ ToUserName string `json:"ToUserName"`
525
+ FileMd5 string `json:"FileMd5"`
526
+ AESKey string `json:"AESKey,omitempty"`
527
+ Signature string `json:"Signature,omitempty"`
528
+ }
529
+
530
+ type ClientWebWxUploadMediaByChunkOptions struct {
531
+ FromUserName string
532
+ ToUserName string
533
+ BaseRequest * BaseRequest
534
+ LoginInfo * LoginInfo
535
+ Filename string
536
+ FileMD5 string
537
+ FileSize int64
538
+ LastModifiedDate time.Time
539
+ AESKey string
540
+ Signature string
541
+ }
507
542
508
- sate , err := file .Stat ()
543
+ type UploadFile interface {
544
+ io.ReaderAt
545
+ io.ReadSeeker
546
+ }
547
+
548
+ // WebWxUploadMediaByChunk 分块上传文件
549
+ // TODO 优化掉这个函数
550
+ func (c * Client ) WebWxUploadMediaByChunk (ctx context.Context , file UploadFile , opt * ClientWebWxUploadMediaByChunkOptions ) (* http.Response , error ) {
551
+ // 获取文件上传的类型
552
+ if _ , err := file .Seek (0 , io .SeekStart ); err != nil {
553
+ return nil , err
554
+ }
555
+ contentType , err := GetFileContentType (file )
509
556
if err != nil {
510
557
return nil , err
511
558
}
512
- filename := sate .Name ()
559
+
560
+ filename := opt .Filename
513
561
514
562
if ext := filepath .Ext (filename ); ext == "" {
515
563
names := strings .Split (contentType , "/" )
@@ -530,22 +578,24 @@ func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file *os.File, opt
530
578
531
579
cookies := c .Jar ().Cookies (path )
532
580
533
- webWxDataTicket , err := getWebWxDataTicket (cookies )
581
+ webWxDataTicket , err := wxDataTicket (cookies )
534
582
if err != nil {
535
583
return nil , err
536
584
}
537
585
538
- uploadMediaRequest := map [string ]interface {}{
539
- "UploadType" : 2 ,
540
- "BaseRequest" : opt .BaseRequest ,
541
- "ClientMediaId" : time .Now ().Unix () * 1e4 ,
542
- "TotalLen" : sate .Size (),
543
- "StartPos" : 0 ,
544
- "DataLen" : sate .Size (),
545
- "MediaType" : 4 ,
546
- "FromUserName" : opt .FromUserName ,
547
- "ToUserName" : opt .ToUserName ,
548
- "FileMd5" : fileMd5 ,
586
+ uploadMediaRequest := & uploadMediaRequest {
587
+ UploadType : 2 ,
588
+ BaseRequest : opt .BaseRequest ,
589
+ ClientMediaId : time .Now ().Unix () * 1e4 ,
590
+ TotalLen : opt .FileSize ,
591
+ StartPos : 0 ,
592
+ DataLen : opt .FileSize ,
593
+ MediaType : 4 ,
594
+ FromUserName : opt .FromUserName ,
595
+ ToUserName : opt .ToUserName ,
596
+ FileMd5 : opt .FileMD5 ,
597
+ AESKey : opt .AESKey ,
598
+ Signature : opt .Signature ,
549
599
}
550
600
551
601
uploadMediaRequestByte , err := json .Marshal (uploadMediaRequest )
@@ -554,14 +604,14 @@ func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file *os.File, opt
554
604
}
555
605
556
606
// 计算上传文件的次数
557
- chunks := int ((sate . Size () + chunkSize - 1 ) / chunkSize )
607
+ chunks := int ((opt . FileSize + chunkSize - 1 ) / chunkSize )
558
608
559
609
content := map [string ]string {
560
610
"id" : "WU_FILE_0" ,
561
611
"name" : filename ,
562
612
"type" : contentType ,
563
- "lastModifiedDate" : sate . ModTime () .Format (TimeFormat ),
564
- "size" : strconv .FormatInt (sate . Size () , 10 ),
613
+ "lastModifiedDate" : opt . LastModifiedDate .Format (TimeFormat ),
614
+ "size" : strconv .FormatInt (opt . FileSize , 10 ),
565
615
"mediatype" : mediaType ,
566
616
"webwx_data_ticket" : webWxDataTicket ,
567
617
"pass_ticket" : opt .LoginInfo .PassTicket ,
@@ -596,7 +646,7 @@ func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file *os.File, opt
596
646
}
597
647
598
648
// create form file
599
- fileWriter , err := writer .CreateFormFile ("filename" , file . Name () )
649
+ fileWriter , err := writer .CreateFormFile ("filename" , filename )
600
650
if err != nil {
601
651
return err
602
652
}
@@ -671,6 +721,7 @@ func (c *Client) WebWxSendAppMsg(ctx context.Context, msg *SendMessage, request
671
721
params := url.Values {}
672
722
params .Add ("fun" , "async" )
673
723
params .Add ("f" , "json" )
724
+ params .Add ("lang" , "zh_CN" )
674
725
path .RawQuery = params .Encode ()
675
726
return c .sendMessage (ctx , request , path .String (), msg )
676
727
}
@@ -815,7 +866,7 @@ func (c *Client) WebWxGetMedia(ctx context.Context, msg *Message, info *LoginInf
815
866
return nil , err
816
867
}
817
868
cookies := c .Jar ().Cookies (path )
818
- webWxDataTicket , err := getWebWxDataTicket (cookies )
869
+ webWxDataTicket , err := wxDataTicket (cookies )
819
870
if err != nil {
820
871
return nil , err
821
872
}
0 commit comments