From f3024864cfe491b8a82b996fd6c43d5b4a839b9b Mon Sep 17 00:00:00 2001 From: _oah <57302072@qq.com> Date: Thu, 11 Jul 2024 21:57:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=B5=81=E7=9A=84=E5=BD=A2=E5=BC=8F=E4=B8=8A=E4=BC=A0=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/content/content.go | 5 +++- miniprogram/security/security.go | 3 +- officialaccount/customerservice/manager.go | 3 +- officialaccount/material/material.go | 3 +- officialaccount/material/media.go | 34 +++++++++++++++++----- util/http.go | 25 +++++++++++----- work/material/media.go | 9 ++++-- 7 files changed, 61 insertions(+), 21 deletions(-) diff --git a/miniprogram/content/content.go b/miniprogram/content/content.go index d53f2db1e..f3f1b784c 100644 --- a/miniprogram/content/content.go +++ b/miniprogram/content/content.go @@ -53,9 +53,12 @@ func (content *Content) CheckImage(media string) error { if err != nil { return err } + var directory = media response, err := util.PostFile( "media", - media, + nil, + "", + directory, fmt.Sprintf(checkImageURL, accessToken), ) if err != nil { diff --git a/miniprogram/security/security.go b/miniprogram/security/security.go index 3e979a513..011ea9915 100644 --- a/miniprogram/security/security.go +++ b/miniprogram/security/security.go @@ -102,7 +102,8 @@ func (security *Security) ImageCheckV1(filename string) (err error) { } uri := fmt.Sprintf(imageCheckURL, accessToken) - response, err := util.PostFile("media", filename, uri) + var directory = filename + response, err := util.PostFile("media", nil, "", directory, uri) if err != nil { return } diff --git a/officialaccount/customerservice/manager.go b/officialaccount/customerservice/manager.go index 973a86c2f..dc92f4bca 100644 --- a/officialaccount/customerservice/manager.go +++ b/officialaccount/customerservice/manager.go @@ -212,7 +212,8 @@ func (csm *Manager) UploadHeadImg(kfAccount, fileName string) (err error) { } uri := fmt.Sprintf("%s?access_token=%s&kf_account=%s", customerServiceUploadHeadImg, accessToken, kfAccount) var response []byte - response, err = util.PostFile("media", fileName, uri) + var directory = fileName + response, err = util.PostFile("media", nil, "", directory, uri) if err != nil { return } diff --git a/officialaccount/material/material.go b/officialaccount/material/material.go index 5b5f1c6bc..0cb961f8d 100644 --- a/officialaccount/material/material.go +++ b/officialaccount/material/material.go @@ -174,7 +174,8 @@ func (material *Material) AddMaterial(mediaType MediaType, filename string) (med uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType) var response []byte - response, err = util.PostFile("media", filename, uri) + var directory = filename + response, err = util.PostFile("media", nil, "", directory, uri) if err != nil { return } diff --git a/officialaccount/material/media.go b/officialaccount/material/media.go index 316758fb4..867b92bd8 100644 --- a/officialaccount/material/media.go +++ b/officialaccount/material/media.go @@ -3,6 +3,9 @@ package material import ( "encoding/json" "fmt" + "io" + "net/http" + "strings" "github.com/silenceper/wechat/v2/util" ) @@ -38,16 +41,32 @@ type Media struct { } // MediaUpload 临时素材上传 -func (material *Material) MediaUpload(mediaType MediaType, filename string) (media Media, err error) { +func (material *Material) MediaUpload(mediaType MediaType, url string) (media Media, err error) { var accessToken string - accessToken, err = material.GetAccessToken() - if err != nil { + if accessToken, err = material.GetAccessToken(); err != nil { return } - uri := fmt.Sprintf("%s?access_token=%s&type=%s", mediaUploadURL, accessToken, mediaType) + // 使用strings.LastIndex函数找到最后一个斜杠的位置 + lastSlashIndex := strings.LastIndex(url, "/") + // 从最后一个斜杠的位置截取到最后,获取文件名 + filename := url[lastSlashIndex+1:] + // 获取图片 + resp, err := http.Get(url) + if err != nil { + err = fmt.Errorf("get image error: %v", err) + return + } + // 读取响应到内存 + var imageData []byte + imageData, err = io.ReadAll(resp.Body) + defer resp.Body.Close() + if err != nil { + err = fmt.Errorf("read image error: %v", err) + return + } var response []byte - response, err = util.PostFile("media", filename, uri) + response, err = util.PostFile("media", imageData, filename, "", uri) if err != nil { return } @@ -56,7 +75,7 @@ func (material *Material) MediaUpload(mediaType MediaType, filename string) (med return } if media.ErrCode != 0 { - err = fmt.Errorf("MediaUpload error : errcode=%v , errmsg=%v", media.ErrCode, media.ErrMsg) + err = fmt.Errorf("MediaUpload error : errcode=%v, errmsg=%v", media.ErrCode, media.ErrMsg) return } return @@ -91,7 +110,8 @@ func (material *Material) ImageUpload(filename string) (url string, err error) { uri := fmt.Sprintf("%s?access_token=%s", mediaUploadImageURL, accessToken) var response []byte - response, err = util.PostFile("media", filename, uri) + var directory = filename + response, err = util.PostFile("media", nil, "", directory, uri) if err != nil { return } diff --git a/util/http.go b/util/http.go index 26e4a7be4..b5d4d9ce5 100644 --- a/util/http.go +++ b/util/http.go @@ -146,13 +146,23 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e return responseData, contentType, err } -// PostFile 上传文件 -func PostFile(fieldName, filename, uri string) ([]byte, error) { +// PostFile 支持流或文件形式上传 +func PostFile(fieldName string, data []byte, fileName string, directory string, uri string) ([]byte, error) { + var fileContent []byte + var isFile bool + // 数据为空且文件目录不为空则按文件形式上传 + if len(data) == 0 && directory != "" { + isFile = true + } else { + fileContent = data + } fields := []MultipartFormField{ { - IsFile: true, + IsFile: isFile, Fieldname: fieldName, - Filename: filename, + Value: fileContent, + Filename: fileName, + Directory: directory, }, } return PostMultipartForm(fields, uri) @@ -164,6 +174,7 @@ type MultipartFormField struct { Fieldname string Value []byte Filename string + Directory string } // PostMultipartForm 上传文件或其他多个字段 @@ -176,13 +187,13 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte for _, field := range fields { if field.IsFile { - fileWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename) + fileWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Directory) if e != nil { err = fmt.Errorf("error writing to buffer , err=%v", e) return } - fh, e := os.Open(field.Filename) + fh, e := os.Open(field.Directory) if e != nil { err = fmt.Errorf("error opening file , err=%v", e) return @@ -193,7 +204,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte return } } else { - partWriter, e := bodyWriter.CreateFormField(field.Fieldname) + partWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename) if e != nil { err = e return diff --git a/work/material/media.go b/work/material/media.go index b32785a5c..0d812acd7 100644 --- a/work/material/media.go +++ b/work/material/media.go @@ -48,7 +48,8 @@ func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) { return nil, err } var response []byte - if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadImgURL, accessToken)); err != nil { + var directory = filename + if response, err = util.PostFile("media", nil, "", directory, fmt.Sprintf(uploadImgURL, accessToken)); err != nil { return nil, err } result := &UploadImgResponse{} @@ -68,7 +69,8 @@ func (r *Client) UploadTempFile(filename string, mediaType string) (*UploadTempF return nil, err } var response []byte - if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil { + var directory = filename + if response, err = util.PostFile("media", nil, "", directory, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil { return nil, err } result := &UploadTempFileResponse{} @@ -89,7 +91,8 @@ func (r *Client) UploadAttachment(filename string, mediaType string, attachmentT return nil, err } var response []byte - if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType)); err != nil { + var directory = filename + if response, err = util.PostFile("media", nil, "", directory, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil { return nil, err } result := &UploadAttachmentResponse{} From afaa30759a03727e5528b93e343dcba81fd2e778 Mon Sep 17 00:00:00 2001 From: _oah <57302072@qq.com> Date: Thu, 11 Jul 2024 21:57:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=B5=81=E7=9A=84=E5=BD=A2=E5=BC=8F=E4=B8=8A=E4=BC=A0=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/content/content.go | 5 +++- miniprogram/security/security.go | 3 +- officialaccount/customerservice/manager.go | 3 +- officialaccount/material/material.go | 3 +- officialaccount/material/media.go | 34 +++++++++++++++++----- util/http.go | 25 +++++++++++----- work/material/media.go | 9 ++++-- 7 files changed, 61 insertions(+), 21 deletions(-) diff --git a/miniprogram/content/content.go b/miniprogram/content/content.go index d53f2db1e..f3f1b784c 100644 --- a/miniprogram/content/content.go +++ b/miniprogram/content/content.go @@ -53,9 +53,12 @@ func (content *Content) CheckImage(media string) error { if err != nil { return err } + var directory = media response, err := util.PostFile( "media", - media, + nil, + "", + directory, fmt.Sprintf(checkImageURL, accessToken), ) if err != nil { diff --git a/miniprogram/security/security.go b/miniprogram/security/security.go index 3e979a513..011ea9915 100644 --- a/miniprogram/security/security.go +++ b/miniprogram/security/security.go @@ -102,7 +102,8 @@ func (security *Security) ImageCheckV1(filename string) (err error) { } uri := fmt.Sprintf(imageCheckURL, accessToken) - response, err := util.PostFile("media", filename, uri) + var directory = filename + response, err := util.PostFile("media", nil, "", directory, uri) if err != nil { return } diff --git a/officialaccount/customerservice/manager.go b/officialaccount/customerservice/manager.go index 973a86c2f..dc92f4bca 100644 --- a/officialaccount/customerservice/manager.go +++ b/officialaccount/customerservice/manager.go @@ -212,7 +212,8 @@ func (csm *Manager) UploadHeadImg(kfAccount, fileName string) (err error) { } uri := fmt.Sprintf("%s?access_token=%s&kf_account=%s", customerServiceUploadHeadImg, accessToken, kfAccount) var response []byte - response, err = util.PostFile("media", fileName, uri) + var directory = fileName + response, err = util.PostFile("media", nil, "", directory, uri) if err != nil { return } diff --git a/officialaccount/material/material.go b/officialaccount/material/material.go index 5b5f1c6bc..0cb961f8d 100644 --- a/officialaccount/material/material.go +++ b/officialaccount/material/material.go @@ -174,7 +174,8 @@ func (material *Material) AddMaterial(mediaType MediaType, filename string) (med uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType) var response []byte - response, err = util.PostFile("media", filename, uri) + var directory = filename + response, err = util.PostFile("media", nil, "", directory, uri) if err != nil { return } diff --git a/officialaccount/material/media.go b/officialaccount/material/media.go index 316758fb4..1d560e0ba 100644 --- a/officialaccount/material/media.go +++ b/officialaccount/material/media.go @@ -3,6 +3,9 @@ package material import ( "encoding/json" "fmt" + "io" + "net/http" + "strings" "github.com/silenceper/wechat/v2/util" ) @@ -38,16 +41,32 @@ type Media struct { } // MediaUpload 临时素材上传 -func (material *Material) MediaUpload(mediaType MediaType, filename string) (media Media, err error) { +func (material *Material) MediaUpload(mediaType MediaType, url string) (media Media, err error) { var accessToken string - accessToken, err = material.GetAccessToken() - if err != nil { + if accessToken, err = material.GetAccessToken(); err != nil { return } - uri := fmt.Sprintf("%s?access_token=%s&type=%s", mediaUploadURL, accessToken, mediaType) + // 使用strings.LastIndex函数找到最后一个斜杠的位置 + lastSlashIndex := strings.LastIndex(url, "/") + // 从最后一个斜杠的位置截取到最后,获取文件名 + filename := url[lastSlashIndex+1:] + // 获取资源 + resp, err := http.Get(url) + if err != nil { + err = fmt.Errorf("get image error: %v", err) + return + } + // 读取响应到内存 + var imageData []byte + imageData, err = io.ReadAll(resp.Body) + defer resp.Body.Close() + if err != nil { + err = fmt.Errorf("read image error: %v", err) + return + } var response []byte - response, err = util.PostFile("media", filename, uri) + response, err = util.PostFile("media", imageData, filename, "", uri) if err != nil { return } @@ -56,7 +75,7 @@ func (material *Material) MediaUpload(mediaType MediaType, filename string) (med return } if media.ErrCode != 0 { - err = fmt.Errorf("MediaUpload error : errcode=%v , errmsg=%v", media.ErrCode, media.ErrMsg) + err = fmt.Errorf("MediaUpload error : errcode=%v, errmsg=%v", media.ErrCode, media.ErrMsg) return } return @@ -91,7 +110,8 @@ func (material *Material) ImageUpload(filename string) (url string, err error) { uri := fmt.Sprintf("%s?access_token=%s", mediaUploadImageURL, accessToken) var response []byte - response, err = util.PostFile("media", filename, uri) + var directory = filename + response, err = util.PostFile("media", nil, "", directory, uri) if err != nil { return } diff --git a/util/http.go b/util/http.go index 26e4a7be4..b5d4d9ce5 100644 --- a/util/http.go +++ b/util/http.go @@ -146,13 +146,23 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e return responseData, contentType, err } -// PostFile 上传文件 -func PostFile(fieldName, filename, uri string) ([]byte, error) { +// PostFile 支持流或文件形式上传 +func PostFile(fieldName string, data []byte, fileName string, directory string, uri string) ([]byte, error) { + var fileContent []byte + var isFile bool + // 数据为空且文件目录不为空则按文件形式上传 + if len(data) == 0 && directory != "" { + isFile = true + } else { + fileContent = data + } fields := []MultipartFormField{ { - IsFile: true, + IsFile: isFile, Fieldname: fieldName, - Filename: filename, + Value: fileContent, + Filename: fileName, + Directory: directory, }, } return PostMultipartForm(fields, uri) @@ -164,6 +174,7 @@ type MultipartFormField struct { Fieldname string Value []byte Filename string + Directory string } // PostMultipartForm 上传文件或其他多个字段 @@ -176,13 +187,13 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte for _, field := range fields { if field.IsFile { - fileWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename) + fileWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Directory) if e != nil { err = fmt.Errorf("error writing to buffer , err=%v", e) return } - fh, e := os.Open(field.Filename) + fh, e := os.Open(field.Directory) if e != nil { err = fmt.Errorf("error opening file , err=%v", e) return @@ -193,7 +204,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte return } } else { - partWriter, e := bodyWriter.CreateFormField(field.Fieldname) + partWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename) if e != nil { err = e return diff --git a/work/material/media.go b/work/material/media.go index b32785a5c..0d812acd7 100644 --- a/work/material/media.go +++ b/work/material/media.go @@ -48,7 +48,8 @@ func (r *Client) UploadImg(filename string) (*UploadImgResponse, error) { return nil, err } var response []byte - if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadImgURL, accessToken)); err != nil { + var directory = filename + if response, err = util.PostFile("media", nil, "", directory, fmt.Sprintf(uploadImgURL, accessToken)); err != nil { return nil, err } result := &UploadImgResponse{} @@ -68,7 +69,8 @@ func (r *Client) UploadTempFile(filename string, mediaType string) (*UploadTempF return nil, err } var response []byte - if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil { + var directory = filename + if response, err = util.PostFile("media", nil, "", directory, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil { return nil, err } result := &UploadTempFileResponse{} @@ -89,7 +91,8 @@ func (r *Client) UploadAttachment(filename string, mediaType string, attachmentT return nil, err } var response []byte - if response, err = util.PostFile("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType)); err != nil { + var directory = filename + if response, err = util.PostFile("media", nil, "", directory, fmt.Sprintf(uploadTempFile, accessToken, mediaType)); err != nil { return nil, err } result := &UploadAttachmentResponse{}