|
4 | 4 |
|
5 | 5 | use Illuminate\Http\Request; |
6 | 6 | use Illuminate\Routing\Controller; |
7 | | -use PhpSms as Sms; |
8 | 7 | use SmsManager as Manager; |
9 | 8 |
|
10 | 9 | class SmsController extends Controller |
11 | 10 | { |
12 | 11 | public function postVoiceVerify(Request $request) |
13 | 12 | { |
14 | | - $token = $request->input('token', null); |
15 | 13 | $mobile = $request->input('mobile', null); |
16 | | - $seconds = $request->input('seconds', 60); |
| 14 | + $interval = $request->input('interval', 60); |
17 | 15 |
|
18 | | - if (!Manager::sendable($token)) { |
19 | | - return response()->json(Manager::genResult(false, 'request_invalid', [$seconds])); |
20 | | - } |
21 | | - |
22 | | - $res = Manager::validate($request->all()); |
| 16 | + $res = Manager::validateSendable($interval); |
23 | 17 | if (!$res['success']) { |
24 | 18 | return response()->json($res); |
25 | 19 | } |
26 | | - |
27 | | - $code = Manager::generateCode(); |
28 | | - $minutes = Manager::getCodeValidMinutes(); |
29 | | - $templates = Manager::getVoiceTemplates(); |
30 | | - $result = Sms::voice($code)->template($templates) |
31 | | - ->data(['code' => $code])->to($mobile)->send(); |
32 | | - |
33 | | - if ($result === null || $result === true || (isset($result['success']) && $result['success'])) { |
34 | | - $data = Manager::getSentInfo(); |
35 | | - $data['sent'] = true; |
36 | | - $data['mobile'] = $mobile; |
37 | | - $data['code'] = $code; |
38 | | - $data['deadline'] = time() + ($minutes * 60); |
39 | | - Manager::storeSentInfo($token, $data); |
40 | | - Manager::storeCanResendAfter($token, $seconds); |
41 | | - $res = Manager::genResult(true, 'voice_send_success'); |
42 | | - } else { |
43 | | - $res = Manager::genResult(false, 'voice_send_failure'); |
| 20 | + $res = Manager::validateFields($request->all()); |
| 21 | + if (!$res['success']) { |
| 22 | + return response()->json($res); |
44 | 23 | } |
| 24 | + $res = Manager::requestVoiceVerify($mobile, $interval); |
45 | 25 |
|
46 | 26 | return response()->json($res); |
47 | 27 | } |
48 | 28 |
|
49 | 29 | public function postSendCode(Request $request) |
50 | 30 | { |
51 | | - $token = $request->input('token', null); |
52 | 31 | $mobile = $request->input('mobile', null); |
53 | | - $seconds = $request->input('seconds', 60); |
| 32 | + $interval = $request->input('interval', 60); |
54 | 33 |
|
55 | | - if (!Manager::sendable($token)) { |
56 | | - return response()->json(Manager::genResult(false, 'request_invalid', [$seconds])); |
57 | | - } |
58 | | - |
59 | | - $res = Manager::validate($request->all()); |
| 34 | + $res = Manager::validateSendable($interval); |
60 | 35 | if (!$res['success']) { |
61 | 36 | return response()->json($res); |
62 | 37 | } |
63 | | - |
64 | | - $code = Manager::generateCode(); |
65 | | - $minutes = Manager::getCodeValidMinutes(); |
66 | | - $templates = Manager::getSmsTemplates(); |
67 | | - $content = Manager::generateSmsContent([$code, $minutes]); |
68 | | - $result = Sms::make($templates)->to($mobile) |
69 | | - ->data(['code' => $code, 'minutes' => $minutes]) |
70 | | - ->content($content)->send(); |
71 | | - |
72 | | - if ($result === null || $result === true || (isset($result['success']) && $result['success'])) { |
73 | | - $data = Manager::getSentInfo(); |
74 | | - $data['sent'] = true; |
75 | | - $data['mobile'] = $mobile; |
76 | | - $data['code'] = $code; |
77 | | - $data['deadline'] = time() + ($minutes * 60); |
78 | | - Manager::storeSentInfo($token, $data); |
79 | | - Manager::storeCanResendAfter($token, $seconds); |
80 | | - $res = Manager::genResult(true, 'sms_send_success'); |
81 | | - } else { |
82 | | - $res = Manager::genResult(false, 'sms_send_failure'); |
| 38 | + $res = Manager::validateFields($request->all()); |
| 39 | + if (!$res['success']) { |
| 40 | + return response()->json($res); |
83 | 41 | } |
| 42 | + $res = Manager::requestVerifySms($mobile, $interval); |
84 | 43 |
|
85 | 44 | return response()->json($res); |
86 | 45 | } |
87 | 46 |
|
88 | | - public function getInfo(Request $request, $token = null) |
| 47 | + public function getInfo() |
89 | 48 | { |
90 | 49 | $html = '<meta charset="UTF-8"/><h2 align="center" style="margin-top: 30px;margin-bottom: 0;">Laravel Sms</h2>'; |
91 | 50 | $html .= '<p style="margin-bottom: 30px;font-size: 13px;color: #888;" align="center">' . SmsManager::VERSION . '</p>'; |
92 | 51 | $html .= '<p><a href="https://github.com/toplan/laravel-sms" target="_blank">laravel-sms源码</a>托管在GitHub,欢迎你的使用。如有问题和建议,欢迎提供issue。</p>'; |
93 | | - $html .= '<p>本页面路由: scheme://your-domain/sms/info<span style="color: #aaa;">/{token?}</spanst></p>'; |
94 | 52 | $html .= '<hr>'; |
95 | 53 | $html .= '<p>你可以在调试模式(设置config/app.php中的debug为true)下查看到存储在存储器中的验证码短信/语音相关数据:</p>'; |
96 | 54 | echo $html; |
97 | | - $token = $token ?: $request->input('token', null); |
98 | 55 | if (config('app.debug')) { |
99 | | - dump(Manager::retrieveAll($token)); |
| 56 | + dump(Manager::retrieveAllData()); |
100 | 57 | } else { |
101 | | - echo '<p align="center" style="color: red;">现在是非调试模式,无法查看验证码短信数据</p>'; |
| 58 | + echo '<p align="center" style="color: red;">现在是非调试模式,无法查看调试数据</p>'; |
102 | 59 | } |
103 | 60 | } |
104 | 61 | } |
0 commit comments