企微短剧业务系统

TemplateCallbackService.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\AuthorizeCorp;
  5. use App\Models\CustomerDetails;
  6. use App\Models\DjUser;
  7. use App\RedisModel;
  8. use App\Service\ChatGroup\ChatGroupService;
  9. class TemplateCallbackService
  10. {
  11. /**
  12. * 回调数据处理
  13. * */
  14. public static function responseCommand($msgStr)
  15. {
  16. try {
  17. if(!empty($msgStr)) {
  18. $msgObj = simplexml_load_string($msgStr, 'SimpleXMLElement', LIBXML_NOCDATA);
  19. $infoType = trim($msgObj->Event);
  20. if(empty($infoType)) {
  21. $infoType = trim($msgObj->InfoType);
  22. }
  23. $suiteId = trim($msgObj->SuiteId);
  24. $suiteTicket = trim($msgObj->SuiteTicket);
  25. switch ($infoType) {
  26. case 'suite_ticket': // 推送suite_ticket
  27. $result = MsgService::receiveSuiteTicket($suiteId, $suiteTicket);
  28. break;
  29. case 'create_auth': // 企业微信应用市场发起授权场景下授权成功通知
  30. $component = config('qyWechat.suite_info');
  31. $suiteSecret = $component['app_template_secret'];
  32. $authCode = trim($msgObj->AuthCode);
  33. $result = RedisModel::lPush(AuthorizeCorp::AUTH_CORP_RDS, json_encode(array('corpid' => $suiteId, 'secret' => $suiteSecret, 'auth_code' => $authCode)));
  34. break;
  35. case 'change_contact': // 通讯录事件回调
  36. $result = RedisModel::lPush(DjUser::CHANGE_CONTACT_RDS, json_encode(['data' => $msgStr]));
  37. if(!$result) {
  38. Log::logError('通讯录事件回调入队列失败', [
  39. 'data' => $msgStr
  40. ], 'ChangeContact');
  41. }
  42. // $result = ContactService::changeContactEvent($msgObj);
  43. break;
  44. case 'change_external_contact': // 外部客户事件回调
  45. $result = RedisModel::lPush(CustomerDetails::CHANGE_EXTERNAL_CONTACT_RDS, json_encode(['data' => $msgStr]));
  46. Log::logInfo('回调数据插入【Playlet::ChangeExternalContactList】队列完成', ['data' => $msgStr]);
  47. if(!$result) {
  48. Log::logError('外部客户事件回调入队列失败', [
  49. 'data' => $msgStr
  50. ], 'ChangeExternalContact');
  51. }
  52. // $result = ExternalUserService::changeExternalContactEvent($msgObj);
  53. break;
  54. case 'change_external_chat':
  55. $result = ChatGroupService::templateCallBack($msgStr);
  56. // $result = RedisModel::lPush(CustomerDetails::CHANGE_EXTERNAL_CONTACT_RDS, json_encode(['data' => $msgStr]));
  57. // Log::logInfo('回调数据插入【Playlet::ChangeExternalChatList】队列完成', ['data' => $msgStr]);
  58. if(!$result) {
  59. Log::logError('客户群变更事件回调处理失败', [
  60. 'data' => $msgStr
  61. ], 'ChangeExternalChat');
  62. }
  63. break;
  64. case 'change_external_tag':
  65. $result = TagService::templateCallBack($msgStr);
  66. break;
  67. default:
  68. $result = true;
  69. }
  70. } else {
  71. # 写入日志
  72. Log::logError($msgStr, [], 'responseTemplate');
  73. return false;
  74. }
  75. } catch (\Exception $e) {
  76. Log::logError('处理回调数据发生异常', [
  77. 'line' => $e->getLine(),
  78. 'msg' => $e->getMessage(),
  79. 'data' => $msgStr
  80. ], 'responseTemplate');
  81. return false;
  82. }
  83. return $result;
  84. }
  85. }