SendNoticeJobs.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. use Illuminate\Support\Facades\DB;
  15. /**
  16. * 发送订阅通知
  17. * @author 唐远望
  18. * @version 1.0
  19. * @date 2026-04-11
  20. */
  21. class SendNoticeJobs implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. protected $message_data;
  25. protected $user_data;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(array $message_data)
  32. {
  33. $this->message_data = $message_data;
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. try {
  43. $company_id = $this->message_data['company_id'];
  44. $SubNoticeLogModel = new SubNoticeLogModel();
  45. //查询需要发送的通知,push_time等于当前时间,或者大于当前时间5分钟的通知
  46. $now_time = time();
  47. $sub_notice_list = $SubNoticeLogModel->where('company_id', $company_id)
  48. ->where(function($query) use ($now_time) {
  49. $query->where('push_time', '=', $now_time)
  50. ->orWhere('push_time', '>', $now_time - 300);
  51. })
  52. ->get();
  53. if (empty($sub_notice_list)) return true;
  54. //执行发送站内信
  55. $status1 = $this->send_internal_msg($sub_notice_list);
  56. //执行发送邮件内容
  57. $status2=$this->send_email_content($sub_notice_list);
  58. //执行发送短信记录
  59. $status3=$this->send_sms_record($sub_notice_list);
  60. //获取所有$sub_notice_list ID
  61. $id_list = array_column($sub_notice_list,'id');
  62. if($status1 && $status2 && $status3){
  63. $SubNoticeLogModel->whereIn('id',$id_list)->update(['status'=>'0']);
  64. }
  65. } catch (\Exception $e) {
  66. Log::info('job_error', '发送订阅通知队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  67. }
  68. }
  69. /**
  70. * 发送站内信通知
  71. * @author 唐远望
  72. * @version 1.0
  73. * @date 2026-04-11
  74. * @param $data
  75. * @return bool
  76. */
  77. public function send_internal_msg($sub_notice_list)
  78. {
  79. $StatisticsNoticesModel = new StatisticsNoticesModel();
  80. $SubNoticeLogModel = new SubNoticeLogModel();
  81. $notices_data = [];
  82. foreach ($sub_notice_list as $key => $value) {
  83. if ($value['internal_notice_status'] == 0) {
  84. continue;
  85. }
  86. $notices_data[] = [
  87. 'company_id' => $value['company_id'],
  88. 'employee_id' => $value['employee_id'],
  89. 'title' => $value['internal_notice_content'],
  90. 'ext_data' => $value['ext_data'],
  91. 'insert_time' => time()
  92. ];
  93. }
  94. //发送站内信通知
  95. $StatisticsNoticesModel->insert($notices_data);
  96. //更新发送状态
  97. $SubNoticeLogModel->whereIn('id', array_column($sub_notice_list, 'id'))->update(['internal_notice_status' => 0]);
  98. return true;
  99. }
  100. /**
  101. * 执行发送邮件内容
  102. * @author 唐远望
  103. * @version 1.0
  104. * @date 2026-04-11
  105. * @param $data
  106. * @return bool
  107. */
  108. public function send_email_content($sub_notice_list)
  109. {
  110. $EmailVerifyCode = new EmailVerifyCode();
  111. $SubNoticeLogModel = new SubNoticeLogModel();
  112. foreach ($sub_notice_list as $key => $value) {
  113. if ($value['email_status'] == 0) {
  114. continue;
  115. }
  116. $email_to = $value['email'];
  117. if (empty($email_to)) {
  118. $SubNoticeLogModel->where(['id' => $value['id']])->update(['email_status' => 0]);
  119. continue;
  120. }
  121. $email_content = json_decode($value['email_content'], true);
  122. $email_title = $email_content['title'];
  123. $email_content = $email_content['content'];
  124. $res_msg = $EmailVerifyCode->sendSmtpEmail($email_to, $email_title, $email_content);
  125. Log::info('job_send_email', '订阅邮件通知推送队列记录', ['email' => $email_to, 'email_content' => $email_content, 'msg' => $res_msg]);
  126. //更新发送状态
  127. $SubNoticeLogModel->where(['id' => $value['id']])->update(['email_status' => 0]);
  128. }
  129. return true;
  130. }
  131. /**
  132. * 执行发送短信记录
  133. * @author 唐远望
  134. * @version 1.0
  135. * @date 2026-04-11
  136. * @param $data
  137. * @return bool
  138. */
  139. public function send_sms_record($sub_notice_list)
  140. {
  141. $SmsVerifyCode = new SmsVerifyCode();
  142. $SubNoticeLogModel = new SubNoticeLogModel();
  143. foreach ($sub_notice_list as $key => $value) {
  144. if ($value['status'] == 0) {
  145. continue;
  146. }
  147. $mobile = $value['mobile'];
  148. if (empty($mobile)) {
  149. $SubNoticeLogModel->where(['id' => $value['id']])->update(['status' => 0]);
  150. continue;
  151. }
  152. $sms_content = json_decode($value['sms_content'], true);
  153. $totle_number = $sms_content['totle_number'];
  154. $number1 = $sms_content['number1'];
  155. $number2 = $sms_content['number2'];
  156. $number3 = $sms_content['number3'];
  157. $sms_tpl_id = $sms_content['sms_tpl_id'];
  158. $res_msg = $SmsVerifyCode->sendContent($mobile, ['totle_number' => $totle_number, 'number1' => $number1, 'number2' => $number2, 'number3' => $number3], $sms_tpl_id);
  159. Log::info('job_send_sms', '订阅短信通知推送队列记录', ['email' => $mobile, 'sms_tpl_id' => $sms_tpl_id, 'msg' => $res_msg]);
  160. //更新发送状态
  161. $SubNoticeLogModel->where(['id' => $value['id']])->update(['sms_status' => 0]);
  162. }
  163. return true;
  164. }
  165. public function failed(\Throwable $exception)
  166. {
  167. Log::info('job_error', '发送订阅通知队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  168. }
  169. }