forked from chanxuehong/wechat
/
group.go
77 lines (69 loc) · 1.76 KB
/
group.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
package user
import (
"github.com/chanxuehong/wechat.v2/mp/core"
)
// GroupId 查询用户所在分组.
func GroupId(clt *core.Client, openId string) (groupId int64, err error) {
const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/getid?access_token="
var request = struct {
OpenId string `json:"openid"`
}{
OpenId: openId,
}
var result struct {
core.Error
GroupId int64 `json:"groupid"`
}
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result.Error
return
}
groupId = result.GroupId
return
}
// MoveToGroup 移动用户分组.
func MoveToGroup(clt *core.Client, openId string, toGroupId int64) (err error) {
const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token="
var request = struct {
OpenId string `json:"openid"`
ToGroupId int64 `json:"to_groupid"`
}{
OpenId: openId,
ToGroupId: toGroupId,
}
var result core.Error
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result
return
}
return
}
// BatchMoveToGroup 批量移动用户分组.
func BatchMoveToGroup(clt *core.Client, openIdList []string, toGroupId int64) (err error) {
const incompleteURL = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token="
if len(openIdList) <= 0 {
return
}
var request = struct {
OpenIdList []string `json:"openid_list,omitempty"`
ToGroupId int64 `json:"to_groupid"`
}{
OpenIdList: openIdList,
ToGroupId: toGroupId,
}
var result core.Error
if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
return
}
if result.ErrCode != core.ErrCodeOK {
err = &result
return
}
return
}