forked from silenceper/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
154 lines (135 loc) · 3.63 KB
/
user.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package user
import (
"encoding/json"
"fmt"
"net/url"
"github.com/silenceper/wechat/v2/officialaccount/context"
"github.com/silenceper/wechat/v2/util"
)
const (
userInfoURL = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN"
updateRemarkURL = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=%s"
userListURL = "https://api.weixin.qq.com/cgi-bin/user/get"
)
//User 用户管理
type User struct {
*context.Context
}
//NewUser 实例化
func NewUser(context *context.Context) *User {
user := new(User)
user.Context = context
return user
}
//Info 用户基本信息
type Info struct {
util.CommonError
Subscribe int32 `json:"subscribe"`
OpenID string `json:"openid"`
Nickname string `json:"nickname"`
Sex int32 `json:"sex"`
City string `json:"city"`
Country string `json:"country"`
Province string `json:"province"`
Language string `json:"language"`
Headimgurl string `json:"headimgurl"`
SubscribeTime int32 `json:"subscribe_time"`
UnionID string `json:"unionid"`
Remark string `json:"remark"`
GroupID int32 `json:"groupid"`
TagIDList []int32 `json:"tagid_list"`
SubscribeScene string `json:"subscribe_scene"`
QrScene int `json:"qr_scene"`
QrSceneStr string `json:"qr_scene_str"`
}
// OpenidList 用户列表
type OpenidList struct {
Total int `json:"total"`
Count int `json:"count"`
Data struct {
OpenIDs []string `json:"openid"`
} `json:"data"`
NextOpenID string `json:"next_openid"`
}
//GetUserInfo 获取用户基本信息
func (user *User) GetUserInfo(openID string) (userInfo *Info, err error) {
var accessToken string
accessToken, err = user.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf(userInfoURL, accessToken, openID)
var response []byte
response, err = util.HTTPGet(uri)
if err != nil {
return
}
userInfo = new(Info)
err = json.Unmarshal(response, userInfo)
if err != nil {
return
}
if userInfo.ErrCode != 0 {
err = fmt.Errorf("GetUserInfo Error , errcode=%d , errmsg=%s", userInfo.ErrCode, userInfo.ErrMsg)
return
}
return
}
// UpdateRemark 设置用户备注名
func (user *User) UpdateRemark(openID, remark string) (err error) {
var accessToken string
accessToken, err = user.GetAccessToken()
if err != nil {
return
}
uri := fmt.Sprintf(updateRemarkURL, accessToken)
var response []byte
response, err = util.PostJSON(uri, map[string]string{"openid": openID, "remark": remark})
if err != nil {
return
}
return util.DecodeWithCommonError(response, "UpdateRemark")
}
// ListUserOpenIDs 返回用户列表
func (user *User) ListUserOpenIDs(nextOpenid ...string) (*OpenidList, error) {
accessToken, err := user.GetAccessToken()
if err != nil {
return nil, err
}
uri, _ := url.Parse(userListURL)
q := uri.Query()
q.Set("access_token", accessToken)
if len(nextOpenid) > 0 && nextOpenid[0] != "" {
q.Set("next_openid", nextOpenid[0])
}
uri.RawQuery = q.Encode()
response, err := util.HTTPGet(uri.String())
if err != nil {
return nil, err
}
userlist := new(OpenidList)
err = json.Unmarshal(response, userlist)
if err != nil {
return nil, err
}
return userlist, nil
}
// ListAllUserOpenIDs 返回所有用户OpenID列表
func (user *User) ListAllUserOpenIDs() ([]string, error) {
nextOpenid := ""
openids := []string{}
count := 0
for {
ul, err := user.ListUserOpenIDs(nextOpenid)
if err != nil {
return nil, err
}
openids = append(openids, ul.Data.OpenIDs...)
count += ul.Count
if ul.Total > count {
nextOpenid = ul.NextOpenID
} else {
return openids, nil
}
}
}