SmsController.php
3.22 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
<?php
namespace App\Http\Container\ProviderSection\Controllers;
use App\Helpers\SmsHelper;
use App\Http\Container\ProviderSection\Requests\SmsRequest;
use App\Support\BusinessCode;
use Hikoon\LaravelApi\Exceptions\ValidationException;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Hikoon\LaravelApi\Support\ApiController;
class SmsController extends ApiController
{
/**
* @throws \Exception
*/
public function __invoke(SmsRequest $request): Response
{
$type = $request->offsetGet('type');
if (!SmsHelper::checkPhoneLimit($request->phoneNumber)) {
return $this->fail(BusinessCode::SMS_CODE_LIMIT);
}
method_exists($this, $type) && $this->$type($request);
return $this->success(ApiCode::CREATE_SUCCESS, '发送成功');
}
/**
* @param \App\Http\Container\ProviderSection\Requests\SmsRequest $request
* @throws \Hikoon\LaravelApi\Exceptions\ValidationException
* @throws \RedisException
*/
protected function info(SmsRequest $request): void
{
if (!$request->checkPhoneExist()) {
throw new ValidationException(BusinessCode::ACCOUNT_NOT_EXISTS);
}
$request->sendSms(['template' => $request->phoneNumber->inChineseMainland() ? 'SMS_195310252' : 'SMS_234031047', 'content' => '验证码${code},您正在尝试变更重要信息,请妥善保管账户信息。']);
}
/**
* @param \App\Http\Container\ProviderSection\Requests\SmsRequest $request
* @return void
* @throws \Hikoon\LaravelApi\Exceptions\ValidationException
* @throws \RedisException
*/
protected function changePhone(SmsRequest $request): void
{
if ($request->checkPhoneExist()) {
throw new ValidationException(BusinessCode::PHONE_EXISTS);
}
$request->sendSms(['template' => $request->phoneNumber->inChineseMainland() ? 'SMS_195310252' : 'SMS_234031047', 'content' => '验证码${code},您正在尝试变更重要信息,请妥善保管账户信息。']);
}
/**
* @param \App\Http\Container\ProviderSection\Requests\SmsRequest $request
* @throws \Hikoon\LaravelApi\Exceptions\ValidationException
* @throws \RedisException
*/
protected function register(SmsRequest $request): void
{
if ($request->checkPhoneExist()) {
throw new ValidationException(BusinessCode::PHONE_EXISTS);
}
$request->sendSms(['template' => $request->phoneNumber->inChineseMainland() ? 'SMS_195310254' : 'SMS_219400324', 'content' => '验证码${code},您正在注册成为新用户,感谢您的支持!']);
}
/**
* @param \App\Http\Container\ProviderSection\Requests\SmsRequest $request
* @throws \Hikoon\LaravelApi\Exceptions\ValidationException
* @throws \RedisException
*/
protected function login(SmsRequest $request): void
{
if (!$request->checkPhoneExist()) {
throw new ValidationException(BusinessCode::ACCOUNT_NOT_EXISTS);
}
$request->sendSms(['template' => $request->phoneNumber->inChineseMainland() ? 'SMS_195310256' : 'SMS_223175300', 'content' => '验证码${code},您正在登录,若非本人操作,请勿泄露。']);
}
}