diff --git a/app/controller/api/Auth.php b/app/controller/api/Auth.php index bc222501..e06a6a1a 100644 --- a/app/controller/api/Auth.php +++ b/app/controller/api/Auth.php @@ -817,4 +817,30 @@ class Auth extends BaseController } } + /** + * 发送短信验证码 + * @return mixed + */ + public function verifyCode() + { + $data = $this->request->params(['phone', ['type', 'login']]); + $sms_limit_key = 'sms_limit_' . $data['phone']; + $limit = Cache::get($sms_limit_key) ? Cache::get($sms_limit_key) : 0; + $sms_limit = systemConfig('sms_limit'); + if ($sms_limit && $limit > $sms_limit) { + return app('json')->fail('请求太频繁请稍后再试'); + } + try { + $sms_code = str_pad(random_int(1, 9999), 4, 0, STR_PAD_LEFT); + $sms_time = systemConfig('sms_time') ? systemConfig('sms_time') : 30; + SmsService::create()->send($data['phone'], 'VERIFICATION_CODE', ['code' => $sms_code, 'time' => $sms_time]); + } catch (Exception $e) { + return app('json')->fail($e->getMessage()); + } + $sms_key = app()->make(SmsService::class)->sendSmsKey($data['phone'], $data['type']); + Cache::set($sms_key, $sms_code, $sms_time * 60); + Cache::set($sms_limit_key, $limit + 1, 60); + return app('json')->success('短信发送成功'); + } + } diff --git a/route/api.php b/route/api.php index 3796ce86..eaa24587 100644 --- a/route/api.php +++ b/route/api.php @@ -608,6 +608,7 @@ Route::group('api/', function () { Route::post('user/change_pwd', 'api.Auth/changePassword'); //验证码 Route::post('auth/verify', 'api.Auth/verify'); + Route::post('auth/verifyCode', 'api.Auth/verifyCode'); //微信配置 Route::get('wechat/config', 'api.Wechat/jsConfig'); //图片验证码