123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Jobs\News;
- use App\Jobs\Job;
- use App\Mail\NoticeMail;
- use App\Repositories\Enums\News\SendStatusEnum;
- use App\Repositories\Enums\News\SendTypeEnum;
- use App\Repositories\Models\Base\Admin;
- use App\Repositories\Models\News\Message;
- use App\Repositories\Models\News\UserMessage;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Mail;
- class SendMessageJob extends Job
- {
- private $model;
- /**
- * Create a new job instance.
- */
- public function __construct($message_id)
- {
- $this->model = Message::query()->find($message_id);
- $this->delay(Carbon::parse($this->model->send_time));
- }
- /**
- * 确定任务应该超时的时间
- *
- * @return \DateTime
- */
- public function retryUntil()
- {
- return Carbon::now()->addHours(8);
- }
- /**
- * Execute the job.
- */
- public function handle()
- {
- $model = Message::query()->find($this->model->id);
- if (!$model) return false;
- if ($model->status != SendStatusEnum::wait) return false;
- $model->status = SendStatusEnum::sending;
- $model->save();
- $sendTypes = $model->send_type;
- $message = false;
- switch ($model->type) {
- case 1:
- $message = $model->message;
- break;
- case 2:
- $message = $model->resource->name;
- break;
- }
- $users = Admin::query()->whereIn('id', $model->user_ids)->select(['id', 'mobile', 'email'])->get();
- //发送消息
- foreach ($users as $user) {
- if (in_array(SendTypeEnum::system, $sendTypes)) {
- //系统消息通知
- UserMessage::query()->create([
- 'admin_id' => $user->id,
- 'message' => $message,
- 'type' => $model->type,
- 'message_id' => $model->id,
- 'resource_type' => $model->resource_type,
- 'resource_id' => $model->resource_id,
- ]);
- }
- if (in_array(SendTypeEnum::mail, $sendTypes)) {
- //邮箱通知
- Mail::to($user['email'])->send(new NoticeMail('消息通知', $message));
- }
- if (in_array(SendTypeEnum::sms, $sendTypes)) {
- //短信
- }
- }
- $model->status = SendStatusEnum::ok;
- $model->save();
- return true;
- }
- }
|