SmsJob.php 3.5 KB
<?php
/**
 * +--------------------------------------------------------------------------------------------------------------------
 * Job:短信发送 处理业务逻辑
 * +--------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Jobs\Lock
 * @package   App\Jobs\Lock
 * @author    Richer <yangzi1028@163.com>
 * @date      2021年8月23日14:45:49
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Jobs;

use App\Models\System\SmsLog;
use App\Models\Warning;
use App\Models\WarningUser;
use App\Sms\MasSms;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

class SmsJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $model;

    protected $desc =  '发送短信';

    /**
     * Create a new job instance.
     *
     * @param $model
     */
    public function __construct($model)
    {
        $this->model = $model;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Log::channel('queue')->info('====================================== '.$this->desc.' begin ======================================');

        if ($this->attempts() > 3) {
            Log::channel('queue')->info('尝试次数过多');
        } else {
            Log::channel('queue')->info('请求模型对象' . json_encode($this->model));
            // 获取模型
            $model  = $this->model;
            $sms = app(MasSms::class);

            // 短信的类型
            $type = $model->type;
            $loggable = $model->loggable;
            $result = null;
            $mobile = $model->mobile;
            switch ($type) {
                case SmsLog::WARNING_USER:// 火警短信
                    $result = $sms->pushNotification($model, $mobile, Warning::OBJ_NAME, $loggable);
                    $user = $loggable->users()->where('mobile', $mobile)->first();
                    if ($result) {
                        $user->result = WarningUser::PROCESSED_SUCCESS;
                        $user->status = WarningUser::PROCESSED_SUCCESS;
                    } else {
                        $user->result = WarningUser::PROCESSED_FAIL;
                        $user->status = WarningUser::PROCESSED_FAIL;
                        $user->remark = $sms->getMessage();
                    }
                    $user->save();
                    break;
//                    $code = Arr::get($result, 'code');
//
//
//                    if ($code === 0) {
//                        $user->result = WarningUser::PROCESSED_SUCCESS;
//                        $user->status = WarningUser::PROCESSED_SUCCESS;
//                    } else {
//                        $user->result = WarningUser::PROCESSED_FAIL;
//                        $user->status = WarningUser::PROCESSED_FAIL;
//                        $user->remark = Arr::get($result, 'message');
//                    }
//                    $user->save();
                    break;
            }
        }
        Log::channel('queue')->info('====================================== '.$this->desc.' end ======================================');
    }
}