MessageNoticeJob.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\Jobs;
  3. use app\common\facades\EasyWeChat;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Queue\SerializesModels;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. class MessageNoticeJob implements ShouldQueue
  9. {
  10. use InteractsWithQueue, Queueable, SerializesModels;
  11. /**
  12. * The number of times the job may be attempted.
  13. *
  14. * @var int
  15. */
  16. public $tries = 5;
  17. /**
  18. * The number of seconds the job can run before timing out.
  19. *
  20. * @var int
  21. */
  22. public $timeout = 120;
  23. protected $templateId;
  24. protected $noticeData;
  25. protected $openId;
  26. protected $url;
  27. protected $uniacid;
  28. /**
  29. * MessageNoticeJob constructor.
  30. * @param $templateId
  31. * @param $noticeData
  32. * @param $openId
  33. * @param $url
  34. * @param $uniacid
  35. */
  36. public function __construct($templateId, $noticeData, $openId, $url)
  37. {
  38. $this->templateId = $templateId;
  39. $this->noticeData = $noticeData;
  40. $this->openId = $openId;
  41. $this->url = $url;
  42. $this->uniacid = \YunShop::app()->uniacid;
  43. }
  44. /**
  45. * Execute the job.
  46. *
  47. * @return bool
  48. */
  49. public function handle()
  50. {
  51. try {
  52. \YunShop::app()->uniacid = \Setting::$uniqueAccountId = $this->uniacid;
  53. if ($this->attempts() > 1) {
  54. \Log::info('消息通知测试,执行大于两次终止');
  55. return true;
  56. }
  57. $app = EasyWeChat::officialAccount();
  58. $res = $app->template_message->send([
  59. 'touser' => $this->openId,
  60. 'template_id' => $this->templateId,
  61. 'url' => $this->url,
  62. 'data' => $this->noticeData,
  63. ]);
  64. if ($res['errcode']) {
  65. \Log::debug('-------消息发送失败-----',[$res,$this->noticeData]);
  66. }
  67. return true;
  68. } catch (\Exception $e) {
  69. \Log::debug('消息发送失败',$e->getMessage());
  70. }
  71. }
  72. }