123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace App\Http\Controllers\AdminApi;
- use App\Http\Controllers\AdminController;
- use App\Models\UserModels\User;
- use App\Servers\Common\CommonServer;
- use App\Servers\Common\PassServer;
- use App\Servers\Common\RedisDataServer;
- class LoginController extends AdminController
- {
- /**
- * 后台登录
- * @return \Illuminate\Http\JsonResponse
- */
- function login(){
- // $type = request()->input('type',1);//登录类型 1密码2验证码
- $phone = request()->input('phone', '');//账号
- $password = request()->input('password', '123456');//密码
- $code = request()->input('code', '');//验证码
- // if(!in_array($type,[1,2])) return $this->apiResponseError('缺少必要参数');
- if (empty($phone)) return $this->apiResponseError( '登录手机号必填');
- if(!CommonServer::creatServer()->verifyPhoneNumber($phone)){
- return $this->apiResponseError( '手机号不正确');
- }
- if (empty($password)) return $this->apiResponseError( '登录密码必填');
- //获取用户信息
- $where = [['is_del',0], ['phone',$phone]];
- $user = User::where($where)->select(['id', 'password', 'encrypt', 'api_token','status'])->first();
- if (empty($user)) {
- return $this->apiResponseError('当前账户不存在');
- }
- if ($user['status'] == 2) {
- return $this->apiResponseError('账户已关闭');
- }
- //密码验证
- $pass_server = PassServer::creatServer($password, $user->{'encrypt'});
- if (!$pass_server->verifyPass($user->{'password'})) {
- return $this->apiResponseError('登陆密码错误');
- }
- //记录旧token
- $old_token = 'gw_adminLogin_' . $user['api_token'];
- //生成token
- $token = hash('sha256', $user['id'] . 'token' . time());
- $update['api_token'] = $token;
- //更新token
- $token_ret = $user->update($update);
- if(empty($token_ret)){
- return $this->apiResponseError('登陆信息更新失败');
- }
- //清除原token缓存
- if(RedisDataServer::creatServer()->getData($old_token,'json')){
- RedisDataServer::creatServer()->delData($old_token);
- }
- //获取用户信息
- $info = User::where('api_token', $token)->select(['id','name','phone'])->first();
- return $this->apiResponseSuccess('登录成功',['token'=>$token,'user'=>$info]);
- }
- /**
- * 退出登录
- * @return \Illuminate\Http\JsonResponse
- */
- public function logout()
- {
- $token = request()->input('api_token');
- if(empty($token)) $token = request()->header('ApiToken');
- //清除原token缓存
- RedisDataServer::creatServer()->delData( 'adminApiLogin_' . $token);
- User::where('api_token', $token)->update(['api_token'=>'']);
- return $this->apiResponseSuccess('退出成功');
- }
- }
|