// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\index\controller; use app\index\service\passport\Login as LoginService; use think\facade\Session; /** * 用户认证模块 * Class Passport * @package app\api\controller */ class Passport extends Controller { /** * 登录接口 (需提交手机号、短信验证码、第三方用户信息) * @return \think\response\Json|\think\response\View * @throws \app\common\exception\BaseException * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function login() { if ($this->request->method() == 'GET') { return view('logIn'); } // 执行登录 $LoginService = new LoginService; if (!$LoginService->toLogin($this->postForm())) { return $this->renderError($LoginService->getError()); } // 用户信息 $userInfo = $LoginService->getUserInfo(); $token = $LoginService->getToken((int)$userInfo['user_id']); Session::set('access_token', $token); Session::set('user_id', $userInfo['user_id']); return $this->renderSuccess([ 'userId' => (int)$userInfo['user_id'], 'token' => $LoginService->getToken((int)$userInfo['user_id']) ], '登录成功'); } public function register() { // 执行登录 $LoginService = new LoginService; if (!$LoginService->toRegister($this->postForm())) { return $this->renderError($LoginService->getError()); } return $this->renderSuccess([], 'Registration is successful'); } /** * 退出登录 * @return \think\response\Redirect */ public function logout() { Session::clear(); return redirect('/index/passport/login'); //return view('logIn'); } public function retrievePassword() { if ($this->request->method() == 'GET') { return view('retrievePassword'); } $params = $this->postForm(); if (empty($params['mobile']) || empty($params['smsCode'])) { if ($params['password'] !== $params['password1']) { return $this->renderError('password not match'); } } $LoginService = new LoginService; $r = $LoginService->resetPassword($params['mobile'], $params['smsCode'], $params['password']); if ($r) { return $this->renderSuccess([], 'Reset success'); } return $this->renderError('something wrong'); } }