Sync.php
7.9 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
<?php
/**
* Sync.php
*
* @Author: Ghaoo
* @Date 2020/7/27 0027
*/
namespace Weasy\Core;
use addons\qyexternal\model\Contacts;
use addons\qyexternal\model\FollowUser;
use addons\qyexternal\model\GroupChat;
use addons\qyexternal\model\UserBehavior;
use think\Log;
use Weasy\External\Api\CorpAPI;
class Sync
{
public $corp;
public $external;
public $contacts;
public function __construct($corp)
{
$this->corp = $corp;
$this->external = new CorpAPI($corp->corp_id, $corp->external_secret);
$this->contacts = new CorpAPI($corp->corp_id, $corp->contacts_secret);
}
/**
* @Note: 同步配置了客户联系功能的成员列表
* @Author: Ghaoo
* @throws \Weasy\External\Utils\Error\HttpError
* @throws \Weasy\External\Utils\Error\InternalError
* @throws \Weasy\External\Utils\Error\NetWorkError
* @throws \Weasy\External\Utils\Error\ParameterError
* @throws \Weasy\External\Utils\Error\QyApiError
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function FollowUserList()
{
$follow = $this->external->ExternalContactGetFollowUserList();
$users = [];
foreach ($follow as $key => $uid) {
$user = $this->contacts->UserGet($uid);
$users[$key] = [
"corp_id" => $this->corp->corp_id,
"corp_name" => $this->corp->name,
"userid" => $uid,
"name" => $user->name,
"mobile" => $user->mobile,
"email" => $user->email,
"avatar" => $user->avatar,
"gender" => $user->gender,
];
$fu = FollowUser::where("corp_id", $this->corp->corp_id)->where("userid", $uid)->find();
if ($fu) {
$users[$key]["id"] = $fu->id;
}
}
$users = array_values($users);
$model = new FollowUser();
$model->saveAll($users);
}
/**
* @Note: 同步企业下所有客户信息
* @Author: Ghaoo
* @throws \Weasy\External\Utils\Error\HttpError
* @throws \Weasy\External\Utils\Error\InternalError
* @throws \Weasy\External\Utils\Error\NetWorkError
* @throws \Weasy\External\Utils\Error\ParameterError
* @throws \Weasy\External\Utils\Error\QyApiError
*/
public function ContactsList($userid, $cursor = "")
{
try {
$contacts = $this->external->ExternalContactBatch($userid, $cursor);
$this->saveExternalContact($contacts['external_contact_list'], $userid);
if (!empty($contacts['next_cursor'])) {
$this->ContactsList($userid, $cursor);
}
} catch (\Exception $e){
//Log::write($e->getTraceAsString());
}
}
/**
* @Note: 保存外部联系人数据
* @Author: Ghaoo
* @param $contacts
* @param $userid
*/
protected function saveExternalContact($contacts, $userid)
{
foreach ($contacts as $contact) {
try {
$user = Contacts::where("corp_id", $this->corp->corp_id)->where("external_userid", $contact['external_contact']["external_userid"])->find();
if (!$user) {
$user = new Contacts();
$user->corp_id = $this->corp->corp_id;
$user->corp_name = $this->corp->name;
$user->external_userid = $contact['external_contact']["external_userid"];
$user->name = $contact["external_contact"]["name"];
$user->avatar = $contact["external_contact"]["avatar"];
$user->type = $contact["external_contact"]["type"];
$user->gender = $contact["external_contact"]["gender"];
if (isset($contact["external_contact"]["unionid"])) {
$user->unionid = $contact["external_contact"]["unionid"];
}
$user->follow_user = json_encode([$userid]);
} else {
$follow_user = json_decode($user->follow_user, true);
array_push($follow_user, $userid);
$user->follow_user = json_encode($follow_user);
}
$user->save();
} catch (\Exception $e) {
Log::write([$e->getMessage(), $e->getTraceAsString()]);
}
}
}
/**
* @Note: 同步企业下所有客户群信息
* @Author: Ghaoo
* @throws \Weasy\External\Utils\Error\HttpError
* @throws \Weasy\External\Utils\Error\InternalError
* @throws \Weasy\External\Utils\Error\NetWorkError
* @throws \Weasy\External\Utils\Error\ParameterError
* @throws \Weasy\External\Utils\Error\QyApiError
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function GroupChatList()
{
$chats = $this->external->ExternalContactGroupChatList(0, 1000);
if ($chats) {
$group_chats = [];
foreach ($chats as $key=>$item) {
$group = GroupChat::where("corp_id", $this->corp->corp_id)->where("chat_id", $item["chat_id"])->find();
if ($group) {
$group_chats[$key]["id"] = $group->id;
}
$group_chats[$key]["corp_id"] = $this->corp->corp_id;
$group_chats[$key]["corp_name"] = $this->corp->name;
$group_chats[$key]["chat_id"] = $item["chat_id"];
$chat = $this->external->ExternalContactGroupChatGet($item["chat_id"]);
$group_chats[$key]["name"] = $chat["name"];
$group_chats[$key]["owner"] = $chat["owner"];
$group_chats[$key]["create_time"] = $chat["create_time"];
if (isset($chat["notice"])) {
$group_chats[$key]["notice"] = $chat["notice"];
}
}
$m = new GroupChat();
$m->saveAll($group_chats);
}
}
/**
* @Note: 同步员工数据
* @Author: Ghaoo
* @param $userid
* @param $start_time
* @param $end_time
* @throws \Weasy\External\Utils\Error\HttpError
* @throws \Weasy\External\Utils\Error\InternalError
* @throws \Weasy\External\Utils\Error\NetWorkError
* @throws \Weasy\External\Utils\Error\ParameterError
* @throws \Weasy\External\Utils\Error\QyApiError
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function UserBehaviorData($userid, $start_time, $end_time)
{
$data = $this->external->ExternalContactBehaviorData($userid, $start_time, $end_time);
$udata = [];
foreach ($data as $key=>$datum) {
if ($behavior = UserBehavior::where("corp_id", $this->corp->corp_id)->where("userid", $userid)->where("date", $datum["stat_time"])->find()) {
$udata[$key]["id"] = $behavior->id;
}
$udata[$key]["corp_id"] = $this->corp->corp_id;
$udata[$key]["userid"] = $userid;
$udata[$key]["date"] = $datum["stat_time"];
$udata[$key]["chat_cnt"] = $datum["chat_cnt"];
$udata[$key]["message_cnt"] = $datum["message_cnt"];
$udata[$key]["reply_percentage"] = isset($datum["reply_percentage"])?$datum["reply_percentage"]:0;
$udata[$key]["avg_reply_time"] = isset($datum["avg_reply_time"])?$datum["avg_reply_time"]:0;
$udata[$key]["negative_feedback_cnt"] = $datum["negative_feedback_cnt"];
$udata[$key]["new_apply_cnt"] = $datum["new_apply_cnt"];
$udata[$key]["new_contact_cnt"] = $datum["new_contact_cnt"];
}
$behavior = new UserBehavior();
$behavior->saveAll($udata);
}
}