mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-17 03:02:26 +08:00
Compare commits
4 Commits
v2.1.7
...
8e84f63cec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e84f63cec | ||
|
|
d3d35387b7 | ||
|
|
0bca4d5792 | ||
|
|
bd0dce6f47 |
96
cache/redis.go
vendored
96
cache/redis.go
vendored
@@ -2,9 +2,13 @@ package cache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Redis .redis cache
|
// Redis .redis cache
|
||||||
@@ -22,6 +26,7 @@ type RedisOpts struct {
|
|||||||
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
||||||
MaxActive int `yml:"max_active" json:"max_active"`
|
MaxActive int `yml:"max_active" json:"max_active"`
|
||||||
IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` // second
|
IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` // second
|
||||||
|
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRedis 实例化
|
// NewRedis 实例化
|
||||||
@@ -33,6 +38,20 @@ func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
|
|||||||
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,
|
||||||
|
Dialer: opts.Dialer,
|
||||||
|
})
|
||||||
|
return &Redis{ctx: ctx, conn: conn}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRedisOverSSH 实例化(通过 SSH 代理连接 Redis )
|
||||||
|
func NewRedisOverSSH(ctx context.Context, opts *RedisOpts, overSSH *OverSSH) *Redis {
|
||||||
|
conn := redis.NewUniversalClient(&redis.UniversalOptions{
|
||||||
|
Addrs: []string{opts.Host},
|
||||||
|
DB: opts.Database,
|
||||||
|
Password: opts.Password,
|
||||||
|
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
||||||
|
MinIdleConns: opts.MaxIdle,
|
||||||
|
Dialer: overSSH.MakeDialer(),
|
||||||
})
|
})
|
||||||
return &Redis{ctx: ctx, conn: conn}
|
return &Redis{ctx: ctx, conn: conn}
|
||||||
}
|
}
|
||||||
@@ -92,3 +111,80 @@ func (r *Redis) Delete(key string) error {
|
|||||||
func (r *Redis) DeleteContext(ctx context.Context, key string) error {
|
func (r *Redis) DeleteContext(ctx context.Context, key string) error {
|
||||||
return r.conn.Del(ctx, key).Err()
|
return r.conn.Del(ctx, key).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SSHAuthMethod SSH认证方式
|
||||||
|
type SSHAuthMethod uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
// PubKeyAuth SSH公钥方式认证
|
||||||
|
PubKeyAuth SSHAuthMethod = 1
|
||||||
|
// PwdAuth SSH密码方式认证
|
||||||
|
PwdAuth SSHAuthMethod = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
// OverSSH SSH 代理配置
|
||||||
|
type OverSSH struct {
|
||||||
|
Host string `yml:"host" json:"host"`
|
||||||
|
Port int `yml:"port" json:"port"`
|
||||||
|
AuthMethod SSHAuthMethod `yml:"auth_method" json:"auth_method"`
|
||||||
|
Username string `yml:"username" json:"username"`
|
||||||
|
Password string `yml:"password" json:"password"`
|
||||||
|
KeyFile string `yml:"key_file" json:"key_file"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialWithPassword 返回密码方式认证的 SSH 客户端
|
||||||
|
func (s *OverSSH) DialWithPassword() (*ssh.Client, error) {
|
||||||
|
return ssh.Dial(
|
||||||
|
"tcp",
|
||||||
|
fmt.Sprintf("%s:%d", s.Host, s.Port),
|
||||||
|
&ssh.ClientConfig{
|
||||||
|
User: s.Username,
|
||||||
|
Auth: []ssh.AuthMethod{
|
||||||
|
ssh.Password(s.Password),
|
||||||
|
},
|
||||||
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialWithKeyFile 返回公钥方式认证的 SSH 客户端
|
||||||
|
func (s *OverSSH) DialWithKeyFile() (*ssh.Client, error) {
|
||||||
|
k, err := os.ReadFile(s.KeyFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
signer, err := ssh.ParsePrivateKey(k)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ssh.Dial(
|
||||||
|
"tcp",
|
||||||
|
fmt.Sprintf("%s:%d", s.Host, s.Port),
|
||||||
|
&ssh.ClientConfig{
|
||||||
|
User: s.Username,
|
||||||
|
Auth: []ssh.AuthMethod{
|
||||||
|
ssh.PublicKeys(signer),
|
||||||
|
},
|
||||||
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeDialer 创建 SSH 代理拨号器
|
||||||
|
func (s *OverSSH) MakeDialer() func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
var err error
|
||||||
|
var sshclient *ssh.Client
|
||||||
|
switch s.AuthMethod {
|
||||||
|
case PwdAuth:
|
||||||
|
sshclient, err = s.DialWithPassword()
|
||||||
|
case PubKeyAuth:
|
||||||
|
sshclient, err = s.DialWithKeyFile()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return sshclient.Dial(network, addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
1
go.sum
1
go.sum
@@ -111,6 +111,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
|||||||
@@ -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 请求并返回二维码二进制数据
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"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"
|
||||||
@@ -164,7 +163,7 @@ type resAddMaterial struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddMaterialFromReader 上传永久性素材(处理视频需要单独上传),从 io.Reader 中读取
|
// AddMaterialFromReader 上传永久性素材(处理视频需要单独上传),从 io.Reader 中读取
|
||||||
func (material *Material) AddMaterialFromReader(mediaType MediaType, filePath string, reader io.Reader) (mediaID string, url string, err error) {
|
func (material *Material) AddMaterialFromReader(mediaType MediaType, filename string, reader io.Reader) (mediaID string, url string, err error) {
|
||||||
if mediaType == MediaTypeVideo {
|
if mediaType == MediaTypeVideo {
|
||||||
err = errors.New("永久视频素材上传使用 AddVideo 方法")
|
err = errors.New("永久视频素材上传使用 AddVideo 方法")
|
||||||
return
|
return
|
||||||
@@ -176,10 +175,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.PostFileFromReader("media", filename, uri, reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -214,7 +211,7 @@ type reqVideo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddVideoFromReader 永久视频素材文件上传,从 io.Reader 中读取
|
// AddVideoFromReader 永久视频素材文件上传,从 io.Reader 中读取
|
||||||
func (material *Material) AddVideoFromReader(filePath, title, introduction string, reader io.Reader) (mediaID string, url string, err error) {
|
func (material *Material) AddVideoFromReader(filename, title, introduction string, reader io.Reader) (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 +229,17 @@ 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,
|
FileReader: reader,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
IsFile: false,
|
IsFile: false,
|
||||||
Fieldname: "description",
|
Fieldname: "description",
|
||||||
Filename: fileName,
|
|
||||||
Value: fieldValue,
|
Value: fieldValue,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -270,14 +265,14 @@ func (material *Material) AddVideoFromReader(filePath, title, introduction strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddVideo 永久视频素材文件上传
|
// AddVideo 永久视频素材文件上传
|
||||||
func (material *Material) AddVideo(directory, title, introduction string) (mediaID string, url string, err error) {
|
func (material *Material) AddVideo(filename, title, introduction string) (mediaID string, url string, err error) {
|
||||||
f, err := os.Open(directory)
|
f, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
defer func() { _ = f.Close() }()
|
defer func() { _ = f.Close() }()
|
||||||
|
|
||||||
return material.AddVideoFromReader(directory, title, introduction, f)
|
return material.AddVideoFromReader(filename, title, introduction, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
type reqDeleteMaterial struct {
|
type reqDeleteMaterial struct {
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
25
util/http.go
25
util/http.go
@@ -146,38 +146,24 @@ 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)
|
return PostMultipartForm(fields, uri)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostFileFromReader 上传文件,从 io.Reader 中读取
|
// PostFileFromReader 上传文件,从 io.Reader 中读取
|
||||||
func PostFileFromReader(filedName, filePath, fileName, uri string, reader io.Reader) ([]byte, error) {
|
func PostFileFromReader(filedName, fileName, uri string, reader io.Reader) ([]byte, error) {
|
||||||
fields := []MultipartFormField{
|
fields := []MultipartFormField{
|
||||||
{
|
{
|
||||||
IsFile: true,
|
IsFile: true,
|
||||||
Fieldname: filedName,
|
Fieldname: filedName,
|
||||||
FilePath: filePath,
|
|
||||||
Filename: fileName,
|
Filename: fileName,
|
||||||
FileReader: reader,
|
FileReader: reader,
|
||||||
},
|
},
|
||||||
@@ -190,7 +176,6 @@ type MultipartFormField struct {
|
|||||||
IsFile bool
|
IsFile bool
|
||||||
Fieldname string
|
Fieldname string
|
||||||
Value []byte
|
Value []byte
|
||||||
FilePath string
|
|
||||||
Filename string
|
Filename string
|
||||||
FileReader io.Reader
|
FileReader io.Reader
|
||||||
}
|
}
|
||||||
@@ -212,7 +197,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
|
|||||||
}
|
}
|
||||||
|
|
||||||
if field.FileReader == nil {
|
if field.FileReader == nil {
|
||||||
fh, e := os.Open(field.FilePath)
|
fh, e := os.Open(field.Filename)
|
||||||
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
|
||||||
@@ -228,7 +213,7 @@ func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} 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
|
||||||
|
|||||||
@@ -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