| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- declare(strict_types=1);
- namespace app\api\service\auth;
- use think\facade\Cache;
- use app\common\model\UserToken;
- use app\common\model\User;
- use think\facade\Config;
- class MysqlAdapter
- {
- private UserToken $usertoken;
-
- public static function userinfo(string $token):int
- {
- return UserToken::where('token',$token)->where('expire','>', time())->value('user_id');
- }
- public static function getUserToken($token):bool
- {
- return boolval(UserToken::where('token',$token)->where('expire','>', time())->order('id','asc')->count());
- }
- public static function login(string $token,User $user)
- {
- $keepalive_time=Config::get('app.user_login.keepalive_time');
- $token = md5($token);
- UserToken::create([
- 'token'=> $token ,
- 'user_id'=>$user->id,
- 'expire'=>time()+$keepalive_time
- ]);
-
- $allow_device_num=Config::get('app.user_login.allow_device_num');
- //如果数据库中保存的设备数大于允许的设备数,如果超出则挤出去最早登陆的设备
- $time=time();
- $count=UserToken::where('user_id',$user->id)->where('expire','>',$time)->count();
- if($count>$allow_device_num){
- $usertoken=UserToken::where('user_id',$user->id)->where('expire','>',$time)->order('id','asc')->find();
- $usertoken->delete();
- }
- return $token ;
- }
- public static function logout(int $uid)
- {
- $token = request()->header('token');
- UserToken::where('token', $token)->delete();
- Cache::store('redis')->delete('user_info_'.$uid);
- }
- }
|