message->id; } /** * 获取唯一任务锁的缓存驱动程序。 * * @return Repository */ public function uniqueVia(): Repository { return Cache::driver('redis'); } /** * Send WeChat Message Job Construct. * * @param SendMessageJob $message */ public function __construct(SendMessageJob $message) { $this->message = $message->withoutRelations(); $this->message_type = MessageType::from($this->message->type); } /** * Execute the job. */ public function handle(): void { $content = json_decode($this->message->content, true); try { $response = match ($this->message_type) { MessageType::TEMPLATE, MessageType::SINGLE_SUBSCRIBE, MessageType::SUBSCRIBE, MessageType::CUSTOM => (new SubscriptionMessage())->send($this->message_type, $content), }; $data = $response->toArray(false); if ($response->isSuccessful()) { $this->successful((string)$data['errcode']); } else { $this->retry((string)$data['errmsg']); } } catch (Exception|Throwable $e) { $message = $e->getMessage().' in '.$e->getFile().':'.$e->getLine(); Log::channel('SendWeChatMessage')->info('Push WeChat Message Error', [$this->message->id, $message]); $this->retry($e->getMessage()); } } /** * Handle a job successful. * * @param string $msg_id * * @return void */ public function successful(string $msg_id): void { $this->message->status = MessageStatus::SUCCESS->value; $this->message->msg_id = $msg_id; $this->message->sent_at = date('Y-m-d H:i:s'); $this->message->save(); } /** * Handle a job retry. * * @param string $fail_reason * @return void */ public function retry(string $fail_reason): void { if ($this->message->number < 3) { ++$this->message->number; $this->message->save(); $this->release(10); } else { $this->failed($fail_reason); } } /** * Handle a job failure. * * @param string $fail_reason * * @return void */ public function failed(string $fail_reason): void { $this->message->status = MessageStatus::FAILURE->value; $this->message->fail_reason = $fail_reason; $this->message->save(); } }