临时解决方案,后期版本会重新优化。
打开文件:app/services/work/WorkClientServices.php
搜索并替换authGetExternalcontact方法的代码
public function authGetExternalcontact(int $page = 1, string $cursor = '')
{
/** @var WorkConfig $config */
$config = app()->make(WorkConfig::class);
$corpId = $config->corpId;
if (!$corpId) {
return true;
}
/** @var WorkMemberServices $memberService */
$memberService = app()->make(WorkMemberServices::class);
$menmberList = $memberService->getDataList(['corp_id' => $corpId], ['userid'], $page, 10);
//没有数据就返回成功
if (!$menmberList) {
return true;
}
if($page == 1 && empty($cursor)){
\think\facade\Db::name('work_client')->where('id', "<>", 0)->delete();
\think\facade\Db::name('work_client_follow')->where('id', "<>", 0)->delete();
\think\facade\Db::name('work_client_follow_tags')->where('follow_id ', "<>", 0)->delete();
}
$tagRes = Work::getCorpTags();
$tagMap = [];
foreach ($tagRes["tag_group"]??[] as $tagGroupItem){
foreach ($tagGroupItem["tag"]??[] as $tagItem){
$tagMap[$tagItem["id"]] = $tagItem +["group_id"=>$tagGroupItem["group_id"],"group_name"=>$tagGroupItem["group_name"]];
}
}
$userids = array_column($menmberList, 'userid');
$response = Work::getBatchClientList($userids, $cursor);
$externalContactList = $response['external_contact_list'] ?? [];
$external = [];
$followUser = [];//内部人员跟踪
$externalUserids = [];//客户信息
$this->transaction(function () use ($externalContactList, $corpId, $externalUserids, $followUser, $external, $tagMap) {
foreach ($externalContactList as $item) {
$externalContact = $item['external_contact'];
$unionid = $externalContact['unionid'] ?? '';
if (isset($externalContact['unionid'])) {
unset($externalContact['unionid']);
}
$corpName = $corpFullName = $position = '';
if (isset($externalContact['corp_name'])) {
$corpName = $externalContact['corp_name'];
unset($externalContact['corp_name']);
}
if (isset($externalContact['corp_full_name'])) {
$corpFullName = $externalContact['corp_full_name'];
unset($externalContact['corp_full_name']);
}
if (isset($externalContact['position'])) {
$position = $externalContact['position'];
unset($externalContact['position']);
}
$externalContact['position'] = $position;
unset($externalContact['external_profile']);
$externalContact['external_profile'] = json_encode($externalContact['external_profile'] ?? [],JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$followUserData = [
'userid' => $item['follow_info']['userid'],
'remark' => $item['follow_info']['remark'] ?? '',
'description' => $item['follow_info']['description'] ?? '',
'createtime' => $item['follow_info']['createtime'] ?? '',
'remark_corp_name' => $item['follow_info']['remark_corp_name'] ?? '',
'remark_mobiles' => json_encode($item['follow_info']['remark_mobiles'] ?? ''),
'add_way' => $item['follow_info']['add_way'] ?? '',
'oper_userid' => $item['follow_info']['oper_userid'] ?? '',
'create_time' => time(),
'tags' => [],
];
foreach ($item['follow_info']['tag_id']??[] as $tagId){
$tag = $tagMap[$tagId]??[];
$followUserData['tags'][] = [
'group_name' => $tag['group_name'] ?? '',
'tag_name' => $tag['name'] ?? '',
'type' => $tag['type'] ?? 1,
'tag_id' => $tag['id'],
'create_time' => time()
];
}
$followUser[$externalContact['external_userid']] = $followUserData;
$externalUserids[] = $externalContact['external_userid'];
$externalUserid = $externalContact['external_userid'];
$externalContact['corp_id'] = $corpId;
$externalContact['unionid'] = $unionid;
$externalContact['corp_name'] = $corpName;
$externalContact['corp_full_name'] = $corpFullName;
if ($this->dao->count(['external_userid' => $externalUserid, 'corp_id' => $corpId])) {
unset($externalContact['external_userid']);
$this->dao->update(['external_userid' => $externalUserid], $externalContact);
} else {
$externalContact['create_time'] = time();
$externalContact['update_time'] = time();
$external[] = $externalContact;
}
}
if ($external) {
$this->dao->saveAll($external);
}
$clientList = $this->dao->getColumn([['external_userid', 'in', $externalUserids], ['corp_id', '=', $corpId]], 'id', 'external_userid');
/** @var WorkClientFollowServices $followService */
$followService = app()->make(WorkClientFollowServices::class);
if ($followUser) {
/** @var WorkClientFollowTagsServices $tagService */
$tagService = app()->make(WorkClientFollowTagsServices::class);
foreach ($followUser as $userid => $items) {
if(!isset($clientList[$userid])){
continue;
}
$items['client_id'] = $clientList[$userid];
if (($id = $followService->value(['client_id' => $clientList[$userid], 'userid' => $items['userid']], 'id'))) {
$followService->update($id, [
'remark' => $items['remark'],
'description' => $items['description'],
'createtime' => $items['createtime'],
'remark_corp_name' => $items['remark_corp_name'],
'remark_mobiles' => $items['remark_mobiles'],
'add_way' => $items['add_way'],
'oper_userid' => $items['oper_userid'],
]);
} else {
$res = $followService->save($items);
$id = $res->id;
}
if (!empty($items['tags'])) {
$tagService->delete(['follow_id' => $id]);
foreach ($items['tags'] as &$tag) {
$tag['follow_id'] = $id;
}
$tagService->saveAll($items['tags']);
}
}
}
});
if (isset($response['next_cursor']) && $response['next_cursor']) {
WorkClientJob::dispatchDo('authClient', [$page, $response['next_cursor'] ?? '']);
} else if (empty($response['next_cursor'])) {
WorkClientJob::dispatchDo('authClient', [$page + 1, '']);
}
return true;
}