123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\Models\CustomerDetailsEsModel;
- use App\Models\CustomerUnionid;
- use App\Support\EmailQueue;
- use App\Support\qyApi\QyCommon;
- class wechatWorkService
- {
- CONST SYNC_CORP_CUSTOMER = 'qyWechat::SyncCorpCustomer';
- // 根据内容员工获取外部客户外层方法(有分页,需要循环获取)
- public static function getCustomerListByUserId($corpid, $userList, $accessToken = null)
- {
- if(empty($corpid) || empty($userList)) {
- return [];
- }
- if(empty($accessToken)) {
- $accessToken = AccessTokenService::getAccessToken($corpid, '根据员工获取外部客户');
- }
- $params = [
- 'userid_list' => $userList,
- 'cursor' => '',
- 'limit' => 100
- ];
- $flag = true;
- $result = null;
- while ($flag) {
- try{
- $result = QyCommon::getCustomerListMethod($params, $accessToken);
- Log::logInfo('同步客户', (array)$result, 'get_corp_customer_list');
- if(!$result) {
- $flag = false;
- }
- if(!empty($result['external_contact_list'])) {
- // 将数据保存入库,并进行翻页查询
- self::userCustomerDataSave($result['external_contact_list'], $corpid);
- }
- if(empty($result['next_cursor'])) {
- $flag = false;
- } else {
- $params['cursor'] = $result['next_cursor'];
- }
- } catch (\Exception $exception) {
- Log::logError('获取企业微信客户数据异常',[
- 'exception' => $exception->getMessage(),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- ], 'sync_corp_customer_exception');
- $subject = config('app.name') . '于' . date('d日H:i:s') . '抛出异常';
- $content = $exception->getMessage() . "\r\n\r\n" . $exception->getTraceAsString();
- EmailQueue::rPush($subject, nl2br($content), ['song.shen@kuxuan-inc.com'], config('企微unionid获取工具'));
- }
- }
- }
- public static function userCustomerDataSave($externalContactList, $corpid, $retry = 0)
- {
- try{
- $unionidList = [];
- foreach($externalContactList as $item) {
- // 企业成员客户跟进信息
- $followInfo = $item['follow_info'];
- // 客户基本信息
- $externalContact = $item['external_contact'];
- if(isset($externalContact['unionid']) && !empty($externalContact['unionid'])) {
- // // 查询unionid是否存在
- // $num = CustomerDetailsEsModel::getDataByUnionid($externalContact['unionid']);
- // if(0 == $num) {
- $unionidList[] = $externalContact['unionid'];
- // }
- }
- // 保存客户信息
- CustomerDetailsEsModel::externalContactDataSave($corpid, $externalContact, $followInfo);
- }
- # 保存unionid
- if(!empty($unionidList)) {
- $unionidList = array_unique($unionidList);
- $alreadyExitUnionid = CustomerUnionid::suffix($corpid)->whereIn('unionid', $unionidList)->get();
- $unionidDataList = [];
- foreach ($unionidList as $unionid) {
- $row = $alreadyExitUnionid->where('unionid', $unionid)->first();
- if(empty($row)) {
- $unionidDataList[] = ['unionid' => $unionid];
- }
- }
- if(!empty($unionidDataList)) {
- CustomerUnionid::suffix($corpid)->insert($unionidDataList);
- }
- }
- } catch (\Exception $exception) {
- if($retry < 2) {
- sleep(1);
- $retry++;
- self::userCustomerDataSave($externalContactList, $corpid, $retry);
- } else {
- // EmailQueue::rPush('保存客户信息失败', $exception->getFile().'('.$exception->getLine().'):'.$exception->getMessage().PHP_EOL.$exception->getTraceAsString(), ['song.shen@kuxuan-inc.com'], '保存客户信息失败');
- // 保存用户信息异常
- Log::logError('保存客户信息失败', [
- 'exception' => $exception->getMessage(),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => $exception->getTraceAsString()
- ], 'external_contract_data_save');
- }
- }
- }
- }
|