forked from omigo/weixin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userinfo.go
107 lines (94 loc) · 3.25 KB
/
userinfo.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
package weixin
import (
"bytes"
"errors"
"fmt"
"text/template"
)
// 用户基本信息
const (
UserInfoUpdateRemarkURL = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=%s"
UserInfoGetUserInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=%s"
UserInfoBatchGetUserInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=%s"
UserInfoGetUserListURL = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s"
)
// UpdateUserRemark 设置用户备注名
func UpdateUserRemark(openId, remark string) (err error) {
url := fmt.Sprintf(UserInfoUpdateRemarkURL, AccessToken())
body := fmt.Sprintf(`{"openid":"%s","remark":"%s"}`, openId, remark)
return Post(url, []byte(body), nil)
}
// Lang 国家地区语言版本
type Lang string
// 微信支持的语言
const (
LangZHCN = "zh_CN" // 简体
LangZHTW = "zh_TW" // 繁体
LangEN = "en" // 英语
)
// UserInfo 用户基本信息
type UserInfo struct {
WXError
Subscribe int `json:"subscribe"`
OpenId string `json:"openid"`
NickName string `json:"nickname"`
Sex int `json:"sex"`
Language Lang `json:"language"`
City string `json:"city"`
Province string `json:"province"`
Country string `json:"country"`
HeadImgURL string `json:"headimgurl"`
SubscribeTime int `json:"subscribe_time"`
UnionId string `json:"unionid"`
Remark string `json:"remark"`
GroupId int `json:"groupid"`
}
// GetUserInfo 获取用户基本信息(包括UnionID机制)
func GetUserInfo(openId string, lang ...Lang) (info *UserInfo, err error) {
if len(lang) == 0 {
lang = []Lang{LangZHCN}
}
url := fmt.Sprintf(UserInfoGetUserInfoURL, AccessToken(), openId, lang[0])
info = &UserInfo{}
err = Get(url, info)
return info, err
}
// BatchGetUserInfo 获取用户基本信息(包括UnionID机制)
func BatchGetUserInfo(openIds []string, lang ...Lang) (infos []UserInfo, err error) {
if len(openIds) == 0 {
return nil, errors.New("openIds is blank")
}
if len(lang) == 0 {
lang = []Lang{LangZHCN}
}
url := fmt.Sprintf(UserInfoBatchGetUserInfoURL, AccessToken())
tlp := `{"user_list":[{{range $index, $elmt := .}}{{if $index}},{{end}}{"openid":"{{.}}","lang":"` + string(lang[0]) + `"}{{end}}]}`
t := template.New("user_info_list_request")
t, _ = t.Parse(tlp)
var buf bytes.Buffer
t.Execute(&buf, openIds)
wrapper := &struct {
WXError
UserInfoList []UserInfo `json:"user_info_list"`
}{}
err = Post(url, buf.Bytes(), wrapper)
return wrapper.UserInfoList, err
}
// GetUserList 获取用户列表。第一个拉取的OPENID,nextOpenId 不填默认从头开始拉取
func GetUserList(fromOpenId ...string) (openIds []string, total, count int, nextOpenId string, err error) {
if len(fromOpenId) == 0 {
fromOpenId = []string{""}
}
url := fmt.Sprintf(UserInfoGetUserListURL, AccessToken(), fromOpenId[0])
wrapper := &struct {
WXError
Total int `json:"total"`
Count int `json:"count"`
NextOpenId string `json:"next_openid"`
Data struct {
OpenIds []string `json:"openid"`
} `json:"data"`
}{}
err = Get(url, wrapper)
return wrapper.Data.OpenIds, wrapper.Total, wrapper.Count, wrapper.NextOpenId, err
}