Manager.php
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* Created by PhpStorm.
* User: asus
* Date: 2019/11/26
* Time: 10:49
*/
namespace app\api\model;
use think\Cache;
class Manager extends Base
{
protected $autoWriteTimestamp = 'datetime';
/**
* 登录 @param $account:账号 $password:密码
*/
public function login($account,$password){
//获取当前用户的账号详情
$info = $this->read(['account'=>$account,'password'=>$password]);
if(empty($info) || $info['status'] == 1){
return [];
}
//更新数据
$token = sha1(uniqid());
$last_login_time = date('Y-m-d H:i:s',time());
$last_ip = request()->ip();
//更新登录时间与登录ip
$rs = $this->edit(['token'=>$token,'update_time'=>$last_login_time,'last_ip'=>$last_ip],['id'=>$info['id']]);
if($rs === false){
return [];
}
//清空上一次缓存,实现单点登录
Cache::rm($info['token']);
//更新
$info['token'] = $token;
$info['update_time'] = $last_login_time;
$info['last_ip'] = $last_ip;
//过滤密码
unset($info['password']);
//生成缓存
$expire = 3600 * 24 * 7;
Cache::set($token, $info, $expire);
return $info;
}
}