Payload.php
10.0 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* Payload.php
*
* @Author: Ghaoo
* @Date 2020/7/27 0027
*/
namespace Weasy\Core;
use addons\qyexternal\model\Contacts;
use addons\qyexternal\model\Corp;
use addons\qyexternal\model\FollowUser;
use addons\qyexternal\model\GroupChat;
use addons\qyexternal\model\Logs;
use think\Log;
use Weasy\External\Api\CorpAPI;
class Payload
{
public function __construct($msg)
{
if (!isset($msg['MsgType']) || $msg['MsgType'] != "event") {
throw new \Exception("解析通知类消息失败!此消息非通知类消息。消息:\n$msg");
}
switch ($msg["Event"]) {
case "change_contact": // 企业成员通知
$this->changeContact($msg);
break;
case "change_external_contact": // 客户通知
$this->changeExternalContact($msg);
break;
case "change_external_chat": // 客户群通知
$this->changeExternalChat($msg);
break;
default:
}
}
// 内部成员变更事件
protected function changeContact($msg)
{
Log::info($msg);
switch ($msg["ChangeType"]) {
case "create_user":
$this->createUser($msg);
break;
case "update_user":
$this->updateUser($msg);
break;
case "delete_user":
$this->deleteUser($msg);
$this->saveLogs("删除企业成员事件", $msg);
break;
default:
}
}
// 外部联系人事件
protected function changeExternalContact($msg)
{
switch ($msg["ChangeType"]) {
case "add_external_contact": // 添加企业客户事件
$this->addExternalContact($msg);
$this->saveLogs("添加企业客户事件", $msg);
break;
case "edit_external_contact": // 编辑企业客户事件,无法获取修改的内容,暂不做处理
$this->saveLogs("编辑企业客户事件", $msg);
break;
case "del_external_contact": // 删除企业客户事件
$this->delExternalContact($msg);
$this->saveLogs("删除企业客户事件", $msg);
break;
case "del_follow_user": // 被外部联系人删除
$this->delExternalContact($msg);
$this->saveLogs("被外部联系人删除", $msg);
break;
default:
}
}
// 客户群变更事件
protected function changeExternalChat($msg)
{
$wxChat = $this->getExternalGroupChat($msg["ToUserName"], $msg["ChatId"]);
$chat = GroupChat::where("corp_id", $msg["ToUserName"])->where("chat_id", $msg["ChatId"])->find();
if (empty($wxChat["member_list"])) {
$chat->delete();
$this->saveLogs("客户群解散", $msg);
return;
}
if (!$chat) {
$chat = new GroupChat();
$chat->corp_id = $msg["ToUserName"];
$chat->corp_name = Corp::where("corp_id", $msg["ToUserName"])->find()->name;
$chat->chat_id = $msg["ChatId"];
}
$chat->name = $wxChat["name"] ? $wxChat["name"] : $msg["ChatId"];
$chat->owner = $wxChat["owner"];
$chat->create_time = $wxChat["create_time"];
if (isset($wxChat["notice"])) {
$chat->notice = $wxChat["notice"];
}
$chat->save();
$this->saveLogs("客户群变更事件", $msg);
}
/**
* @Note: 新增外部联系人
* @Author: Ghaoo
* @param $msg
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
protected function addExternalContact($msg)
{
$contact = $this->getExternalContact($msg["ToUserName"], $msg["ExternalUserID"]);
if ($contact) {
$external = $contact["external_contact"];
$follow_user = [];
if (isset($contact["follow_user"])) {
foreach ($contact["follow_user"] as $follow) {
array_push($follow_user, $follow["userid"]);
}
}
}
$user = Contacts::where("corp_id", $msg["ToUserName"])->where("external_userid", $msg["ExternalUserID"])->find();
if (!$user) {
$user = new Contacts();
$user->corp_id = $msg["ToUserName"];
$user->corp_name = Corp::where("corp_id", $msg["ToUserName"])->find()->name;
$user->external_userid = $msg["ExternalUserID"];
$user->name = $msg["ExternalUserID"];
}
if (isset($external)) {
$user->name = $external["name"];
$user->type = $external["type"];
$user->gender = $external["gender"];
if (isset($external["avatar"])) {
$user->avatar = $external["avatar"];
}
if (isset($external["unionid"])) {
$user->unionid = $external["unionid"];
}
$user->follow_user = json_encode($follow_user);
}
$user->save();
}
/**
* @Note: 主动或被动删除外部联系人
* @Author: Ghaoo
* @param $msg
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
protected function delExternalContact($msg)
{
$user = Contacts::where("corp_id", $msg["ToUserName"])->where("external_userid", $msg["ExternalUserID"])->find();
if ($user) {
$follow = json_decode($user['follow_user'], true);
$follow = array_diff($follow, [$msg["UserID"]]);
if (count($follow) <= 0) {
$user->delete();
} else {
$user->follow_user = json_encode($follow);
$user->save();
}
}
}
/**
* @Note: 新增企业成员事件
* @param $msg
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
protected function createUser($msg)
{
$user = new FollowUser();
$user->corp_id = $msg["ToUserName"];
$user->corp_name = Corp::where("corp_id", $msg["ToUserName"])->find()->name;
$user->userid = $msg["UserID"];
if (isset($msg["Name"])) {
$user->name = $msg["Name"];
}
if (isset($msg["Mobile"])) {
$user->mobile = $msg["Mobile"];
}
if (isset($msg["Email"])) {
$user->email = $msg["Email"];
}
if (isset($msg["Avatar"])) {
$user->avatar = $msg["Avatar"];
}
if (isset($msg["Gender"])) {
$user->gender = $msg["Gender"];
}
if (isset($msg["Status"])) {
$user->status = $msg["Status"];
}
$user->save();
$this->saveLogs("新增企业成员事件", $msg);
}
/**
* @Note: 更新企业成员事件
* @Author: Ghaoo
* @param $msg
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
protected function updateUser($msg)
{
$user = FollowUser::where("corp_id", $msg["ToUserName"])->where("userid", $msg["UserID"])->find();
if ($user) {
$user->corp_id = $msg["ToUserName"];
$user->corp_name = Corp::where("corp_id", $msg["ToUserName"])->find()->name;
$user->userid = isset($msg["NewUserID"]) ? $msg["NewUserID"] : $msg["UserID"];
if (isset($msg["Name"])) {
$user->name = $msg["Name"];
}
if (isset($msg["Mobile"])) {
$user->mobile = $msg["Mobile"];
}
if (isset($msg["Email"])) {
$user->email = $msg["Email"];
}
if (isset($msg["Avatar"])) {
$user->avatar = $msg["Avatar"];
}
if (isset($msg["Gender"])) {
$user->gender = $msg["Gender"];
}
if (isset($msg["Status"])) {
$user->status = $msg["Status"];
}
$user->save();
$this->saveLogs("更新企业成员事件", $msg);
}
}
/**
* @Note: 删除成员事件
* @Author: Ghaoo
* @param $msg
*/
protected function deleteUser($msg)
{
FollowUser::where("corp_id", $msg["ToUserName"])->where("userid", $msg["UserID"])->delete();
}
/**
* @Note:
* @Author: Ghaoo
* @param $corpid
* @param $userid
* @return bool|null
*/
protected function getExternalContact($corpid, $userid)
{
try {
$corp = Corp::where("corp_id", $corpid)->find();
$api = new CorpAPI($corpid, $corp->external_secret);
return $api->ExternalContactGet($userid);
} catch (\Exception $exception) {
return false;
}
}
/**
* @Note:
* @Author: Ghaoo
* @param $corpid
* @param $chatid
* @return bool
*/
protected function getExternalGroupChat($corpid, $chatid)
{
try {
$corp = Corp::where("corp_id", $corpid)->find();
$api = new CorpAPI($corpid, $corp->external_secret);
return $api->ExternalContactGroupChatGet($chatid);
} catch (\Exception $exception) {
return false;
}
}
protected function saveLogs($title, $msg)
{
$md5 = md5(json_encode($msg));
$log = Logs::where("corp_id", $msg["ToUserName"])->where("md5", $md5)->count();
if (!$log) {
$log = new Logs();
$log->corp_id = $msg["ToUserName"];
$log->content = json_encode($msg);
$log->title = $title;
$log->md5 = $md5;
$log->save();
}
}
}