@@ -3,8 +3,6 @@ package openwechat
33import (
44 "bytes"
55 "context"
6- "crypto/md5"
7- "encoding/hex"
86 "encoding/json"
97 "errors"
108 "fmt"
@@ -479,37 +477,87 @@ func (c *Client) WebWxGetHeadImg(ctx context.Context, user *User) (*http.Respons
479477 return c .Do (req )
480478}
481479
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"`
487488}
488489
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 )
494500 if err != nil {
495501 return nil , err
496502 }
497- if _ , err = file .Seek (0 , io .SeekStart ); err != nil {
503+ body , err := jsonEncode (req )
504+ if err != nil {
498505 return nil , err
499506 }
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 {
504509 return nil , err
505510 }
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+ }
507542
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 )
509556 if err != nil {
510557 return nil , err
511558 }
512- filename := sate .Name ()
559+
560+ filename := opt .Filename
513561
514562 if ext := filepath .Ext (filename ); ext == "" {
515563 names := strings .Split (contentType , "/" )
@@ -530,22 +578,24 @@ func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file *os.File, opt
530578
531579 cookies := c .Jar ().Cookies (path )
532580
533- webWxDataTicket , err := getWebWxDataTicket (cookies )
581+ webWxDataTicket , err := wxDataTicket (cookies )
534582 if err != nil {
535583 return nil , err
536584 }
537585
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 ,
549599 }
550600
551601 uploadMediaRequestByte , err := json .Marshal (uploadMediaRequest )
@@ -554,14 +604,14 @@ func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file *os.File, opt
554604 }
555605
556606 // 计算上传文件的次数
557- chunks := int ((sate . Size () + chunkSize - 1 ) / chunkSize )
607+ chunks := int ((opt . FileSize + chunkSize - 1 ) / chunkSize )
558608
559609 content := map [string ]string {
560610 "id" : "WU_FILE_0" ,
561611 "name" : filename ,
562612 "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 ),
565615 "mediatype" : mediaType ,
566616 "webwx_data_ticket" : webWxDataTicket ,
567617 "pass_ticket" : opt .LoginInfo .PassTicket ,
@@ -596,7 +646,7 @@ func (c *Client) WebWxUploadMediaByChunk(ctx context.Context, file *os.File, opt
596646 }
597647
598648 // create form file
599- fileWriter , err := writer .CreateFormFile ("filename" , file . Name () )
649+ fileWriter , err := writer .CreateFormFile ("filename" , filename )
600650 if err != nil {
601651 return err
602652 }
@@ -671,6 +721,7 @@ func (c *Client) WebWxSendAppMsg(ctx context.Context, msg *SendMessage, request
671721 params := url.Values {}
672722 params .Add ("fun" , "async" )
673723 params .Add ("f" , "json" )
724+ params .Add ("lang" , "zh_CN" )
674725 path .RawQuery = params .Encode ()
675726 return c .sendMessage (ctx , request , path .String (), msg )
676727}
@@ -815,7 +866,7 @@ func (c *Client) WebWxGetMedia(ctx context.Context, msg *Message, info *LoginInf
815866 return nil , err
816867 }
817868 cookies := c .Jar ().Cookies (path )
818- webWxDataTicket , err := getWebWxDataTicket (cookies )
869+ webWxDataTicket , err := wxDataTicket (cookies )
819870 if err != nil {
820871 return nil , err
821872 }
0 commit comments