PublicController.php
7.1 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\user\controller;
use think\Db;
use think\Validate;
use cmf\controller\RestBaseController;
class PublicController extends RestBaseController
{
// 用户注册
public function register()
{
$validate = new Validate([
'username' => 'require',
'password' => 'require',
'verification_code' => 'require'
]);
$validate->message([
'username.require' => '请输入手机号,邮箱!',
'password.require' => '请输入您的密码!',
'verification_code.require' => '请输入数字验证码!'
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$user = [];
$findUserWhere = [];
if (Validate::is($data['username'], 'email')) {
$user['user_email'] = $data['username'];
$findUserWhere['user_email'] = $data['username'];
} else if (preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['username'])) {
$user['mobile'] = $data['username'];
$findUserWhere['mobile'] = $data['username'];
} else {
$this->error("请输入正确的手机或者邮箱格式!");
}
$errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
if (!empty($errMsg)) {
$this->error($errMsg);
}
$findUserCount = Db::name("user")->where($findUserWhere)->count();
if ($findUserCount > 0) {
$this->error("此账号已存在!");
}
$user['create_time'] = time();
$user['user_status'] = 1;
$user['user_type'] = 2;
$user['user_pass'] = cmf_password($data['password']);
$result = Db::name("user")->insert($user);
if (empty($result)) {
$this->error("注册失败,请重试!");
}
$this->success("注册并激活成功,请登录!");
}
// 用户登录 TODO 增加最后登录信息记录,如 ip
public function login()
{
$validate = new Validate([
'username' => 'require',
'password' => 'require'
]);
$validate->message([
'username.require' => '请输入手机号,邮箱或用户名!',
'password.require' => '请输入您的密码!'
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$findUserWhere = [];
if (Validate::is($data['username'], 'email')) {
$findUserWhere['user_email'] = $data['username'];
} else if (preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['username'])) {
$findUserWhere['mobile'] = $data['username'];
} else {
$findUserWhere['user_login'] = $data['username'];
}
$findUser = Db::name("user")->where($findUserWhere)->find();
if (empty($findUser)) {
$this->error("用户不存在!");
} else {
switch ($findUser['user_status']) {
case 0:
$this->error('您已被拉黑!');
case 2:
$this->error('账户还没有验证成功!');
}
if (!cmf_compare_password($data['password'], $findUser['user_pass'])) {
$this->error("密码不正确!");
}
}
$allowedDeviceTypes = $this->allowedDeviceTypes;
if (empty($data['device_type']) || !in_array($data['device_type'], $allowedDeviceTypes)) {
$this->error("请求错误,未知设备!");
}
$userTokenQuery = Db::name("user_token")
->where('user_id', $findUser['id'])
->where('device_type', $data['device_type']);
$findUserToken = $userTokenQuery->find();
$currentTime = time();
$expireTime = $currentTime + 24 * 3600 * 180;
$token = md5(uniqid()) . md5(uniqid());
if (empty($findUserToken)) {
$result = $userTokenQuery->insert([
'token' => $token,
'user_id' => $findUser['id'],
'expire_time' => $expireTime,
'create_time' => $currentTime,
'device_type' => $data['device_type']
]);
} else {
$result = $userTokenQuery
->where('user_id', $findUser['id'])
->where('device_type', $data['device_type'])
->update([
'token' => $token,
'expire_time' => $expireTime,
'create_time' => $currentTime
]);
}
if (empty($result)) {
$this->error("登录失败!");
}
$this->success("登录成功!", ['token' => $token, 'user' => $findUser]);
}
// 用户退出
public function logout()
{
$userId = $this->getUserId();
Db::name('user_token')->where([
'token' => $this->token,
'user_id' => $userId,
'device_type' => $this->deviceType
])->update(['token' => '']);
$this->success("退出成功!");
}
// 用户密码重置
public function passwordReset()
{
$validate = new Validate([
'username' => 'require',
'password' => 'require',
'verification_code' => 'require'
]);
$validate->message([
'username.require' => '请输入手机号,邮箱!',
'password.require' => '请输入您的密码!',
'verification_code.require' => '请输入数字验证码!'
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$userWhere = [];
if (Validate::is($data['username'], 'email')) {
$userWhere['user_email'] = $data['username'];
} else if (preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['username'])) {
$userWhere['mobile'] = $data['username'];
} else {
$this->error("请输入正确的手机或者邮箱格式!");
}
$errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
if (!empty($errMsg)) {
$this->error($errMsg);
}
$userPass = cmf_password($data['password']);
Db::name("user")->where($userWhere)->update(['user_pass' => $userPass]);
$this->success("密码重置成功,请使用新密码登录!");
}
}