WeiBanSync.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Jobs;
  3. use App\Facades\Servers\Logs\Log;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use App\Facades\Servers\WeiBan\OpenApi;
  11. use App\Models\WeiBan\Sync;
  12. use App\Models\Custom;
  13. use App\Models\WeiBan\External as WeiBanExternal;
  14. use App\Models\WeiBan\Follow as WeiBanFollow;
  15. use App\Models\WeiBan\Tags as WeiBanTags;
  16. class WeiBanSync implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. /**
  20. * 任务可尝试的次数
  21. *
  22. * @var int
  23. */
  24. public $tries = 3;
  25. /**
  26. * 队列参数
  27. * @var array|mixed
  28. */
  29. protected $extUser = [];
  30. /**
  31. * Create a new job instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct($extUser=[])
  36. {
  37. // 获取参数赋值
  38. $this->extUser = $extUser;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. // 如果存在需要更新的的客户
  48. if( $this->extUser['id'] ){
  49. // 实例化
  50. $Sync = new Sync();
  51. // 尝试执行
  52. try{
  53. // 通过id查询详情信息
  54. $extUser = OpenApi::getUserDetail($this->extUser['id']);
  55. // 不存在客户信息,再次获取
  56. if( !$extUser ) $extUser = OpenApi::getUserDetail($this->extUser['id']);
  57. // 存在客户信息,则继续
  58. if( $extUser ) $this->sync_user($extUser);
  59. // 解除锁定
  60. $Sync->unlockSyncExtidMark($this->extUser['id']);
  61. } catch (\Exception $exception) {
  62. // 解除锁定
  63. $Sync->unlockSyncExtidMark($this->extUser['id']);
  64. // 记录错误信息
  65. Log::error('weiban_sync_error',$exception->getMessage().'尝试执行第'.$this->attempts().'次',$this->extUser);
  66. // 每次尝试执行 时间间隔
  67. $this->release($this->attempts() * 5);
  68. }
  69. }
  70. }
  71. /**
  72. * 同步
  73. *
  74. * */
  75. public function sync_user($extUser){
  76. // 实例
  77. $Custom = New Custom();
  78. $External = New WeiBanExternal();
  79. $Follow = New WeiBanFollow();
  80. $Tags = New WeiBanTags();
  81. // 获取结果
  82. $followList = $extUser['follow_staffs'];
  83. // 获取结构数据
  84. $extUser = ['id'=>$extUser['id'],'name'=>$extUser['name'],'avatar'=>str_ireplace('http://','https://',(string)$extUser['avatar']),'gender'=>$extUser['gender'],'type'=>$extUser['type'],'corp_name'=>(string)$extUser['corp_name'],'corp_full_name'=>(string)$extUser['corp_full_name'],'insert_time'=>$extUser['created_at'],'update_time'=>$extUser['updated_at'],'custom_uid'=>0];
  85. // 手机号
  86. $phone = '';
  87. // 循环跟进客服
  88. foreach ( $followList as $follow ) {
  89. // 标签处理
  90. $this->tagsHandle($extUser['id'],$follow['staff_id'],$follow['tags'],$Tags);
  91. // 客服处理
  92. $this->staffHandle($follow,$extUser,$Follow);
  93. // 获取手机号
  94. $phone = $follow['phone_number'];
  95. }
  96. // 如果没有客服,状态流失
  97. if( !$followList ) $extUser['status'] = 4;
  98. // 判断客户是否存在
  99. $oldExtUser = $External->query()->find($extUser['id'],['custom_uid']);
  100. // 如果存在账号的话获取客户UID
  101. if( $oldExtUser ) $extUser['custom_uid'] = ($oldExtUser->custom_uid);
  102. // 存在手机号,才创建账号
  103. if( $phone ) $extUser['custom_uid'] = $this->customHandle($phone,$extUser,$Custom);
  104. // 新增或者修改
  105. $External->query()->upsert($extUser,'id',['name','avatar','gender','type','corp_name','corp_full_name','status','custom_uid','update_time']);
  106. }
  107. /**
  108. * 标签处理
  109. */
  110. private function tagsHandle($extId,$staffId,$tagList,WeiBanTags $Tags){
  111. // 如果标签不存在,删除客服给客户的标签
  112. if( !$tagList ) return $Tags->query()->where([['weiban_extid','=',$extId],['staff_id','=',$staffId]])->delete();
  113. // 查询客户的标签
  114. $oldTags = $Tags->getListByExtStaff($extId,$staffId);
  115. // 循环标签数据
  116. foreach ($tagList as $k=>$tag) {
  117. // 标签数据
  118. $tag['id'] = 0;
  119. // 获取结果
  120. foreach ($oldTags as $oldtag) {
  121. // 如果有相同的话,获取ID
  122. if( $oldtag['name'] == $tag['name'] && $oldtag['group'] == $tag['group'] ) $tag['id'] = $oldtag['id'];
  123. }
  124. // 如果没有ID
  125. if( !$tag['id'] ) {
  126. // 返回结果
  127. $tag['id'] = $Tags->add(['name'=>$tag['name'],'group'=>$tag['group'],'weiban_extid'=>$extId,'staff_id'=>$staffId]);
  128. }
  129. // 重组
  130. $follow['tags'][$k] = $tag;
  131. }
  132. // 如果不在标签内的,删除
  133. if( $follow['tags'] ) $Tags->query()->where([['weiban_extid','=',$extId],['staff_id','=',$staffId]])->whereNotIn('id',array_column($follow['tags'],'id'))->delete();
  134. // 返回结果
  135. return true;
  136. }
  137. /**
  138. * 注册处理
  139. */
  140. private function customHandle($phone,$extUser,Custom $Custom){
  141. // 是否已经注册
  142. $custom = $Custom->getOneByPhone($phone);
  143. // 如果已经注册
  144. $uid = $custom ? $Custom->edit($custom['uid'],['username'=>$extUser['name'],'userpic'=>$extUser['avatar'],'sex'=>$extUser['gender'],'weiban_extid'=>$extUser['id']]) : $Custom->add(['phone'=>$phone,'username'=>$extUser['name'],'userpic'=>$extUser['avatar'],'sex'=>$extUser['gender'],'weiban_extid'=>$extUser['id']]);
  145. // 成功,赋值
  146. return $uid;
  147. }
  148. /**
  149. * 客服处理
  150. */
  151. private function staffHandle($follow,$extUser,WeiBanFollow $Follow){
  152. // 备注手机号,如果存在,解析成数组,转字符串
  153. $follow['remark_mobiles'] = $follow['remark_mobiles'] ? implode(',',json_decode($follow['remark_mobiles'],true)): '';
  154. // 获取必要数据
  155. $follow = [
  156. 'staff_id'=>(string)$follow['staff_id'],
  157. 'staff_name'=>str_ireplace('http://','https://',(string)$follow['staff_name']),
  158. 'staff_avatar'=>(string)$follow['staff_avatar'],
  159. 'phone_number'=>(string)$follow['phone_number'],
  160. 'remark'=>(string)$follow['remark'],
  161. 'remark_state'=>(string)$follow['remark_state'],
  162. 'remark_corp_name'=>(string)$follow['remark_corp_name'],
  163. 'remark_mobiles'=>(string)$follow['remark_mobiles'],
  164. 'state_name'=>(string)$follow['state_name'],
  165. 'state_text'=>(string)$follow['state_text'],
  166. 'state_type'=>(string)$follow['state_type'],
  167. 'deleted_by'=>(string)$follow['deleted_by'],
  168. // 员工删除客户1,客户删除的2
  169. 'status'=>( $follow['deleted'] ? 1 : ( $follow['deleted_each_other'] ? 2 : 0)),
  170. 'weiban_extid'=>$extUser['id'],
  171. ];
  172. // 如果没有企微企业,使用客服备注的企业
  173. if( !$extUser['corp_name']) $extUser['corp_name'] = $follow['remark_corp_name'];
  174. // 判断客户是否跟进中
  175. $followId = $Follow->query()->where([['weiban_extid','=',$extUser['id']],['staff_id','=',$follow['staff_id']]])->value('id');
  176. // 有则更新,无则增加
  177. $followId ? $Follow->edit($followId,$follow) : $Follow->add($follow);
  178. }
  179. }