From e5911f906d0848b8c718a363937dcccfa3e48b49 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Wed, 23 Aug 2023 09:43:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E6=8E=A7=E5=88=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/Passport.php | 128 ++++++++++++++++++++++++++++++++++++ app/controller/User.php | 114 -------------------------------- route/app.php | 9 ++- 3 files changed, 134 insertions(+), 117 deletions(-) create mode 100644 app/controller/Passport.php diff --git a/app/controller/Passport.php b/app/controller/Passport.php new file mode 100644 index 0000000..e0129e7 --- /dev/null +++ b/app/controller/Passport.php @@ -0,0 +1,128 @@ +scene('register')->check($data); + + // 验证手机号短信验证码 + $userModel = new UserModel(); + $smsCode = $data['sms_code']; + $phone = $data['phone']; + + if (!isset($data['invite_code'])) $data['invite_code'] = ''; + $invite_code = $data['invite_code']; + + if (!empty($invite_code) && !$userModel->verifyInviteCode($invite_code)) { + return $this->renderError('邀请码无效'); + } + + if (!$userModel->verifySmsCode($phone, $smsCode)) { + return $this->renderError('短信验证码错误'); + } + + // 注册用户 + if ($userModel->register($data)) { + return $this->renderSuccess('注册成功'); + } else { + return $this->renderSuccess('手机号已注册'); + } + } catch (ValidateException $exception) { + return $this->renderError($exception->getMessage()); + } + } + + /** + * 用户登录 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author whj + * @date 2023-08-22 17:04 + */ + public function login() + { + $data = Request::param(); + + try { + // 验证用户输入 + validate(UserValidate::class)->scene('login')->check($data); + + // 用户登录 + $userModel = new UserModel(); + $user = $userModel->login($data); + + if ($user['status']) { + + $userinfo = ['id' => $user['data']['id'], 'username' => $user['data']['username']]; + $token = ['token'=>signToken($userinfo)]; + + return $this->renderSuccess('登陆成功',$token); + } else { + return $this->renderError($user['msg']); + } + } catch (ValidateException $exception) { + return $this->renderError($exception->getMessage()); + } + } + + /** + * 找回密码 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @author whj + * @date 2023-08-22 17:22 + */ + public function retrieve() + { + $data = Request::param(); + + try { + // 验证用户输入 + validate(UserValidate::class)->scene('retrieve')->check($data); + + $userModel = new UserModel(); + $phone = $data['phone']; + $smsCode = $data['sms_code']; + + if (!$userModel->verifySmsCode($phone, $smsCode)) { + return $this->renderError('短信验证码错误'); + } + + $user = $userModel->retrieve($data); + + if ($user['status']) { + return $this->renderSuccess('密码重置成功'); + } else { + return $this->renderError($user['msg']); + } + } catch (ValidateException $exception) { + return $this->renderError($exception->getMessage()); + } + } +} \ No newline at end of file diff --git a/app/controller/User.php b/app/controller/User.php index cdebdd2..6d5bd2a 100644 --- a/app/controller/User.php +++ b/app/controller/User.php @@ -9,121 +9,7 @@ use think\facade\Request; class User extends BaseController { - /** - * 用户注册 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author whj - * @date 2023-08-22 17:04 - */ - public function register() - { - $data = Request::param(); - - try { - // 验证用户输入 - validate(UserValidate::class)->scene('register')->check($data); - - // 验证手机号短信验证码 - $userModel = new UserModel(); - $smsCode = $data['sms_code']; - $phone = $data['phone']; - - if (!isset($data['invite_code'])) $data['invite_code'] = ''; - $invite_code = $data['invite_code']; - - if (!empty($invite_code) && !$userModel->verifyInviteCode($invite_code)) { - return $this->renderError('邀请码无效'); - } - - if (!$userModel->verifySmsCode($phone, $smsCode)) { - return $this->renderError('短信验证码错误'); - } - - // 注册用户 - if ($userModel->register($data)) { - return $this->renderSuccess('注册成功'); - } else { - return $this->renderSuccess('手机号已注册'); - } - } catch (ValidateException $exception) { - return $this->renderError($exception->getMessage()); - } - } - - /** - * 用户登录 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author whj - * @date 2023-08-22 17:04 - */ - public function login() - { - $data = Request::param(); - try { - // 验证用户输入 - validate(UserValidate::class)->scene('login')->check($data); - - // 用户登录 - $userModel = new UserModel(); - $user = $userModel->login($data); - - if ($user['status']) { - - $userinfo = ['id' => $user['data']['id'], 'username' => $user['data']['username']]; - $token = ['token'=>signToken($userinfo)]; - - return $this->renderSuccess('登陆成功',$token); - } else { - return $this->renderError($user['msg']); - } - } catch (ValidateException $exception) { - return $this->renderError($exception->getMessage()); - } - } - - /** - * 找回密码 - * @return array - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - * @author whj - * @date 2023-08-22 17:22 - */ - public function retrieve() - { - $data = Request::param(); - - try { - // 验证用户输入 - validate(UserValidate::class)->scene('retrieve')->check($data); - - $userModel = new UserModel(); - $phone = $data['phone']; - $smsCode = $data['sms_code']; - - if (!$userModel->verifySmsCode($phone, $smsCode)) { - return $this->renderError('短信验证码错误'); - } - - $user = $userModel->retrieve($data); - - if ($user['status']) { - return $this->renderSuccess('密码重置成功'); - } else { - return $this->renderError($user['msg']); - } - } catch (ValidateException $exception) { - return $this->renderError($exception->getMessage()); - } - } /** * 找回密码 diff --git a/route/app.php b/route/app.php index f411faa..7be4698 100644 --- a/route/app.php +++ b/route/app.php @@ -16,10 +16,13 @@ Route::get('think', function () { }); +Route::group('passport',function (){ + Route::post('register','passport/register'); + Route::post('login','passport/login'); + Route::post('retrieve','passport/retrieve'); +}); + Route::group('user',function (){ - Route::post('register','user/register'); - Route::post('login','user/login'); - Route::post('retrieve','user/retrieve'); Route::post('modifyPassword','user/modifyPassword')->middleware(CheckToken::class); });