// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\index\controller; use app\common\model\User as UserAlias; use app\index\model\User as UserModel; use app\index\model\user\UserAccessLog; use think\facade\Cache; 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') { UserAccessLog::doSave([ 'user_id' => 0, 'path' => $this->request->action(), 'remark' => '', 'ip' => $this->request->ip(), 'store_id' => $this->getStoreId(), 'create_time' => time() ]); 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']); $returnUri = Session::pull('returnuri'); return $this->renderSuccess([ 'userId' => (int)$userInfo['user_id'], 'token' => $LoginService->getToken((int)$userInfo['user_id']), 'returnUri' => $returnUri ?? '' ], '登录成功'); } public function register() { $form = $this->postForm(); UserAccessLog::doSave([ 'user_id' => 0, 'path' => $this->request->action(), 'remark' => json_encode($form), 'ip' => $this->request->ip(), 'store_id' => $this->getStoreId(), 'create_time' => time() ]); // 执行登录 $LoginService = new LoginService; if (!$LoginService->toRegister($form)) { return $this->renderError($LoginService->getError()); } if (!empty($form['mobile'])) { $userInfo = $LoginService->getUserInfo(); UserModel::setIncPoints(intval($userInfo['user_id']), UserAlias::POINTS_FOR_REGISTER, 'Register .'); } return $this->renderSuccess([], 'Registration is successful'); } /** * 退出登录 * @return \think\response\Redirect */ public function logout() { Session::clear(); return redirect('/index/passport/login.html'); //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'); } }