-
Notifications
You must be signed in to change notification settings - Fork 0
/
headimagemodify.go
117 lines (103 loc) · 3.29 KB
/
headimagemodify.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
/**
* Created by GoLand.
* User: 姜伟
* Date: 2020/1/10 0010
* Time: 15:45
*/
package account
import (
"github.com/a07061625/gompf/mpf"
"github.com/a07061625/gompf/mpf/api"
"github.com/a07061625/gompf/mpf/api/wx"
"github.com/a07061625/gompf/mpf/mpconstant/errorcode"
"github.com/a07061625/gompf/mpf/mpconstant/project"
"github.com/a07061625/gompf/mpf/mperr"
"github.com/valyala/fasthttp"
)
// 修改头像
type headImageModify struct {
wx.BaseWxOpen
appId string // 应用ID
headImgMediaId string // 头像素材
x1 float32 // 起始点横坐标
y1 float32 // 起始点纵坐标
x2 float32 // 截止点横坐标
y2 float32 // 截止点纵坐标
}
func (him *headImageModify) SetHeadImage(headImage string) {
if len(headImage) > 0 {
him.headImgMediaId = headImage
} else {
panic(mperr.NewWxOpenMini(errorcode.WxOpenParam, "头像素材不合法", nil))
}
}
func (him *headImageModify) SetX1(x1 float32) {
if (x1 >= 0) && (x1 <= 1) {
him.x1 = x1
} else {
panic(mperr.NewWxOpenMini(errorcode.WxOpenParam, "起始点横坐标不合法", nil))
}
}
func (him *headImageModify) SetY1(y1 float32) {
if (y1 >= 0) && (y1 <= 1) {
him.y1 = y1
} else {
panic(mperr.NewWxOpenMini(errorcode.WxOpenParam, "起始点纵坐标不合法", nil))
}
}
func (him *headImageModify) SetX2(x2 float32) {
if (x2 >= 0) && (x2 <= 1) {
him.x2 = x2
} else {
panic(mperr.NewWxOpenMini(errorcode.WxOpenParam, "截止点横坐标不合法", nil))
}
}
func (him *headImageModify) SetY2(y2 float32) {
if (y2 >= 0) && (y2 <= 1) {
him.y2 = y2
} else {
panic(mperr.NewWxOpenMini(errorcode.WxOpenParam, "截止点纵坐标不合法", nil))
}
}
func (him *headImageModify) checkData() {
if len(him.headImgMediaId) == 0 {
panic(mperr.NewWxOpenMini(errorcode.WxOpenParam, "头像素材不能为空", nil))
}
}
func (him *headImageModify) SendRequest() api.APIResult {
him.checkData()
reqData := make(map[string]interface{})
reqData["head_img_media_id"] = him.headImgMediaId
reqData["x1"] = him.x1
reqData["y1"] = him.y1
reqData["x2"] = him.x2
reqData["y2"] = him.y2
reqBody := mpf.JSONMarshal(reqData)
him.ReqURI = "https://api.weixin.qq.com/cgi-bin/account/modifyheadimage?access_token=" + wx.NewUtilWx().GetOpenAuthorizeAccessToken(him.appId)
client, req := him.GetRequest()
req.SetBody([]byte(reqBody))
resp, result := him.SendInner(client, req, errorcode.WxOpenRequestPost)
if resp.RespCode > 0 {
return result
}
respData, _ := mpf.JSONUnmarshalMap(resp.Content)
errCode, ok := respData["errcode"]
if ok && (errCode.(int) == 0) {
result.Data = respData
} else {
result.Code = errorcode.WxOpenRequestPost
result.Msg = respData["errmsg"].(string)
}
return result
}
func NewHeadImageModify(appId string) *headImageModify {
him := &headImageModify{wx.NewBaseWxOpen(), "", "", 0.00, 0.00, 0.00, 0.00}
him.appId = appId
him.x1 = 0.00
him.y1 = 0.00
him.x2 = 1.00
him.y2 = 1.00
him.ReqContentType = project.HTTPContentTypeJSON
him.ReqMethod = fasthttp.MethodPost
return him
}