forked from thenbsp/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroup.php
More file actions
159 lines (129 loc) · 4.17 KB
/
Group.php
File metadata and controls
159 lines (129 loc) · 4.17 KB
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
<?php
namespace Thenbsp\Wechat\User;
use Thenbsp\Wechat\Bridge\Http;
use Thenbsp\Wechat\Wechat\AccessToken;
use Doctrine\Common\Collections\ArrayCollection;
class Group
{
const SELECT = 'https://api.weixin.qq.com/cgi-bin/groups/get';
const CREATE = 'https://api.weixin.qq.com/cgi-bin/groups/create';
const UPDAET = 'https://api.weixin.qq.com/cgi-bin/groups/update';
const DELETE = 'https://api.weixin.qq.com/cgi-bin/groups/delete';
const QUERY_USER_GROUP = 'https://api.weixin.qq.com/cgi-bin/groups/getid';
const UPDATE_USER_GROUP = 'https://api.weixin.qq.com/cgi-bin/groups/members/update';
const BETCH_UPDATE_USER_GROUP = 'https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate';
/**
* Thenbsp\Wechat\Wechat\AccessToken
*/
protected $accessToken;
/**
* 构造方法
*/
public function __construct(AccessToken $accessToken)
{
$this->accessToken = $accessToken;
}
/**
* 查询全部分组
*/
public function query()
{
$response = Http::request('GET', static::SELECT)
->withAccessToken($this->accessToken)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return new ArrayCollection($response['groups']);
}
/**
* 创建新分组
*/
public function create($name)
{
$body = array(
'group' => array('name'=>$name)
);
$response = Http::request('POST', static::CREATE)
->withAccessToken($this->accessToken)
->withBody($body)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return new ArrayCollection($response['group']);
}
/**
* 修改分组名称
*/
public function update($id, $newName)
{
$body = array(
'group' => array(
'id' => $id,
'name' => $newName
)
);
$response = Http::request('POST', static::UPDAET)
->withAccessToken($this->accessToken)
->withBody($body)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return true;
}
/**
* 删除分组
*/
public function delete($id)
{
$body = array(
'group' => array('id'=>$id)
);
$response = Http::request('POST', static::DELETE)
->withAccessToken($this->accessToken)
->withBody($body)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return true;
}
/**
* 查询指定用户所在分组
*/
public function queryUserGroup($openid)
{
$body = array('openid'=>$openid);
$response = Http::request('POST', static::QUERY_USER_GROUP)
->withAccessToken($this->accessToken)
->withBody($body)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return $response['groupid'];
}
/**
* 移动用户分组
*/
public function updateUserGroup($openid, $newId)
{
$key = is_array($openid)
? 'openid_list'
: 'openid';
$api = is_array($openid)
? static::BETCH_UPDATE_USER_GROUP
: static::UPDATE_USER_GROUP;
$body = array($key=>$openid, 'to_groupid'=>$newId);
$response = Http::request('POST', $api)
->withAccessToken($this->accessToken)
->withBody($body)
->send();
if( $response['errcode'] != 0 ) {
throw new \Exception($response['errmsg'], $response['errcode']);
}
return true;
}
}