QrcodeWelcomeJobs.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Jobs\OpenWork\External;
  3. use App\Servers\DB\DbService;
  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\Logs\Log;
  11. use App\Models\OpenWork\job\Records;
  12. use App\Facades\Servers\Wechat\OpenWork;
  13. use App\Models\OpenWork\Contactway\QrcodeWelcome as QrcodeWelcomeModel;
  14. use App\Models\OpenWork\External\Welcome as WelcomeModel;
  15. /**
  16. * 渠道活码欢迎语任务队列
  17. * @author 唐远望
  18. * @version 1.0
  19. * @date 2025-11-12
  20. *
  21. */
  22. class QrcodeWelcomeJobs implements ShouldQueue
  23. {
  24. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  25. protected $message_data;
  26. protected $Records;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(array $message_data)
  33. {
  34. $this->message_data = $message_data;
  35. }
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. try {
  44. $corpId = $this->message_data['corpid'];
  45. (new DbService())->getConnectionNameByCorpId($corpId);
  46. // 创建任务记录
  47. $this->Records = Records::create([
  48. 'job_id' => $corpId . '_QrcodeWelcomeJobs',
  49. 'name' => static::class,
  50. 'payload' => json_encode($this->message_data),
  51. 'status' => 'processing',
  52. 'started_at' => now()
  53. ]);
  54. $userid = $this->message_data['userid'];
  55. $welcomeCode = $this->message_data['WelcomeCode'];
  56. $state = $this->message_data['state'];
  57. $external_userid = $this->message_data['external_userid'];
  58. $QrcodeWelcomeModel = new QrcodeWelcomeModel();
  59. $WelcomeModel = new WelcomeModel();
  60. $work = OpenWork::getWork($corpId);
  61. $extUser = $work->external_contact->get($external_userid);
  62. if (!$extUser || $extUser['errcode']) {
  63. Log::info('job_error', 'QrcodeWelcomeJobs获取外部人详情错误:', ['data' => $external_userid, 'error' => $extUser]);
  64. $this->Records->delete();
  65. return true;
  66. }
  67. $external_user_name = $extUser['external_contact']['name'];
  68. $msg = [];
  69. $isQrcodeWelcomeFlag = 0; // 是否有渠道活码欢迎语
  70. if (!empty($state)) {
  71. //解析state
  72. $qrcodeExplode = explode('&', $state); //t1&qrcodeId
  73. if (isset($qrcodeExplode[1]) && $qrcodeExplode[0] == 't1') {
  74. $qrcodeId = $qrcodeExplode[1];
  75. //通过qrcodeId 获取tags
  76. $qrcodeWelcome = $QrcodeWelcomeModel->getOneByQrcodeId($corpId, $qrcodeId, '');
  77. //如果存在欢迎语
  78. if ($qrcodeWelcome) {
  79. if ($qrcodeWelcome['content'] || $qrcodeWelcome['attachments']) {
  80. // 欢迎语文本消息
  81. if ($qrcodeWelcome['content']) $msg['text'] = ['content' => $qrcodeWelcome['content']];
  82. //组合附件消息
  83. if ($qrcodeWelcome['attachments']) $msg['attachments'] = $qrcodeWelcome['attachments'];
  84. $isQrcodeWelcomeFlag = 1;
  85. }
  86. }
  87. }
  88. }
  89. //如果没有渠道活码的欢迎语则使用成员对应的欢迎语
  90. if ($isQrcodeWelcomeFlag == 0) {
  91. // 获取成员对应的欢迎语
  92. $userWelcome = $WelcomeModel->getOneByUserId($corpId, $userid, '');
  93. // 如果存在欢迎语
  94. if ($userWelcome) {
  95. // 内容和图片不能同时为空
  96. if ($userWelcome['content'] || $userWelcome['attachments']) {
  97. // 欢迎语文本消息
  98. if ($userWelcome['content']) $msg['text'] = ['content' => $userWelcome['content']];
  99. // 组合附件
  100. if ($userWelcome['attachments']) $msg['attachments'] = $userWelcome['attachments'];
  101. }
  102. }
  103. }
  104. // 发送欢迎语
  105. if ($msg) {
  106. if (!empty($msg['text']['content'])) $msg['text']['content'] = str_ireplace('{客户昵称}', $external_user_name, $msg['text']['content']);
  107. $msgResult = $work->external_contact_message->sendWelcome($welcomeCode, $msg);
  108. if ($msgResult['errcode']) {
  109. Log::info('job_error', 'QrcodeWelcomeJobs任务队列处理失败', ['data' => $this->message_data, 'error' => $msgResult]);
  110. }
  111. }
  112. //删除任务记录
  113. $this->Records->delete();
  114. // 成功处理...
  115. } catch (\Exception $e) {
  116. // 失败处理...
  117. if ($this->Records) {
  118. $this->Records->delete();
  119. }
  120. Log::info('job_error', 'QrcodeWelcomeJobs任务队列处理失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  121. }
  122. }
  123. public function failed(\Throwable $exception)
  124. {
  125. Log::info('job_error', 'QrcodeWelcomeJobs任务队列彻底失败', ['data' => $this->message_data, 'error' => $exception->getMessage()]);
  126. // 失败处理...
  127. if ($this->Records) {
  128. $this->Records->delete();
  129. }
  130. }
  131. }