SendNoticeJobs.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Jobs\Manager\Process;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Facades\Servers\Logs\Log;
  10. use App\Models\Manager\Process\SubNoticeLog as SubNoticeLogModel;
  11. use App\Models\Manager\Process\StatisticsNotices as StatisticsNoticesModel;
  12. use App\Servers\Email\VerifyCode as EmailVerifyCode;
  13. use App\Servers\Sms\VerifyCode as SmsVerifyCode;
  14. /**
  15. * 发送订阅通知
  16. * @author 唐远望
  17. * @version 1.0
  18. * @date 2026-04-11
  19. */
  20. class SendNoticeJobs implements ShouldQueue
  21. {
  22. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  23. protected $message_data;
  24. protected $user_data;
  25. /**
  26. * Create a new job instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct(array $message_data)
  31. {
  32. $this->message_data = $message_data;
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. try {
  42. $company_id = $this->message_data['company_id'];
  43. $SubNoticeLogModel = new SubNoticeLogModel();
  44. //查询需要发送的通知,push_time等于当前时间,或者大于当前时间5分钟的通知
  45. $now_time = time();
  46. $sub_notice_list = $SubNoticeLogModel->where(['company_id' => $company_id])->whereOr([['push_time', '=', $now_time], ['push_time', '>', $now_time - 300]])->get()->toarray();
  47. if (empty($sub_notice_list)) return true;
  48. //执行发送站内信
  49. $this->send_internal_msg($sub_notice_list);
  50. //执行发送邮件内容
  51. $this->send_email_content($sub_notice_list);
  52. //执行发送短信记录
  53. // $this->send_sms_record($sub_notice_list);
  54. } catch (\Exception $e) {
  55. Log::info('job_error', '发送订阅通知队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  56. }
  57. }
  58. /**
  59. * 发送站内信通知
  60. * @author 唐远望
  61. * @version 1.0
  62. * @date 2026-04-11
  63. * @param $data
  64. * @return bool
  65. */
  66. public function send_internal_msg($sub_notice_list)
  67. {
  68. $StatisticsNoticesModel = new StatisticsNoticesModel();
  69. $SubNoticeLogModel = new SubNoticeLogModel();
  70. $notices_data = [];
  71. foreach ($sub_notice_list as $key => $value) {
  72. if ($value['internal_notice_status'] == 0) {
  73. continue;
  74. }
  75. $notices_data[] = [
  76. 'company_id' => $value['company_id'],
  77. 'employee_id' => $value['custom_uid'],
  78. 'title' => $value['internal_notice_content'],
  79. 'ext_data' => $value['ext_data'],
  80. 'insert_time' => time()
  81. ];
  82. }
  83. //发送站内信通知
  84. $StatisticsNoticesModel->insert($notices_data);
  85. //更新发送状态
  86. $SubNoticeLogModel->whereIn('id', array_column($sub_notice_list, 'id'))->update(['internal_notice_status' => 0]);
  87. }
  88. /**
  89. * 执行发送邮件内容
  90. * @author 唐远望
  91. * @version 1.0
  92. * @date 2026-04-11
  93. * @param $data
  94. * @return bool
  95. */
  96. public function send_email_content($sub_notice_list)
  97. {
  98. $EmailVerifyCode = new EmailVerifyCode();
  99. $SubNoticeLogModel = new SubNoticeLogModel();
  100. foreach ($sub_notice_list as $key => $value) {
  101. if ($value['email_status'] == 0) {
  102. continue;
  103. }
  104. $email_to = $value['email'];
  105. $email_content = json_decode($value['email_content'], true);
  106. $email_title = $email_content['email_title'];
  107. $email_content = $email_content['email_content'];
  108. $res_msg = $EmailVerifyCode->sendSmtpEmail($email_to, $email_title, $email_content);
  109. Log::info('job_send_email', '订阅邮件通知推送队列记录', ['email' => $email_to, 'email_content' => $email_content, 'msg' => $res_msg]);
  110. //更新发送状态
  111. $SubNoticeLogModel->where(['id' => $value['id']])->update(['email_status' => 0]);
  112. }
  113. }
  114. /**
  115. * 执行发送短信记录
  116. * @author 唐远望
  117. * @version 1.0
  118. * @date 2026-04-11
  119. * @param $data
  120. * @return bool
  121. */
  122. public function send_sms_record($sub_notice_list)
  123. {
  124. $SmsVerifyCode = new SmsVerifyCode();
  125. $SubNoticeLogModel = new SubNoticeLogModel();
  126. foreach ($sub_notice_list as $key => $value) {
  127. if ($value['status'] == 0) {
  128. continue;
  129. }
  130. $mobile = $value['mobile'];
  131. $sms_content = json_decode($value['sms_content'], true);
  132. $totle_number = $sms_content['totle_number'];
  133. $number1 = $sms_content['number1'];
  134. $number2 = $sms_content['number2'];
  135. $number3 = $sms_content['number3'];
  136. $sms_tpl_id = $sms_content['sms_tpl_id'];
  137. $res_msg = $SmsVerifyCode->sendContent($mobile, ['totle_number' => $totle_number, 'number1' => $number1, 'number2' => $number2, 'number3' => $number3], $sms_tpl_id);
  138. Log::info('job_send_sms', '订阅短信通知推送队列记录', ['email' => $mobile, 'sms_tpl_id' => $sms_tpl_id, 'msg' => $res_msg]);
  139. //更新发送状态
  140. $SubNoticeLogModel->where(['id' => $value['id']])->update(['status' => 0]);
  141. }
  142. }
  143. public function failed(\Throwable $exception)
  144. {
  145. Log::info('job_error', '发送订阅通知队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  146. }
  147. }