-
Notifications
You must be signed in to change notification settings - Fork 35
/
usergroup.go
106 lines (93 loc) · 3.17 KB
/
usergroup.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
package weixin
import (
"encoding/json"
"fmt"
)
// 用户分组管理
const (
UserGroupCreateURL = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token=%s"
UserGroupUpdateURL = "https://api.weixin.qq.com/cgi-bin/groups/update?access_token=%s"
UserGroupDeleteURL = "https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=%s"
UserGroupGetAllURL = "https://api.weixin.qq.com/cgi-bin/groups/get?access_token=%s"
UserGroupGetGroupIdURL = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=%s"
UserGroupUpdateMemberGroupURL = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=%s"
UserGroupBatchUpdateMemberGroupURL = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=%s"
)
// GroupsWapper 所有用户分组包装器
type GroupsWapper struct {
WXError
Groups []Group `json:"groups"`
}
// GroupWapper 用户分组包装器
type GroupWapper struct {
WXError
Group *Group `json:"group"`
}
// Group 用户分组
type Group struct {
Id int `json:"id,omitempty"`
Name string `json:"name"`
Count int `json:"count,omitempty"`
}
// CreateUserGroup 创建用户分组
func CreateUserGroup(name string) (g *Group, err error) {
url := fmt.Sprintf(UserGroupCreateURL, AccessToken())
w := &GroupWapper{
Group: &Group{
Name: name,
},
}
err = Post(url, w, w)
return w.Group, err
}
// UpdateUserGroup 修改用户分组名
func UpdateUserGroup(id int, name string) (g *Group, err error) {
url := fmt.Sprintf(UserGroupCreateURL, AccessToken())
w := &GroupWapper{
Group: &Group{
Id: id,
Name: name,
},
}
err = Post(url, w, w)
return w.Group, err
}
// DeleteUserGroup 修改用户分组名
func DeleteUserGroup(groupId int) (err error) {
url := fmt.Sprintf(UserGroupDeleteURL, AccessToken())
body := fmt.Sprintf(`{"group":{"id":%d}}`, groupId)
return Post(url, []byte(body), nil)
}
// GetAllUserGroups 查询所有分组
func GetAllUserGroups() (gs []Group, err error) {
url := fmt.Sprintf(UserGroupGetAllURL, AccessToken())
w := &GroupsWapper{}
err = Get(url, w)
return w.Groups, err
}
// GroupIdWapper 用户所在分组包装器
type GroupIdWapper struct {
WXError
GroupId int `json:"groupid"`
}
// GetGroupIdByOpenId 查询用户所在分组
func GetGroupIdByOpenId(openId string) (groupId int, err error) {
url := fmt.Sprintf(UserGroupGetGroupIdURL, AccessToken())
body := fmt.Sprintf(`{"openid":"%s"}`, openId)
wapper := &GroupIdWapper{}
err = Post(url, []byte(body), wapper)
return wapper.GroupId, err
}
// UpdateMemberGroup 移动用户分组
func UpdateMemberGroup(openId string, toGroupId int) (err error) {
url := fmt.Sprintf(UserGroupUpdateMemberGroupURL, AccessToken())
body := fmt.Sprintf(`{"openid":"%s","to_groupid":%d}`, openId, toGroupId)
return Post(url, []byte(body), nil)
}
// BatchUpdateMemberGroup 批量移动用户分组
func BatchUpdateMemberGroup(openIds []string, toGroupId int) (err error) {
url := fmt.Sprintf(UserGroupBatchUpdateMemberGroupURL, AccessToken())
js, _ := json.Marshal(openIds)
body := fmt.Sprintf(`{"openid_list":%s,"to_groupid":%d}`, js, toGroupId)
return Post(url, []byte(body), nil)
}