-
Notifications
You must be signed in to change notification settings - Fork 173
/
client.go
130 lines (106 loc) · 3.41 KB
/
client.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
package media
import (
"github.com/ArtisanCloud/PowerLibs/v2/http/contract"
"github.com/ArtisanCloud/PowerLibs/v2/object"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel/power"
response2 "github.com/ArtisanCloud/PowerWeChat/v2/src/work/media/response"
)
type Client struct {
*kernel.BaseClient
}
func NewClient(app kernel.ApplicationInterface) (*Client, error) {
baseClient, err := kernel.NewBaseClient(&app, nil)
if err != nil {
return nil, err
}
return &Client{
baseClient,
}, nil
}
// 获取临时素材
// https://developer.work.weixin.qq.com/document/path/90254
func (comp *Client) Get(mediaID string) (contract.ResponseInterface, error) {
result := ""
header := &response2.ResponseHeaderMedia{}
response, err := comp.RequestRaw("cgi-bin/media/get", "GET", &object.HashMap{
"query": &object.StringMap{
"media_id": mediaID,
},
}, header, &result)
return response.(contract.ResponseInterface), err
}
// 获取高清语音素材
// https://developer.work.weixin.qq.com/document/path/90255
func (comp *Client) GetJSSDK(mediaID string) (contract.ResponseInterface, error) {
result := ""
header := &response2.ResponseHeaderMedia{}
response, err := comp.RequestRaw("cgi-bin/media/get/jssdk", "GET", &object.HashMap{
"query": &object.StringMap{
"media_id": mediaID,
},
}, header, &result)
return response.(contract.ResponseInterface), err
}
// 上传图片
// https://developer.work.weixin.qq.com/document/path/90256
func (comp *Client) UploadImage(path string, form *power.HashMap) (*response2.ResponseUploadImage, error) {
result := &response2.ResponseUploadImage{}
var files *object.HashMap
if path != "" {
files = &object.HashMap{
"media": path,
}
}
var formData *kernel.UploadForm
if form != nil {
formData = &kernel.UploadForm{
Contents: []*kernel.UploadContent{
&kernel.UploadContent{
Name: (*form)["name"].(string),
Value: (*form)["value"],
},
},
}
}
_, err := comp.HttpUpload("cgi-bin/media/uploadimg", files, formData, nil, nil, &result)
return result, err
}
func (comp *Client) UploadTempImage(path string, form *power.HashMap) (*response2.ResponseUploadMedia, error) {
return comp.Upload("image", path, form)
}
func (comp *Client) UploadTempVoice(path string, form *power.HashMap) (*response2.ResponseUploadMedia, error) {
return comp.Upload("voice", path, form)
}
func (comp *Client) UploadTempVideo(path string, form *power.HashMap) (*response2.ResponseUploadMedia, error) {
return comp.Upload("video", path, form)
}
func (comp *Client) UploadTempFile(path string, form *power.HashMap) (*response2.ResponseUploadMedia, error) {
return comp.Upload("file", path, form)
}
// 上传临时素材
// https://developer.work.weixin.qq.com/document/path/90253
func (comp *Client) Upload(mediaType string, path string, form *power.HashMap) (*response2.ResponseUploadMedia, error) {
outResponse := &response2.ResponseUploadMedia{}
var files *object.HashMap
if path != "" {
files = &object.HashMap{
"media": path,
}
}
var formData *kernel.UploadForm
if form != nil {
formData = &kernel.UploadForm{
Contents: []*kernel.UploadContent{
&kernel.UploadContent{
Name: (*form)["name"].(string),
Value: (*form)["value"],
},
},
}
}
_, err := comp.HttpUpload("cgi-bin/media/upload", files, formData, &object.StringMap{
"type": mediaType,
}, nil, outResponse)
return outResponse, err
}