Subscription.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Models\Manager\Personnel\Employee as EmployeeModel;
  10. use App\Facades\Servers\Wechat\Official;
  11. use App\Facades\Servers\Logs\Log;
  12. /**
  13. * 订阅消息通知推送
  14. * @author 唐远望
  15. * @version 1.0
  16. * @date 2026-03-04
  17. */
  18. class Subscription implements ShouldQueue
  19. {
  20. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  21. protected $message_data;
  22. /**
  23. * Create a new job instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct(array $message_data)
  28. {
  29. $this->message_data = $message_data;
  30. }
  31. /**
  32. * Execute the job.
  33. *
  34. * @return void
  35. */
  36. public function handle()
  37. {
  38. try {
  39. $this->send($this->message_data['user_id_list'], $this->message_data['msg_title']);
  40. } catch (\Exception $e) {
  41. Log::info('job_error', '订阅消息通知推送队列失败', ['data' => $this->message_data, 'error' => $e->getMessage()]);
  42. }
  43. }
  44. /**
  45. * 公众号消息订阅推送
  46. *
  47. * */
  48. private function send($user_id_list, $msg_title = '')
  49. {
  50. $EmployeeModel = new EmployeeModel();
  51. $user_list = $EmployeeModel->whereIn('id',$user_id_list)->where([['open_subscribe', '=', 0], ['status', '=', 1]])->select(['openid'])->select();
  52. if(empty($user_list)) return true;
  53. $data = [
  54. 'thing1' => $msg_title,
  55. 'thing2' => '您有一条新的违规待处理通知要处理,请及时查看',
  56. 'time7' => strtotime(time(), 'Y:m:d H:i:s'),
  57. 'phrase9' => '待处理',
  58. ];
  59. foreach ($user_list as $value) {
  60. $params = [
  61. 'touser' => $value['openid'],
  62. 'template_id' => 't559Iagds7Av-YcqwIpeAaS5gt7LuOKuIBDvVKlyfm8',
  63. 'url' => '',
  64. 'data' => $data,
  65. ];
  66. //发送模板消息
  67. $result = Official::sendSubscription($params);
  68. }
  69. // 告知结果
  70. return json_send(['code' => 'success', 'msg' => '成功', 'path' => '']);
  71. }
  72. public function failed(\Throwable $exception)
  73. {
  74. Log::info('job_error', '订阅消息通知推送队列完全失败', ['data' => $this->message_data, 'error' => $exception]);
  75. }
  76. }