OpenApi.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php namespace App\Servers\WeiBan;
  2. use Ixudra\Curl\Facades\Curl;
  3. use App\Facades\Servers\Logs\Log;
  4. use App\Facades\Servers\Redis\Redis;
  5. use App\Facades\Servers\Redis\RedisLock;
  6. /**
  7. * 微信小程序
  8. *
  9. */
  10. class OpenApi
  11. {
  12. // 微伴接口API
  13. private $domain = 'https://open.weibanzhushou.com';
  14. // 微伴接口调用接口凭证
  15. private $accessToken = null;
  16. // 返回结果
  17. public function __construct(){
  18. // 接口凭证不存在则获取
  19. if( is_null($this->accessToken) ) $this->accessToken = $this->getAccessToken();
  20. }
  21. /**
  22. * 获取接口调用凭证
  23. *
  24. */
  25. public function getAccessToken(){
  26. // 从缓存获取数据
  27. $corpId = config('weiban.corp_id');
  28. $secret = config('weiban.secret');
  29. // 如果没有配置
  30. if( !$corpId || !$secret ) return '';//['error'=>'请先配置微伴助手接口参数'];
  31. // 缓存key
  32. $key = 'weiban:corpid:access_token:'.$corpId;
  33. // 获取凭证
  34. $accessToken = cache($key);
  35. // 如果存在凭证
  36. if( $accessToken ) return $accessToken;
  37. // 组装接口
  38. $url = $this->domain.'/open-api/access_token/get';
  39. // 获取凭证
  40. $result = Curl::to($url)->withData(['corp_id'=>$corpId,'secret'=>$secret])->asJson(true)->post();
  41. // 如果没有返回
  42. if( !isset($result['errcode']) ) {
  43. // 记录错误信息
  44. Log::error('weiban_openai/access_token','未获取到凭证');
  45. return '';
  46. }
  47. // 调用失败
  48. if( $result['errcode'] != 0 ) {
  49. // 记录错误信息
  50. Log::error('weiban_openai/access_token',$result['errcode'].'=>'.$result['errmsg']);
  51. return '';
  52. }
  53. // 调用成功,缓存调用凭证
  54. cache([$key=>$result['access_token']],$result['expires_in']);
  55. // 返回凭证
  56. return $result['access_token'];
  57. }
  58. /**
  59. * 获取更新的用户列表
  60. *
  61. * @param int $limit 每页条数
  62. * @param int $offset 列表偏移。用于迭代获取所有数据,默认值为0
  63. * @param int $startTime 时间筛选开始时间,时间戳格式。
  64. * @param string $source 客户关联信息的类型,可选external_user(用户信息), remark(客户详情页的基本信息和自定义信息), staff_relation(客户关系), tag_relation(标签数据)。
  65. * (注:返回的数据是external_user的updated_at,source选external_user外的其他条件是筛选(remark:客户更新基本信息, staff_relation:客户更新关系,tag_relation: 客户更新标签)时间。)
  66. *
  67. */
  68. public function getUserList($limit,$offset,$startTime=0,$source='remark'){
  69. // 获取调用凭证
  70. $accessToken = $this->getAccessToken();
  71. // 如果没有返回
  72. if( !$accessToken ) return ['total'=>0,'user_list'=>[]];
  73. // 参数
  74. $param = ['order'=>'asc','source'=>$source,'start_update_time'=>$startTime,'limit'=>$limit,'offset'=>$offset,'access_token'=>$accessToken];
  75. // 组装接口
  76. $url = $this->domain.'/open-api/external_user/update/list';
  77. // 调用接口
  78. $result = Curl::to($url)->withData($param)->asJson(true)->get();
  79. // 如果没有返回
  80. if( !isset($result['errcode']) ) {
  81. Log::error('weiban_openai/external_user_update',$source.'=>未获取到用户列表');
  82. return ['total'=>0,'user_list'=>[]];
  83. }
  84. // 如果是凭证错误,递归
  85. if( $result['errcode'] == '10001' ) return $this->getUserList($limit,$offset,$startTime);
  86. // 调用失败
  87. if( $result['errcode'] != 0 ) {
  88. Log::error('weiban_openai/external_user_update',$source.'=>'.$result['errcode'].'=>'.$result['errmsg']);
  89. return ['total'=>0,'user_list'=>[]];
  90. }
  91. // 返回结果
  92. return ['total'=>$result['total'],'user_list'=>$result['external_user_list']];
  93. }
  94. /**
  95. * 获取手机号用户
  96. *
  97. * @param string $phone 手机号
  98. *
  99. */
  100. public function getUserListByPhone($phone){
  101. // 获取调用凭证
  102. $accessToken = $this->getAccessToken();
  103. // 如果没有返回
  104. if( !$accessToken ) return [];
  105. // 参数
  106. $param = ['phone_number'=>$phone,'access_token'=>$accessToken];
  107. // 组装接口
  108. $url = $this->domain.'/open-api/external_user/list';
  109. // 调用接口
  110. $result = Curl::to($url)->withData($param)->asJson(true)->get();
  111. // 如果没有返回
  112. if( !isset($result['errcode']) ) {
  113. Log::error('weiban_openai/external_user_list','未获取到用户列表');
  114. return [];
  115. }
  116. // 如果是凭证错误,递归
  117. if( $result['errcode'] == '10001' ) return $this->getUserListByPhone($phone);
  118. // 调用失败
  119. if( $result['errcode'] != 0 ) {
  120. Log::error('weiban_openai/external_user_list',$result['errcode'].'=>'.$result['errmsg']);
  121. return [];
  122. }
  123. // 返回结果
  124. return $result['external_user_list'];
  125. }
  126. /**
  127. * 获取用户详情
  128. *
  129. * @param string 客户id。对应企业微信客户 id
  130. *
  131. */
  132. public function getUserDetail($id) {
  133. // 获取调用凭证
  134. $accessToken = $this->getAccessToken();
  135. // 如果没有返回
  136. if( !$accessToken ) return [];
  137. // 组装接口
  138. $url = $this->domain.'/open-api/external_user/get';
  139. // 调用接口
  140. $result = Curl::to($url)->withData(['id'=>$id,'access_token'=>$accessToken])->asJson(true)->get();
  141. // 如果没有返回
  142. if( !isset($result['errcode']) ) {
  143. Log::error('weiban_openai/external_user_get',$id.'=>接口未返回数据');
  144. return [];
  145. }
  146. // 如果是凭证错误,递归
  147. if( $result['errcode'] == '10001' ) return $this->getUserDetail($id);
  148. // 调用失败
  149. if( $result['errcode'] != 0 ) {
  150. Log::error('weiban_openai/external_user_get',$id.'=>'.$result['errcode'].'=>'.$result['errmsg']);
  151. return [];
  152. }
  153. // 返回结果
  154. return $result['external_user'];
  155. }
  156. }
  157. ?>