forked from silenceper/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
134 lines (121 loc) · 4.04 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package tcb
import (
"fmt"
"github.com/antsbean/wechat/util"
)
const (
//获取文件上传链接
uploadFilePathURL = "https://api.weixin.qq.com/tcb/uploadfile"
//获取文件下载链接
batchDownloadFileURL = "https://api.weixin.qq.com/tcb/batchdownloadfile"
//删除文件链接
batchDeleteFileURL = "https://api.weixin.qq.com/tcb/batchdeletefile"
)
//UploadFileReq 上传文件请求值
type UploadFileReq struct {
Env string `json:"env,omitempty"`
Path string `json:"path,omitempty"`
}
//UploadFileRes 上传文件返回结果
type UploadFileRes struct {
util.CommonError
URL string `json:"url"` //上传url
Token string `json:"token"` //token
Authorization string `json:"authorization"` //authorization
FileID string `json:"file_id"` //文件ID
CosFileID string `json:"cos_file_id"` //cos文件ID
}
//BatchDownloadFileReq 上传文件请求值
type BatchDownloadFileReq struct {
Env string `json:"env,omitempty"`
FileList []*DownloadFile `json:"file_list,omitempty"`
}
//DownloadFile 文件信息
type DownloadFile struct {
FileID string `json:"fileid"` //文件ID
MaxAge int64 `json:"max_age"` //下载链接有效期
}
//BatchDownloadFileRes 上传文件返回结果
type BatchDownloadFileRes struct {
util.CommonError
FileList []struct {
FileID string `json:"file_id"` //文件ID
DownloadURL string `json:"download_url"` //下载链接
Status int64 `json:"status"` //状态码
ErrMsg string `json:"errmsg"` //该文件错误信息
} `json:"file_list"`
}
//BatchDeleteFileReq 批量删除文件请求参数
type BatchDeleteFileReq struct {
Env string `json:"env,omitempty"`
FileIDList []string `json:"fileid_list,omitempty"`
}
//BatchDeleteFileRes 批量删除文件返回结果
type BatchDeleteFileRes struct {
util.CommonError
DeleteList []struct {
FileID string `json:"fileid"`
Status int64 `json:"status"`
ErrMsg string `json:"errmsg"`
} `json:"delete_list"`
}
//UploadFile 上传文件
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/uploadFile.html
func (tcb *Tcb) UploadFile(env, path string) (*UploadFileRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", uploadFilePathURL, accessToken)
req := &UploadFileReq{
Env: env,
Path: path,
}
response, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
uploadFileRes := &UploadFileRes{}
err = util.DecodeWithError(response, uploadFileRes, "UploadFile")
return uploadFileRes, err
}
//BatchDownloadFile 获取文件下载链接
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDownloadFile.html
func (tcb *Tcb) BatchDownloadFile(env string, fileList []*DownloadFile) (*BatchDownloadFileRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", batchDownloadFileURL, accessToken)
req := &BatchDownloadFileReq{
Env: env,
FileList: fileList,
}
response, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
batchDownloadFileRes := &BatchDownloadFileRes{}
err = util.DecodeWithError(response, batchDownloadFileRes, "BatchDownloadFile")
return batchDownloadFileRes, err
}
//BatchDeleteFile 批量删除文件
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDeleteFile.html
func (tcb *Tcb) BatchDeleteFile(env string, fileIDList []string) (*BatchDeleteFileRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s", batchDeleteFileURL, accessToken)
req := &BatchDeleteFileReq{
Env: env,
FileIDList: fileIDList,
}
response, err := util.PostJSON(uri, req)
if err != nil {
return nil, err
}
batchDeleteFileRes := &BatchDeleteFileRes{}
err = util.DecodeWithError(response, batchDeleteFileRes, "BatchDeleteFile")
return batchDeleteFileRes, nil
}