Sin descripción

wechatWorkService.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\CustomerDetailsEsModel;
  5. use App\Models\CustomerUnionid;
  6. use App\Support\EmailQueue;
  7. use App\Support\qyApi\QyCommon;
  8. class wechatWorkService
  9. {
  10. CONST SYNC_CORP_CUSTOMER = 'qyWechat::SyncCorpCustomer';
  11. // 根据内容员工获取外部客户外层方法(有分页,需要循环获取)
  12. public static function getCustomerListByUserId($corpid, $userList, $accessToken = null)
  13. {
  14. if(empty($corpid) || empty($userList)) {
  15. return [];
  16. }
  17. if(empty($accessToken)) {
  18. $accessToken = AccessTokenService::getAccessToken($corpid, '根据员工获取外部客户');
  19. }
  20. $params = [
  21. 'userid_list' => $userList,
  22. 'cursor' => '',
  23. 'limit' => 100
  24. ];
  25. $flag = true;
  26. $result = null;
  27. while ($flag) {
  28. try{
  29. $result = QyCommon::getCustomerListMethod($params, $accessToken);
  30. Log::logInfo('同步客户', (array)$result, 'get_corp_customer_list');
  31. if(!$result) {
  32. $flag = false;
  33. }
  34. if(!empty($result['external_contact_list'])) {
  35. // 将数据保存入库,并进行翻页查询
  36. self::userCustomerDataSave($result['external_contact_list'], $corpid);
  37. }
  38. if(empty($result['next_cursor'])) {
  39. $flag = false;
  40. } else {
  41. $params['cursor'] = $result['next_cursor'];
  42. }
  43. } catch (\Exception $exception) {
  44. Log::logError('获取企业微信客户数据异常',[
  45. 'exception' => $exception->getMessage(),
  46. 'file' => $exception->getFile(),
  47. 'line' => $exception->getLine(),
  48. ], 'sync_corp_customer_exception');
  49. $subject = config('app.name') . '于' . date('d日H:i:s') . '抛出异常';
  50. $content = $exception->getMessage() . "\r\n\r\n" . $exception->getTraceAsString();
  51. EmailQueue::rPush($subject, nl2br($content), ['song.shen@kuxuan-inc.com'], config('企微unionid获取工具'));
  52. }
  53. }
  54. }
  55. public static function userCustomerDataSave($externalContactList, $corpid, $retry = 0)
  56. {
  57. try{
  58. $unionidList = [];
  59. foreach($externalContactList as $item) {
  60. // 企业成员客户跟进信息
  61. $followInfo = $item['follow_info'];
  62. // 客户基本信息
  63. $externalContact = $item['external_contact'];
  64. if(isset($externalContact['unionid']) && !empty($externalContact['unionid'])) {
  65. // // 查询unionid是否存在
  66. // $num = CustomerDetailsEsModel::getDataByUnionid($externalContact['unionid']);
  67. // if(0 == $num) {
  68. $unionidList[] = $externalContact['unionid'];
  69. // }
  70. }
  71. // 保存客户信息
  72. CustomerDetailsEsModel::externalContactDataSave($corpid, $externalContact, $followInfo);
  73. }
  74. # 保存unionid
  75. if(!empty($unionidList)) {
  76. $unionidList = array_unique($unionidList);
  77. $alreadyExitUnionid = CustomerUnionid::suffix($corpid)->whereIn('unionid', $unionidList)->get();
  78. $unionidDataList = [];
  79. foreach ($unionidList as $unionid) {
  80. $row = $alreadyExitUnionid->where('unionid', $unionid)->first();
  81. if(empty($row)) {
  82. $unionidDataList[] = ['unionid' => $unionid];
  83. }
  84. }
  85. if(!empty($unionidDataList)) {
  86. CustomerUnionid::suffix($corpid)->insert($unionidDataList);
  87. }
  88. }
  89. } catch (\Exception $exception) {
  90. if($retry < 2) {
  91. sleep(1);
  92. $retry++;
  93. self::userCustomerDataSave($externalContactList, $corpid, $retry);
  94. } else {
  95. // EmailQueue::rPush('保存客户信息失败', $exception->getFile().'('.$exception->getLine().'):'.$exception->getMessage().PHP_EOL.$exception->getTraceAsString(), ['song.shen@kuxuan-inc.com'], '保存客户信息失败');
  96. // 保存用户信息异常
  97. Log::logError('保存客户信息失败', [
  98. 'exception' => $exception->getMessage(),
  99. 'file' => $exception->getFile(),
  100. 'line' => $exception->getLine(),
  101. 'trace' => $exception->getTraceAsString()
  102. ], 'external_contract_data_save');
  103. }
  104. }
  105. }
  106. }