mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-14 01:32:27 +08:00
Compare commits
2 Commits
v2.1.7
...
22a172e3af
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
22a172e3af | ||
|
|
de92dc0dcd |
2
cache/redis.go
vendored
2
cache/redis.go
vendored
@@ -16,7 +16,6 @@ type Redis struct {
|
|||||||
// RedisOpts redis 连接属性
|
// RedisOpts redis 连接属性
|
||||||
type RedisOpts struct {
|
type RedisOpts struct {
|
||||||
Host string `yml:"host" json:"host"`
|
Host string `yml:"host" json:"host"`
|
||||||
Username string `yaml:"username" json:"username"`
|
|
||||||
Password string `yml:"password" json:"password"`
|
Password string `yml:"password" json:"password"`
|
||||||
Database int `yml:"database" json:"database"`
|
Database int `yml:"database" json:"database"`
|
||||||
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
||||||
@@ -29,7 +28,6 @@ func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
|
|||||||
conn := redis.NewUniversalClient(&redis.UniversalOptions{
|
conn := redis.NewUniversalClient(&redis.UniversalOptions{
|
||||||
Addrs: []string{opts.Host},
|
Addrs: []string{opts.Host},
|
||||||
DB: opts.Database,
|
DB: opts.Database,
|
||||||
Username: opts.Username,
|
|
||||||
Password: opts.Password,
|
Password: opts.Password,
|
||||||
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
||||||
MinIdleConns: opts.MaxIdle,
|
MinIdleConns: opts.MaxIdle,
|
||||||
|
|||||||
@@ -54,8 +54,6 @@ type QRCoder struct {
|
|||||||
IsHyaline bool `json:"is_hyaline,omitempty"`
|
IsHyaline bool `json:"is_hyaline,omitempty"`
|
||||||
// envVersion 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"
|
// envVersion 要打开的小程序版本。正式版为 "release",体验版为 "trial",开发版为 "develop"
|
||||||
EnvVersion string `json:"env_version,omitempty"`
|
EnvVersion string `json:"env_version,omitempty"`
|
||||||
// ShowSplashAd 控制通过该小程序码进入小程序是否展示封面广告1、默认为true,展示封面广告2、传入为false时,不展示封面广告
|
|
||||||
ShowSplashAd bool `json:"show_splash_ad,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchCode 请求并返回二维码二进制数据
|
// fetchCode 请求并返回二维码二进制数据
|
||||||
|
|||||||
@@ -4,9 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/officialaccount/context"
|
"github.com/silenceper/wechat/v2/officialaccount/context"
|
||||||
"github.com/silenceper/wechat/v2/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
@@ -163,8 +160,8 @@ type resAddMaterial struct {
|
|||||||
URL string `json:"url"`
|
URL string `json:"url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddMaterialFromReader 上传永久性素材(处理视频需要单独上传),从 io.Reader 中读取
|
// AddMaterial 上传永久性素材(处理视频需要单独上传)
|
||||||
func (material *Material) AddMaterialFromReader(mediaType MediaType, filePath string, reader io.Reader) (mediaID string, url string, err error) {
|
func (material *Material) AddMaterial(mediaType MediaType, filename string) (mediaID string, url string, err error) {
|
||||||
if mediaType == MediaTypeVideo {
|
if mediaType == MediaTypeVideo {
|
||||||
err = errors.New("永久视频素材上传使用 AddVideo 方法")
|
err = errors.New("永久视频素材上传使用 AddVideo 方法")
|
||||||
return
|
return
|
||||||
@@ -176,10 +173,8 @@ func (material *Material) AddMaterialFromReader(mediaType MediaType, filePath st
|
|||||||
}
|
}
|
||||||
|
|
||||||
uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType)
|
uri := fmt.Sprintf("%s?access_token=%s&type=%s", addMaterialURL, accessToken, mediaType)
|
||||||
// 获取文件名
|
|
||||||
filename := path.Base(filePath)
|
|
||||||
var response []byte
|
var response []byte
|
||||||
response, err = util.PostFileFromReader("media", filePath, filename, uri, reader)
|
response, err = util.PostFile("media", filename, uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -197,24 +192,13 @@ func (material *Material) AddMaterialFromReader(mediaType MediaType, filePath st
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddMaterial 上传永久性素材(处理视频需要单独上传)
|
|
||||||
func (material *Material) AddMaterial(mediaType MediaType, filename string) (mediaID string, url string, err error) {
|
|
||||||
f, err := os.Open(filename)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer func() { _ = f.Close() }()
|
|
||||||
|
|
||||||
return material.AddMaterialFromReader(mediaType, filename, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
type reqVideo struct {
|
type reqVideo struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Introduction string `json:"introduction"`
|
Introduction string `json:"introduction"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddVideoFromReader 永久视频素材文件上传,从 io.Reader 中读取
|
// AddVideo 永久视频素材文件上传
|
||||||
func (material *Material) AddVideoFromReader(filePath, title, introduction string, reader io.Reader) (mediaID string, url string, err error) {
|
func (material *Material) AddVideo(filename, title, introduction string) (mediaID string, url string, err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
accessToken, err = material.GetAccessToken()
|
accessToken, err = material.GetAccessToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -232,19 +216,16 @@ func (material *Material) AddVideoFromReader(filePath, title, introduction strin
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fileName := path.Base(filePath)
|
|
||||||
fields := []util.MultipartFormField{
|
fields := []util.MultipartFormField{
|
||||||
{
|
{
|
||||||
IsFile: true,
|
IsFile: true,
|
||||||
Fieldname: "media",
|
Fieldname: "media",
|
||||||
FilePath: filePath,
|
Filename: filename,
|
||||||
Filename: fileName,
|
|
||||||
FileReader: reader,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
IsFile: false,
|
IsFile: false,
|
||||||
Fieldname: "description",
|
Fieldname: "description",
|
||||||
Filename: fileName,
|
|
||||||
Value: fieldValue,
|
Value: fieldValue,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -269,17 +250,6 @@ func (material *Material) AddVideoFromReader(filePath, title, introduction strin
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddVideo 永久视频素材文件上传
|
|
||||||
func (material *Material) AddVideo(directory, title, introduction string) (mediaID string, url string, err error) {
|
|
||||||
f, err := os.Open(directory)
|
|
||||||
if err != nil {
|
|
||||||
return "", "", err
|
|
||||||
}
|
|
||||||
defer func() { _ = f.Close() }()
|
|
||||||
|
|
||||||
return material.AddVideoFromReader(directory, title, introduction, f)
|
|
||||||
}
|
|
||||||
|
|
||||||
type reqDeleteMaterial struct {
|
type reqDeleteMaterial struct {
|
||||||
MediaID string `json:"media_id"`
|
MediaID string `json:"media_id"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package material
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
@@ -63,38 +62,6 @@ func (material *Material) MediaUpload(mediaType MediaType, filename string) (med
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// MediaUploadFromReader 临时素材上传
|
|
||||||
func (material *Material) MediaUploadFromReader(mediaType MediaType, filename string, reader io.Reader) (media Media, err error) {
|
|
||||||
var accessToken string
|
|
||||||
accessToken, err = material.GetAccessToken()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
uri := fmt.Sprintf("%s?access_token=%s&type=%s", mediaUploadURL, accessToken, mediaType)
|
|
||||||
|
|
||||||
var byteData []byte
|
|
||||||
byteData, err = io.ReadAll(reader)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var response []byte
|
|
||||||
response, err = util.PostFileByStream("media", filename, uri, byteData)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(response, &media)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if media.ErrCode != 0 {
|
|
||||||
err = fmt.Errorf("MediaUpload error : errcode=%v , errmsg=%v", media.ErrCode, media.ErrMsg)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetMediaURL 返回临时素材的下载地址供用户自己处理
|
// GetMediaURL 返回临时素材的下载地址供用户自己处理
|
||||||
// NOTICE: URL 不可公开,因为含access_token 需要立即另存文件
|
// NOTICE: URL 不可公开,因为含access_token 需要立即另存文件
|
||||||
func (material *Material) GetMediaURL(mediaID string) (mediaURL string, err error) {
|
func (material *Material) GetMediaURL(mediaID string) (mediaURL string, err error) {
|
||||||
|
|||||||
@@ -61,15 +61,15 @@ func (tpl *Template) Send(msg *TemplateMessage) (msgID int64, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var (
|
uri := fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken)
|
||||||
uri = fmt.Sprintf("%s?access_token=%s", templateSendURL, accessToken)
|
var response []byte
|
||||||
response []byte
|
response, err = util.PostJSON(uri, msg)
|
||||||
)
|
if err != nil {
|
||||||
if response, err = util.PostJSON(uri, msg); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var result resTemplateSend
|
var result resTemplateSend
|
||||||
if err = json.Unmarshal(response, &result); err != nil {
|
err = json.Unmarshal(response, &result)
|
||||||
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if result.ErrCode != 0 {
|
if result.ErrCode != 0 {
|
||||||
@@ -103,11 +103,10 @@ func (tpl *Template) List() (templateList []*TemplateItem, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var (
|
uri := fmt.Sprintf("%s?access_token=%s", templateListURL, accessToken)
|
||||||
uri = fmt.Sprintf("%s?access_token=%s", templateListURL, accessToken)
|
var response []byte
|
||||||
response []byte
|
response, err = util.HTTPGet(uri)
|
||||||
)
|
if err != nil {
|
||||||
if response, err = util.HTTPGet(uri); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var res resTemplateList
|
var res resTemplateList
|
||||||
@@ -122,23 +121,22 @@ type resTemplateAdd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add 添加模板.
|
// Add 添加模板.
|
||||||
func (tpl *Template) Add(shortID string, keyNameList []string) (templateID string, err error) {
|
func (tpl *Template) Add(shortID string) (templateID string, err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
accessToken, err = tpl.GetAccessToken()
|
accessToken, err = tpl.GetAccessToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var (
|
var msg = struct {
|
||||||
msg = struct {
|
ShortID string `json:"template_id_short"`
|
||||||
ShortID string `json:"template_id_short"`
|
}{ShortID: shortID}
|
||||||
KeyNameList []string `json:"keyword_name_list"`
|
uri := fmt.Sprintf("%s?access_token=%s", templateAddURL, accessToken)
|
||||||
}{ShortID: shortID, KeyNameList: keyNameList}
|
var response []byte
|
||||||
uri = fmt.Sprintf("%s?access_token=%s", templateAddURL, accessToken)
|
response, err = util.PostJSON(uri, msg)
|
||||||
response []byte
|
if err != nil {
|
||||||
)
|
|
||||||
if response, err = util.PostJSON(uri, msg); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var result resTemplateAdd
|
var result resTemplateAdd
|
||||||
err = util.DecodeWithError(response, &result, "AddTemplate")
|
err = util.DecodeWithError(response, &result, "AddTemplate")
|
||||||
return result.TemplateID, err
|
return result.TemplateID, err
|
||||||
@@ -151,14 +149,14 @@ func (tpl *Template) Delete(templateID string) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var (
|
var msg = struct {
|
||||||
msg = struct {
|
TemplateID string `json:"template_id"`
|
||||||
TemplateID string `json:"template_id"`
|
}{TemplateID: templateID}
|
||||||
}{TemplateID: templateID}
|
|
||||||
uri = fmt.Sprintf("%s?access_token=%s", templateDelURL, accessToken)
|
uri := fmt.Sprintf("%s?access_token=%s", templateDelURL, accessToken)
|
||||||
response []byte
|
var response []byte
|
||||||
)
|
response, err = util.PostJSON(uri, msg)
|
||||||
if response, err = util.PostJSON(uri, msg); err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return util.DecodeWithCommonError(response, "DeleteTemplate")
|
return util.DecodeWithCommonError(response, "DeleteTemplate")
|
||||||
|
|||||||
60
openplatform/miniprogram/auth/auth.go
Normal file
60
openplatform/miniprogram/auth/auth.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
stdContext "context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/silenceper/wechat/v2/openplatform/context"
|
||||||
|
"github.com/silenceper/wechat/v2/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
code2SessionURL = "https://api.weixin.qq.com/sns/component/jscode2session?appid=%s&js_code=%s&grant_type=authorization_code&component_appid=%s&component_access_token=%s"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Auth 登录/用户信息
|
||||||
|
type Auth struct {
|
||||||
|
*context.Context
|
||||||
|
authorizerAppID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAuth new auth (授权方appID)
|
||||||
|
func NewAuth(ctx *context.Context, appID string) *Auth {
|
||||||
|
return &Auth{ctx, appID}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ResCode2Session 登录凭证校验的返回结果
|
||||||
|
type ResCode2Session struct {
|
||||||
|
util.CommonError
|
||||||
|
OpenID string `json:"openid"` // 用户唯一标识
|
||||||
|
SessionKey string `json:"session_key"` // 会话密钥
|
||||||
|
UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符,在满足UnionID下发条件的情况下会返回
|
||||||
|
}
|
||||||
|
|
||||||
|
// Code2Session 登录凭证校验。
|
||||||
|
func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error) {
|
||||||
|
return auth.Code2SessionContext(stdContext.Background(), jsCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Code2SessionContext 登录凭证校验。
|
||||||
|
func (auth *Auth) Code2SessionContext(ctx stdContext.Context, jsCode string) (result ResCode2Session, err error) {
|
||||||
|
var response []byte
|
||||||
|
var componentAccessToken string
|
||||||
|
componentAccessToken, err = auth.GetComponentAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
parse := fmt.Sprintf(code2SessionURL, auth.authorizerAppID, jsCode, auth.Context.AppID, componentAccessToken)
|
||||||
|
if response, err = util.HTTPGetContext(ctx, parse); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal(response, &result); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("Code2Session error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
miniContext "github.com/silenceper/wechat/v2/miniprogram/context"
|
miniContext "github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/urllink"
|
"github.com/silenceper/wechat/v2/miniprogram/urllink"
|
||||||
openContext "github.com/silenceper/wechat/v2/openplatform/context"
|
openContext "github.com/silenceper/wechat/v2/openplatform/context"
|
||||||
|
"github.com/silenceper/wechat/v2/openplatform/miniprogram/auth"
|
||||||
"github.com/silenceper/wechat/v2/openplatform/miniprogram/basic"
|
"github.com/silenceper/wechat/v2/openplatform/miniprogram/basic"
|
||||||
"github.com/silenceper/wechat/v2/openplatform/miniprogram/component"
|
"github.com/silenceper/wechat/v2/openplatform/miniprogram/component"
|
||||||
)
|
)
|
||||||
@@ -72,6 +73,11 @@ func (miniProgram *MiniProgram) GetURLLink() *urllink.URLLink {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAuth 登录/用户信息相关接口
|
||||||
|
func (miniProgram *MiniProgram) GetAuth() *auth.Auth {
|
||||||
|
return auth.NewAuth(miniProgram.openContext, miniProgram.AppID)
|
||||||
|
}
|
||||||
|
|
||||||
// DefaultAuthrAccessToken 默认获取授权ak的方法
|
// DefaultAuthrAccessToken 默认获取授权ak的方法
|
||||||
type DefaultAuthrAccessToken struct {
|
type DefaultAuthrAccessToken struct {
|
||||||
opCtx *openContext.Context
|
opCtx *openContext.Context
|
||||||
|
|||||||
69
util/http.go
69
util/http.go
@@ -146,40 +146,13 @@ func PostJSONWithRespContentType(uri string, obj interface{}) ([]byte, string, e
|
|||||||
return responseData, contentType, err
|
return responseData, contentType, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostFileByStream 上传文件
|
|
||||||
func PostFileByStream(fieldName, fileName, uri string, byteData []byte) ([]byte, error) {
|
|
||||||
fields := []MultipartFormField{
|
|
||||||
{
|
|
||||||
IsFile: false,
|
|
||||||
Fieldname: fieldName,
|
|
||||||
Filename: fileName,
|
|
||||||
Value: byteData,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return PostMultipartForm(fields, uri)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PostFile 上传文件
|
// PostFile 上传文件
|
||||||
func PostFile(fieldName, filePath, uri string) ([]byte, error) {
|
func PostFile(fieldName, filename, uri string) ([]byte, error) {
|
||||||
fields := []MultipartFormField{
|
fields := []MultipartFormField{
|
||||||
{
|
{
|
||||||
IsFile: true,
|
IsFile: true,
|
||||||
Fieldname: fieldName,
|
Fieldname: fieldName,
|
||||||
FilePath: filePath,
|
Filename: filename,
|
||||||
},
|
|
||||||
}
|
|
||||||
return PostMultipartForm(fields, uri)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PostFileFromReader 上传文件,从 io.Reader 中读取
|
|
||||||
func PostFileFromReader(filedName, filePath, fileName, uri string, reader io.Reader) ([]byte, error) {
|
|
||||||
fields := []MultipartFormField{
|
|
||||||
{
|
|
||||||
IsFile: true,
|
|
||||||
Fieldname: filedName,
|
|
||||||
FilePath: filePath,
|
|
||||||
Filename: fileName,
|
|
||||||
FileReader: reader,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return PostMultipartForm(fields, uri)
|
return PostMultipartForm(fields, uri)
|
||||||
@@ -187,12 +160,10 @@ func PostFileFromReader(filedName, filePath, fileName, uri string, reader io.Rea
|
|||||||
|
|
||||||
// MultipartFormField 保存文件或其他字段信息
|
// MultipartFormField 保存文件或其他字段信息
|
||||||
type MultipartFormField struct {
|
type MultipartFormField struct {
|
||||||
IsFile bool
|
IsFile bool
|
||||||
Fieldname string
|
Fieldname string
|
||||||
Value []byte
|
Value []byte
|
||||||
FilePath string
|
Filename string
|
||||||
Filename string
|
|
||||||
FileReader io.Reader
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostMultipartForm 上传文件或其他多个字段
|
// PostMultipartForm 上传文件或其他多个字段
|
||||||
@@ -211,24 +182,18 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if field.FileReader == nil {
|
fh, e := os.Open(field.Filename)
|
||||||
fh, e := os.Open(field.FilePath)
|
if e != nil {
|
||||||
if e != nil {
|
err = fmt.Errorf("error opening file , err=%v", e)
|
||||||
err = fmt.Errorf("error opening file , err=%v", e)
|
return
|
||||||
return
|
}
|
||||||
}
|
defer fh.Close()
|
||||||
_, err = io.Copy(fileWriter, fh)
|
|
||||||
_ = fh.Close()
|
if _, err = io.Copy(fileWriter, fh); err != nil {
|
||||||
if err != nil {
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if _, err = io.Copy(fileWriter, field.FileReader); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
partWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename)
|
partWriter, e := bodyWriter.CreateFormField(field.Fieldname)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
err = e
|
err = e
|
||||||
return
|
return
|
||||||
@@ -250,7 +215,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("http code error : uri=%v , statusCode=%v", uri, resp.StatusCode)
|
return nil, err
|
||||||
}
|
}
|
||||||
respBody, err = io.ReadAll(resp.Body)
|
respBody, err = io.ReadAll(resp.Body)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package material
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/util"
|
"github.com/silenceper/wechat/v2/util"
|
||||||
)
|
)
|
||||||
@@ -97,54 +96,3 @@ func (r *Client) UploadAttachment(filename string, mediaType string, attachmentT
|
|||||||
err = util.DecodeWithError(response, result, "UploadAttachment")
|
err = util.DecodeWithError(response, result, "UploadAttachment")
|
||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UploadTempFileFromReader 上传临时素材
|
|
||||||
// @see https://developer.work.weixin.qq.com/document/path/90253
|
|
||||||
// @mediaType 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file)
|
|
||||||
func (r *Client) UploadTempFileFromReader(filename, mediaType string, reader io.Reader) (*UploadTempFileResponse, error) {
|
|
||||||
var (
|
|
||||||
accessToken string
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var byteData []byte
|
|
||||||
byteData, err = io.ReadAll(reader)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var response []byte
|
|
||||||
if response, err = util.PostFileByStream("media", filename, fmt.Sprintf(uploadTempFile, accessToken, mediaType), byteData); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
result := &UploadTempFileResponse{}
|
|
||||||
err = util.DecodeWithError(response, result, "UploadTempFile")
|
|
||||||
return result, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// UploadAttachmentFromReader 上传附件资源
|
|
||||||
// @see https://developer.work.weixin.qq.com/document/path/95098
|
|
||||||
// @mediaType 媒体文件类型,分别有图片(image)、视频(video)、普通文件(file)
|
|
||||||
// @attachment_type 附件类型,不同的附件类型用于不同的场景。1:朋友圈;2:商品图册
|
|
||||||
func (r *Client) UploadAttachmentFromReader(filename, mediaType string, reader io.Reader, attachmentType int) (*UploadAttachmentResponse, error) {
|
|
||||||
var (
|
|
||||||
accessToken string
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
if accessToken, err = r.GetAccessToken(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var byteData []byte
|
|
||||||
byteData, err = io.ReadAll(reader)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var response []byte
|
|
||||||
if response, err = util.PostFileByStream("media", filename, fmt.Sprintf(uploadAttachment, accessToken, mediaType, attachmentType), byteData); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
result := &UploadAttachmentResponse{}
|
|
||||||
err = util.DecodeWithError(response, result, "UploadAttachment")
|
|
||||||
return result, err
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user