forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
head_img.go
47 lines (41 loc) · 1.23 KB
/
head_img.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
package account
import (
"io"
"os"
"path/filepath"
"github.com/bububa/wechat/mp/core"
)
// UploadHeadImage 上传客服头像.
func UploadHeadImage(clt *core.Client, kfAccount, imageFilePath string) (err error) {
file, err := os.Open(imageFilePath)
if err != nil {
return
}
defer file.Close()
return UploadHeadImageFromReader(clt, kfAccount, filepath.Base(imageFilePath), file)
}
// UploadHeadImageFromReader 上传客服头像.
//
// NOTE: 参数 filename 不是文件路径, 是 multipart/form-data 里面 filename 的值.
func UploadHeadImageFromReader(clt *core.Client, kfAccount, filename string, reader io.Reader) (err error) {
// TODO
// incompleteURL := "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account=" +
// url.QueryEscape(kfAccount) + "&access_token="
incompleteURL := "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?kf_account=" +
kfAccount + "&access_token="
var fields = []core.MultipartFormField{{
IsFile: true,
Name: "media",
FileName: filename,
Value: reader,
}}
var result core.Error
if err = clt.PostMultipartForm(incompleteURL, fields, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result
return
}
return
}