SmsJob.php
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?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 ======================================');
}
}