SmsService.php
5.4 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 逻辑层:短信 服务类,处理业务逻辑
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Services
* @package App\Services
* @author Richer <yangzi1028@163.com>
* @date 2020年11月12日 08:35:04
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Services;
use App\Http\Requests\SmsRequest;
use App\Models\System\SmsLog;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
/**
* Class SmsService.
*
* @category App\Services
* @package App\Services
* @author Richer <yangzi1028@163.com>
* @date 2020年11月12日 08:35:04
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class SmsService extends BaseService
{
/**
* SmsService constructor.
*
* @param SmsLog $model
*/
public function __construct(SmsLog $model)
{
// 执行父类构造方法
parent::__construct($model);
}
/**
* 短信回执.
*
* @param Request $request
*
* @return bool|Response
*/
public function notify($request)
{
// {"errorCode":"DELIVRD","mobile":"15956278880","msgGroup":"0826134700000001144529","receiveDate":"20210826134700","reportStatus":"CM:0000","submitDate":"20210826134700"}
Log::channel('sms')->info('========================= MAS 短信回执 begin =========================');
Log::channel('sms')->info(json_encode($request->all()));
$model = $this->model->where('out_id', $request->msgGroup)->first();
if ($model) {
$model->noticed_body = $request->all();
$model->noticed_at = now()->toDateTimeString();
$model->save();
}
Log::channel('sms')->info('========================= MAS 短信回执 end =========================');
return true;
}
/**
* Store a newly created resource in storage.
*
* @param SmsRequest $request
*
* @return bool|Response
*/
public function store(SmsRequest $request)
{
$user = $this->getLoginUser();
$mobile = $request->mobile ?? optional($user)->mobile;
if (!$mobile) {
$this->message ='请填写手机号!';
return false;
}
// type:1 注册,2:登录,3找回密码
$type = $request->type;
// 验证是否频繁发送
if (!$this -> verifyIsSentFrequently($mobile, $type)) {
return false;
}
// 生成验证码
$code = self::generateCode();
// 发送短信、
$sms = app(\App\Sms\Sms::class);
$result = $sms->pushSmsCode($mobile, $code);
// 验证发送结果
if ($result['status'] == 'success') {
// 发送成功
// 用于验证是否频繁发送
\Cache::add(self::generateCacheKey($mobile, $type, 'sendtime'), time(), SmsLog::INTERVALS);
// 缓存中增加该手机号的验证码,保存时间为发送间隔时间
\Cache::add(self::generateCacheKey($mobile, $type, $code), $code, SmsLog::INVALID_TIME);
// 发送记录入库,方便统计和查询,可以放在缓存中进行校验,不查询数据
// 发送记录入库并返回记录
$request['code'] = $code;
$request['send_time'] = date('Y-m-d H:i:s');
return $this->create($request);
}
$this->message = $sms->getMessage();
return false;
}
/**
* 生成缓存key
*
* @param string $mobile
* @param string $type
* @return string
*/
private static function generateCacheKey($mobile, $type, $suffix)
{
return 'sms_' . $mobile . '_' . $type .'-'.$suffix;
}
/**
* 验证
*
* @param string $mobile
* @param string $code
* @param string $type
* @return boolean
*/
public static function checkCode($mobile = '', $code = '', $type = '')
{
// 特殊字符不进行验证
if ($code == '000000') {
return true;
}
// 获取缓存key
$cacheKey = self::generateCacheKey($mobile, $type, $code);
// 获取缓存
$cacheCode = \Cache::get($cacheKey);
if ($cacheCode != $code) {
return false;
}
// 不将缓存中的验证码清空,省短信流量
// 获取成功后,将缓存删除
// Cache::delete($cacheKey);
return true;
}
/**
* 生成随机验证码
*
* @return int
*/
public static function generateCode()
{
return mt_rand(100000, 999999);
}
/**
* 验证是否发生频繁
*
* @param $mobile
* @param $type
* @return bool
*/
public function verifyIsSentFrequently($mobile, $type)
{
$cacheKey = self::generateCacheKey($mobile, $type, 'sendtime');
// 获取缓存
$cacheCode = \Cache::get($cacheKey);
if ($cacheCode) {
$this->message ='操作频繁!';
return false;
}
return true;
}
}