/
infobatchget.go
85 lines (74 loc) · 2.44 KB
/
infobatchget.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
/**
* Created by GoLand.
* User: 姜伟
* Date: 2020/1/12 0012
* Time: 23:44
*/
package user
import (
"regexp"
"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 infoBatchGet struct {
wx.BaseWxAccount
appId string
openidList []string // 用户openid列表
}
func (ibg *infoBatchGet) SetOpenidList(openidList []string) {
ibg.openidList = make([]string, 0)
for _, v := range openidList {
match, _ := regexp.MatchString(project.RegexWxOpenid, v)
if match {
ibg.openidList = append(ibg.openidList, v)
}
}
}
func (ibg *infoBatchGet) checkData() {
if len(ibg.openidList) == 0 {
panic(mperr.NewWxAccount(errorcode.WxAccountParam, "用户openid列表不能为空", nil))
} else if len(ibg.openidList) > 100 {
panic(mperr.NewWxAccount(errorcode.WxAccountParam, "用户openid列表不能超过100个", nil))
}
}
func (ibg *infoBatchGet) SendRequest() api.APIResult {
ibg.checkData()
userList := make([]map[string]string, 0)
for _, v := range ibg.openidList {
userInfo := make(map[string]string)
userInfo["openid"] = v
userInfo["lang"] = "zh-CN"
userList = append(userList, userInfo)
}
reqData := make(map[string]interface{})
reqData["user_list"] = userList
reqBody := mpf.JSONMarshal(reqData)
ibg.ReqURI = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=" + wx.NewUtilWx().GetSingleAccessToken(ibg.appId)
client, req := ibg.GetRequest()
req.SetBody([]byte(reqBody))
resp, result := ibg.SendInner(client, req, errorcode.WxAccountRequestPost)
if resp.RespCode > 0 {
return result
}
respData, _ := mpf.JSONUnmarshalMap(resp.Content)
_, ok := respData["user_info_list"]
if ok {
result.Data = respData
} else {
result.Code = errorcode.WxAccountRequestPost
result.Msg = respData["errmsg"].(string)
}
return result
}
func NewInfoBatchGet(appId string) *infoBatchGet {
ibg := &infoBatchGet{wx.NewBaseWxAccount(), "", make([]string, 0)}
ibg.appId = appId
ibg.ReqContentType = project.HTTPContentTypeJSON
ibg.ReqMethod = fasthttp.MethodPost
return ibg
}