|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
|
4
|
|
|
|
|
5
|
|
|
use App\Support\Http\FormRequest; |
|
6
|
|
|
use App\Support\Mob\Sms; |
|
7
|
|
|
|
|
8
|
|
|
class VerifyPhoneNumber extends FormRequest |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Determine if the user is authorized to make this request. |
|
12
|
|
|
* |
|
13
|
|
|
* @return bool |
|
14
|
|
|
*/ |
|
15
|
|
|
public function authorize() |
|
16
|
|
|
{ |
|
17
|
|
|
return true; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Get the validation rules that apply to the request. |
|
22
|
|
|
* |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public function rules() |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
'phone' => 'bail|required|digits:11', |
|
29
|
|
|
'code' => 'required', |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get custom attributes for validator errors. |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function attributes() |
|
39
|
|
|
{ |
|
40
|
|
|
return [ |
|
41
|
|
|
'phone' => '手机号', |
|
42
|
|
|
'code' => '验证码', |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the credentials for verfication. |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getCredentials() |
|
52
|
|
|
{ |
|
53
|
|
|
return array_filter($this->only('phone', 'code', 'zone')) + ['zone' => '86']; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the phone number. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getPhone() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->input('phone'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Verify the phone number and the code. |
|
68
|
|
|
* |
|
69
|
|
|
* @return array credentials |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \App\Exceptions\InvalidInputException |
|
72
|
|
|
* @throws \App\Exceptions\ActionFailureException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function verify() |
|
75
|
|
|
{ |
|
76
|
|
|
$credentials = $this->getCredentials(); |
|
77
|
|
|
|
|
78
|
|
|
if (! $this->shouldPassThrough($credentials)) { |
|
79
|
|
|
Sms::verify($credentials); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $credentials; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Determine if the given credentials should be passed through verification. |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function shouldPassThrough($credentials) |
|
91
|
|
|
{ |
|
92
|
|
|
foreach (config('var.validation.verify_phone_excepts.phones') as $phone) { |
|
93
|
|
|
foreach (config('var.validation.verify_phone_excepts.codes') as $code) { |
|
94
|
|
|
if ( |
|
95
|
|
|
str_is($phone, $credentials['phone']) && |
|
96
|
|
|
str_is($code, $credentials['code']) |
|
97
|
|
|
) { |
|
98
|
|
|
return true; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|