暫無描述

ExternalUserService.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Service;
  3. class ExternalUserService
  4. {
  5. /**
  6. * 外部联系人变更事件回调处理
  7. * */
  8. public static function changeExternalContactEvent($msgObj)
  9. {
  10. try {
  11. $changeType = trim($msgObj->ChangeType);
  12. $corpid = trim($msgObj->ToUserName);
  13. switch($changeType) {
  14. case 'add_external_contact': // 添加企业客户事件
  15. case 'edit_external_contact': // 编辑客户事件
  16. case 'add_half_external_contact': // 外部联系人免验证添加成员事件
  17. $data['corpid'] = $corpid;
  18. $data['user_id'] = trim($msgObj->UserID);
  19. $data['external_userid'] = trim($msgObj->ExternalUserID);
  20. $data['state'] = trim($msgObj->State);
  21. $data['change_type'] = $changeType; // 添加变更类型
  22. $welcomeCode = trim($msgObj->WelcomeCode);
  23. $createTime = trim($msgObj->CreateTime);
  24. # 异步获取客户信息
  25. // $rdsKey = Customer::CUSTOMER_UPDATE_WAITING_TEMPLATE_RDS;
  26. // $result = RedisModel::lPush($rdsKey, json_encode($data));
  27. //
  28. // if(!$result) {
  29. // Log::logError('新添加客户信息入队列失败', $data, 'UpdateExternalContact');
  30. // return false;
  31. // }
  32. # 获取客户信息
  33. $result = CustomerService::updateExternalInfo(
  34. $corpid, $data['user_id'], $data['external_userid'], $data['state'], $changeType
  35. );
  36. if(!$result) {
  37. Log::logError('外部联系人详情信息获取失败', [
  38. 'data' => $data
  39. ], 'ChangeExternalContactEvent');
  40. }
  41. # 渠道码处理入队列
  42. RedisModel::lPush(SourceQrcodes::SOURCE_QRCODE_DEAL_RDS, json_encode([
  43. 'corpid' => $corpid,
  44. 'user_id' => $data['user_id'],
  45. 'external_userid' => $data['external_userid'],
  46. 'state' => $data['state'],
  47. 'change_type' => $changeType,
  48. 'create_time' => $createTime
  49. ]));
  50. if($changeType != 'edit_external_contact') {
  51. # 添加客户动态信息
  52. $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 1);
  53. if(!$result) {
  54. Log::logError('(添加客户)客户动态信息存储失败', $data, 'CustomerDynamicSave');
  55. }
  56. # 客户充值信息入队列
  57. $result = RedisModel::lPush(Customer::CUSTOMER_PAID_INFO_RDS, json_encode(['corpid' => $corpid, 'external_userid' => $data['external_userid']]));
  58. if(!$result) {
  59. Log::logError('(添加客户)客户充值信息入队列失败', $data, 'CustomerPaidInfoDeal');
  60. }
  61. # 客户信息入队列以处理绑定投放相关数据
  62. $result = RedisModel::lPush(Customer::CUSTOMER_AD_RELATION_RDS, json_encode([
  63. 'corpid' => $corpid,
  64. 'external_userid' => $data['external_userid'],
  65. 'user_id' => $data['user_id']
  66. ]));
  67. if(!$result) {
  68. Log::logError('(添加客户)处理绑定投放相关数据入队列失败', $data, 'CustomerAdRelationDeal');
  69. }
  70. }
  71. if('add_external_contact' == $changeType) { // 客服客户数+1
  72. RedisModel::hIncrby(CustomerDetails::EXTERNAL_CONTACT_COUNT_RDS, $corpid.'##'.$data['user_id'], 1);
  73. }
  74. # 放入客服号总粉丝数预警队列中
  75. RedisModel::lPush(AbnormalAccountWarnConf::USER_SCALE_WARN_USER_LIST, json_encode([
  76. 'corpid' => $corpid, 'user_id' => $data['user_id']
  77. ]));
  78. break;
  79. case 'del_external_contact': // 删除企业客户事件
  80. $data['corpid'] = $corpid;
  81. $data['user_id'] = trim($msgObj->UserID);
  82. $data['external_userid'] = trim($msgObj->ExternalUserID);
  83. $data['source'] = trim($msgObj->Source);
  84. $result = ExternalContactService::deleteExternalContact(
  85. $corpid, $data['user_id'], $data['external_userid'], $data['source']
  86. );
  87. // $result = RedisModel::lPush(Customer::DEL_CUSTOMER_RDS, json_encode($data));
  88. if(!$result) {
  89. Log::logError('删除企业客户事件处理失败', $data, 'DeleteExternalContact');
  90. return false;
  91. }
  92. # 添加成员动态信息
  93. $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 2);
  94. if(!$result) {
  95. Log::logError('(删除企业客户)客户动态信息存储失败', $data, 'CustomerDynamicSave');
  96. }
  97. # 客服客户数-1
  98. RedisModel::hIncrby(CustomerDetails::EXTERNAL_CONTACT_COUNT_RDS, $corpid.'##'.$data['user_id'], -1);
  99. break;
  100. case 'del_follow_user': // 删除跟进成员事件
  101. $data['corpid'] = $corpid;
  102. $data['user_id'] = trim($msgObj->UserID);
  103. $data['external_userid'] = trim($msgObj->ExternalUserID);
  104. $result = ExternalContactService::deleteFollowUser($corpid, $data['user_id'], $data['external_userid']);
  105. // $result = RedisModel::lPush(Customer::DEL_FOLLOW_USER_RDS, json_encode($data));
  106. if(!$result) {
  107. Log::logError('删除跟进成员通知处理失败', $data, 'DeleteFollowUser');
  108. return false;
  109. }
  110. # 添加成员动态信息
  111. $result = CustomerDynamic::customerDynamicSave($corpid, $data['external_userid'], $data['user_id'], 3);
  112. if(!$result) {
  113. Log::logError('(删除跟进成员)客户动态信息存储失败', $data, 'CustomerDynamicSave');
  114. }
  115. break;
  116. case 'customer_refused': // 客户接替失败事件
  117. $data['corpid'] = $corpid;
  118. $data['user_id'] = trim($msgObj->UserID);
  119. $data['external_userid'] = trim($msgObj->ExternalUserID);
  120. $data['fail_reason'] = trim($msgObj->FailReason);
  121. $result = RedisModel::lPush(Customer::CUSTOMER_REFUSED_RDS, json_encode($data));
  122. if(!$result) {
  123. Log::logError('新添加客户信息入队列失败', $data, 'CustomerRefused');
  124. return false;
  125. }
  126. break;
  127. }
  128. } catch (\Exception $e) {
  129. Log::logError('外部联系人变更事件回调处理发生异常', [
  130. 'line' => $e->getLine(),
  131. 'msg' => $e->getMessage(),
  132. 'data' => $msgObj
  133. ], 'ChangeExternalContactEvent-Exception');
  134. return false;
  135. }
  136. return true;
  137. }
  138. }