Bez popisu

ExternalContactService.php 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\CustomerDetails;
  5. class ExternalContactService
  6. {
  7. /**
  8. * 获取外部联系人信息详情
  9. * @param $corpid string 授权方企业微信id
  10. * @param $externalUserid string 外部联系人的userid
  11. * @param $cursor string 上次请求返回的next_cursor,用于分页请求(见官方文档)
  12. * @param $retry integer 重试次数,默认为0
  13. * */
  14. public static function getExternalContactDetail($corpid, $externalUserid, $cursor=null, $retry=0)
  15. {
  16. # 获取SuiteAccessToken
  17. $accessToken = AccessTokenService::getAccessToken($corpid, '获取外部联系人信息详情');
  18. if(empty($accessToken)) { // 令牌获取失败,发送报警
  19. Log::logError('【获取外部联系人信息详情】令牌获取失败', [
  20. 'corpid' => $corpid,
  21. 'externalUserId' => $externalUserid,
  22. 'cursor' => $cursor,
  23. 'retry' => $retry,
  24. 'access_token' => $accessToken
  25. ], 'GetExternalContactDetail');
  26. return false;
  27. }
  28. # 获取外部联系人详情信息API
  29. $getExternalContactUri = config('qyWechat.external_contact_detail');
  30. $getExternalContactUri .= $accessToken . '&external_userid=' . $externalUserid;
  31. if($cursor) {
  32. $getExternalContactUri .= '&cursor=' . $cursor;
  33. }
  34. $response = HttpService::httpGet($getExternalContactUri);
  35. $responseData = json_decode($response, true);
  36. if(isset($responseData['errcode']) && $responseData['errcode']) {
  37. if($retry <=5 && $responseData['errcode'] == -1) {
  38. $retry++;
  39. return ExternalContactService::getExternalContactDetail($corpid, $externalUserid, $cursor, $retry);
  40. }
  41. Log::logError('获取外部联系人详情信息返回错误码', [
  42. 'response' => $response
  43. ], 'ExternalContactDetailGet');
  44. return [];
  45. }
  46. return $responseData;
  47. }
  48. }