Browse Source

用户登录修改session登录记录

master
wanghongjun 2 years ago
parent
commit
1c3e9c67b1
  1. 2
      app/controller/User.php
  2. 6
      app/middleware/CheckUser.php
  3. 3
      app/model/User.php

2
app/controller/User.php

@ -13,6 +13,7 @@ use think\exception\ValidateException;
use think\facade\Cache; use think\facade\Cache;
use think\facade\Db; use think\facade\Db;
use think\facade\Request; use think\facade\Request;
use think\facade\Session;
class User extends BaseController class User extends BaseController
{ {
@ -232,6 +233,7 @@ class User extends BaseController
*/ */
public function LogOut() public function LogOut()
{ {
Session::delete('login_user_data');
$login_user_data = Cache::store('redis')->get('login_user_data'); $login_user_data = Cache::store('redis')->get('login_user_data');
if ($login_user_data) Cache::store('redis')->delete('login_user_data'); if ($login_user_data) Cache::store('redis')->delete('login_user_data');
return $this->renderSuccess('退出登陆成功'); return $this->renderSuccess('退出登陆成功');

6
app/middleware/CheckUser.php

@ -5,7 +5,8 @@ namespace app\middleware;
use think\Exception; use think\Exception;
use think\facade\Cache; use think\facade\Cache;
use \think\facade\Request; use think\facade\Request;
use think\facade\Session;
class CheckUser class CheckUser
{ {
@ -26,7 +27,8 @@ class CheckUser
if($userinfo['code'] != 200) if($userinfo['code'] != 200)
throw new Exception('当前登录已失效,请重新登录',403); throw new Exception('当前登录已失效,请重新登录',403);
$request->userInfo = $userinfo['data']; $request->userInfo = $userinfo['data'];
if (!Cache::store('redis')->get('login_user_data')) { //!Cache::store('redis')->get('login_user_data')
if (!Session::get('login_user_data')) {
throw new Exception('用户未登陆,请先登陆后操作',201); throw new Exception('用户未登陆,请先登陆后操作',201);
} }
} }

3
app/model/User.php

@ -3,6 +3,7 @@
namespace app\model; namespace app\model;
use think\facade\Cache; use think\facade\Cache;
use think\facade\Session;
use think\Model; use think\Model;
use think\model\concern\SoftDelete; use think\model\concern\SoftDelete;
@ -67,7 +68,7 @@ class User extends Model
# 缓存用户信息 # 缓存用户信息
$login_user_data = $user->toArray(); $login_user_data = $user->toArray();
unset($login_user_data['password'],$login_user_data['salt']); unset($login_user_data['password'],$login_user_data['salt']);
#Session::set('login_user_data',$login_user_data); Session::set('login_user_data',$login_user_data);
Cache::store('redis')->set('login_user_data',serialize($login_user_data),7200); Cache::store('redis')->set('login_user_data',serialize($login_user_data),7200);
// 登陆成功 // 登陆成功
return ['status' => true, 'msg' => '登陆成功', 'data' => $login_user_data]; return ['status' => true, 'msg' => '登陆成功', 'data' => $login_user_data];

Loading…
Cancel
Save