SendNoticeJobs.php 7.0 KB

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