-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.php
More file actions
134 lines (121 loc) · 3.21 KB
/
Base.php
File metadata and controls
134 lines (121 loc) · 3.21 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/10/30 0030
* Time: 14:13
*/
require_once './helpers.php';
require_once './DBServer.php';
require_once './RedisServer.php';
require_once './UserModel.php';
require_once './validater.php';
class Base
{
protected $redis;
protected $validater;
protected $user;
//email或phone的字段名
protected $identifier = 'identifier';
public $type;
public $sessionLifeTime = 1440;
//记住我的时间
public $rememberTime = 1296000;
public $rememberKey = 'rememberme';
public function __construct()
{
$this->redis = new RedisServer();
$this->validater = new validater();
$this->user = new UserModel();
$this->validater->setIdentifierField($this->identifier);
}
/**
* @param $data
* @param $msg
* @param $code
* 返回
*/
protected function jsonReturn($result)
{
header('Content-type: application/json');
echo json_encode($result);
exit();
}
/**
* @param $record
* 用户登录成功后将用户信息写入session
*/
protected function writeInfoInSession($user)
{
if(is_null($_SESSION['user']['id']))
$_SESSION['user']['id'] = $user['id'];
if(is_null($_SESSION['user']['name']))
$_SESSION['user']['name'] = $user['name'];
}
/**
* @param $userId
* 将用户的sessionId存入redis中并写入session
*/
protected function saveShareSessionId($user)
{
$key = $this->getShareSessionKey($user['id']);
//如果之前在其他的客户端已经存储了就不用再存储
if($this->redis->has($key))
{
//我习惯把session设置为自动开启,因此现在这里删除掉此次session
//然后再把已经存在的session放到http的头部中
header_remove('Set-Cookie');
setcookie(session_name(),$this->redis->get($key));
return;
}
//session_regenerate_id();
//存入redis
$this->redis->setex($key,session_id(),$this->sessionLifeTime);
//写入session
$this->writeInfoInSession($user);
}
/**
* @param $userId
* @return string
* 获取share session 的key,是由用户的id和一个固定的前缀获得的
*/
public function getShareSessionKey($userId)
{
return $this->getPrefixKey('sharesession',$userId);
}
/**
* @param $userId
* @return string
* 获取根据prefix,key进行加密
*/
protected function getPrefixKey($prefix,$key)
{
return md5($prefix.$key);
}
/**
* @return string
* 获取一个随机的key
*/
protected function getRandomKey()
{
return md5(uniqid());
}
/**
* @param $password
* @return string
* 记住我功能的密码加密
*/
protected function rememberPasswordEncrypt($password)
{
return base64_encode($password);
}
/**
* @param $encrypt
* @return bool|string
* 记住我功能的密码解密
*/
protected function rememberPasswordDecrypt($encrypt)
{
return base64_decode($encrypt);
}
}