123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- 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 app\index\service\User as UserService;
- 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
- * @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()
- ]);
- $userId = UserService::getCurrentLoginUserId();
- if ($userId){
- return redirect('/index/index/index.html');
- }
- 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');
- }
- }
|