forked from shenghui0779/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.go
161 lines (141 loc) · 6.17 KB
/
subscriber.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
155
156
157
158
159
160
161
package oa
import (
"encoding/json"
"github.com/tidwall/gjson"
"github.com/accaolei/gochat/wx"
)
// MaxSubscriberListCount 关注列表的最大数目
const MaxSubscriberListCount = 10000
// SubscribeScene 关注的渠道来源
type SubscribeScene string
// 微信支持的关注的渠道来源
const (
SceneSearch SubscribeScene = "ADD_SCENE_SEARCH" // 公众号搜索
SceneQRCode SubscribeScene = "ADD_SCENE_QR_CODE" // 扫描二维码
SceneAccountMigration SubscribeScene = "ADD_SCENE_ACCOUNT_MIGRATION" // 公众号迁移
SceneProfileCard SubscribeScene = "ADD_SCENE_PROFILE_CARD" // 名片分享
SceneProfileLink SubscribeScene = "ADD_SCENE_PROFILE_LINK" // 图文页内名称点击
SceneProfileItem SubscribeScene = "ADD_SCENE_PROFILE_ITEM" // 图文页右上角菜单
ScenePaid SubscribeScene = "ADD_SCENE_PAID" // 支付后关注
SceneWechatAD SubscribeScene = "ADD_SCENE_WECHAT_ADVERTISEMENT" // 微信广告
SceneOthers SubscribeScene = "ADD_SCENE_OTHERS" // 其他
)
// SubscriberInfo 关注用户信息
type SubscriberInfo struct {
Subscribe int `json:"subscribe"` // 用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息。
OpenID string `json:"openid"` // 用户的标识,对当前公众号唯一
NickName string `json:"nickname"` // 用户的昵称
Sex int `json:"sex"` // 用户的性别,男(1),女(2),未知(0)
Country string `json:"country"` // 用户所在国家
City string `json:"city"` // 用户所在城市
Province string `json:"province"` // 用户所在省份
Language string `json:"language"` // 用户的语言,简体中文为zh_CN
HeadImgURL string `json:"headimgurl"` // 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。
SubscribeTime int64 `json:"subscribe_time"` // 用户关注时间,为时间戳。如果用户曾多次关注,则取最后关注时间
UnionID string `json:"unionid"` // 只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。
Remark string `json:"remark"` // 公众号运营者对粉丝的备注,公众号运营者可在微信公众平台用户管理界面对粉丝添加备注
GroupID int64 `json:"groupid"` // 用户所在的分组ID(兼容旧的用户分组接口)
TagidList []int64 `json:"tagid_list"` // 用户被打上的标签ID列表
SubscribeScene SubscribeScene `json:"subscribe_scene"` // 用户关注的渠道来源
QRScene int64 `json:"qr_scene"` // 二维码扫码场景(开发者自定义)
QRSceneStr string `json:"qr_scene_str"` // 二维码扫码场景描述(开发者自定义)
}
// SubscriberList 关注列表
type SubscriberList struct {
Total int `json:"total"`
Count int `json:"count"`
Data SubscriberListData `json:"data"`
NextOpenID string `json:"next_openid"`
}
// SubscriberListData 关注列表数据
type SubscriberListData struct {
OpenID []string `json:"openid"`
}
// GetSubscriberInfo 获取关注用户信息
func GetSubscriberInfo(dest *SubscriberInfo, openid string) wx.Action {
return wx.NewAPI(SubscriberGetURL,
wx.WithMethod(wx.MethodGet),
wx.WithQuery("openid", openid),
wx.WithQuery("lang", "zh_CN"),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// BatchGetSubscribers 批量关注用户信息
func BatchGetSubscribers(dest *[]*SubscriberInfo, openids ...string) wx.Action {
return wx.NewAPI(SubscriberBatchGetURL,
wx.WithMethod(wx.MethodPost),
wx.WithBody(func() ([]byte, error) {
userList := make([]map[string]string, 0, len(openids))
for _, v := range openids {
userList = append(userList, map[string]string{
"openid": v,
"lang": "zh_CN",
})
}
return json.Marshal(wx.X{"user_list": userList})
}),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal([]byte(gjson.GetBytes(resp, "user_info_list").Raw), dest)
}),
)
}
// GetSubscriberList 获取关注用户列表
func GetSubscriberList(dest *SubscriberList, nextOpenID ...string) wx.Action {
return wx.NewAPI(SubscriberListURL,
wx.WithMethod(wx.MethodGet),
wx.WithQuery("next_openid", nextOpenID[0]),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// GetBlackList 获取用户黑名单列表
func GetBlackList(dest *SubscriberList, beginOpenID ...string) wx.Action {
return wx.NewAPI(BlackListGetURL,
wx.WithMethod(wx.MethodPost),
wx.WithBody(func() ([]byte, error) {
params := wx.X{
"begin_openid": "",
}
if len(beginOpenID) != 0 {
params["begin_openid"] = beginOpenID[0]
}
return json.Marshal(params)
}),
wx.WithDecode(func(resp []byte) error {
return json.Unmarshal(resp, dest)
}),
)
}
// BlackSubscribers 拉黑用户
func BlackSubscribers(openids ...string) wx.Action {
return wx.NewAPI(BatchBlackListURL,
wx.WithMethod(wx.MethodPost),
wx.WithBody(func() ([]byte, error) {
return json.Marshal(wx.X{"openid_list": openids})
}),
)
}
// UnBlackSubscriber 取消拉黑用户
func UnBlackSubscribers(openids ...string) wx.Action {
return wx.NewAPI(BatchUnBlackListURL,
wx.WithMethod(wx.MethodPost),
wx.WithBody(func() ([]byte, error) {
return json.Marshal(wx.X{"openid_list": openids})
}),
)
}
// SetUserRemark 设置用户备注名(该接口暂时开放给微信认证的服务号)
func SetUserRemark(openid, remark string) wx.Action {
return wx.NewAPI(UserRemarkSetURL,
wx.WithMethod(wx.MethodPost),
wx.WithBody(func() ([]byte, error) {
return json.Marshal(wx.X{
"openid": openid,
"remark": remark,
})
}),
)
}