User.php
12.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/4/28
* Time: 17:44
*/
namespace app\index\controller;
use app\common\controller\Frontend;
use app\index\model\Code;
use fast\Random;
use think\captcha\Captcha;
use think\Cookie;
use think\Db;
class User extends Frontend
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* 登录页
* @return mixed
*/
public function login_view(){
$target_url = $this->request->param('target_url');
$this->assign('title',"登录");
$this->assign('target_url',$target_url);
return $this->fetch();
}
/**
* 注册页
* @return mixed
*/
public function register_view(){
$this->assign('title',"注册");
return $this->fetch();
}
/**
* 注册
*/
public function register_login(){
$username = $this->request->param('username');
$mobile = $this->request->param('mobile');
$code = $this->request->param('code');
$password = $this->request->param('password');
if(empty($mobile) || empty($password) || empty($username) || empty($code)){
$this->error('缺少必要参数');
}
$codeModel = new Code();
$data = $codeModel->findData(['mobile'=>$mobile]);
if(!$data){
$this->error('请先获取验证码');
}
if($data['is_use'] != 2){
$this->error('验证码已失效');
}
if($data['pasttime'] < time()){
$this->error('验证码已过期');
}
if($data['code'] != $code){
$this->error('验证码错误');
}
$userModel = new \app\index\model\User();
$result = $userModel->where(['mobile'=>$mobile])->find();
if($result){
$this->error('该手机号已经注册');
}
Db::startTrans();
$salt = Random::alnum();
$arr3['username'] = $username;
$arr3['avatar'] = "/assets/img/avatar.png";
$arr3['mobile'] = $mobile;
$arr3['salt'] = $salt;
$arr3['pwd'] = $password;
$arr3['password'] = md5(md5($password) . $salt);
$arr3['logintime'] = time();
$arr3['loginip'] = $this->request->ip(0, true);
$arr3['joinip'] = $this->request->ip(0, true);
$arr3['createtime'] = time();
$arr3['logintime'] = time();
$arr3['loginip'] = $this->request->ip(0, true);
$result3 = $userModel->insertData($arr3);
if(empty($result3)){
Db::rollback();
$this->error('sql执行失败');
}
$result4 = $codeModel->updateData(['id'=>$data['id']],['is_use'=>1]);
if(empty($result4)){
Db::rollback();
$this->error('sql执行失败');
}
Db::commit();
// $token = generate_user_token($result3);
// session('token',$token);
$this->auth->direct($result3);
$token = $this->auth->getToken();
session('token',$token);
Cookie::set('token',$token);
$this->success('SUCCESS');
}
/**
* 密码登录
*/
public function password_login(){
$mobile = $this->request->param('mobile');
$password = $this->request->param('password');
$target_url = $this->request->param('target_url');
if(empty($mobile) || empty($password)){
$this->error('缺少必要参数');
}
$userModel = new \app\index\model\User();
$result = $userModel->findData(['mobile'=>$mobile]);
if(empty($result)){
$this->error('账号错误');
}
$password = md5(md5($password) . $result['salt']);
if($result['password'] != $password){
$this->error('密码错误');
}
$user = Db::name('user')->where(['id'=>$result['id']])->find();
if($user['status'] != 'normal'){
$this->error('抱歉,您已被加入黑名单');
}
$arr3['logintime'] = time();
$arr3['loginip'] = $this->request->ip(0, true);
$result3 = $userModel->updateData(['id'=>$result['id']],$arr3);
if(empty($result3)){
$this->error('sql执行失败');
}
// $token = generate_user_token($result['id']);
// session('token',$token);
$this->auth->direct($result['id']);
$token = $this->auth->getToken();
session('token',$token);
Cookie::set('token',$token);
session('user',$result);
$this->success('SUCCESS','',['target_url'=>rawurldecode($target_url)]);
}
/**
* 短信验证码登录
*/
public function code_login(){
$mobile = $this->request->param('mobile');
$code = $this->request->param('code');
if(empty($mobile) || empty($code)){
$this->error('缺少必要参数');
}
$userModel = new \app\index\model\User();
$result1 = $userModel->findData(['mobile'=>$mobile]);
if(empty($result1)){
$this->error('账号错误');
}
$codeModel = new Code();
$data = $codeModel->findData(['mobile'=>$mobile]);
if(empty($data)){
$this->error('请先获取验证码');
}
if($data['is_use'] != 2){
$this->error('验证码已失效');
}
if($data['pasttime'] < time()){
$this->error('验证码已过期');
}
if($data['code'] != $code){
$this->error('验证码错误');
}
$user = Db::name('user')->where(['id'=>$result1['id']])->find();
if($user['status'] != 'normal'){
$this->error('抱歉,您已被加入黑名单');
}
$arr3['logintime'] = time();
$arr3['loginip'] = $this->request->ip(0, true);
$result3 = $userModel->updateData(['id'=>$result1['id']],$arr3);
if(empty($result3)){
$this->error('sql执行失败');
}
$result4 = $codeModel->updateData(['id'=>$data['id']],['is_use'=>1]);
if(empty($result4)){
Db::rollback();
$this->error('sql执行失败');
}
// $token = generate_user_token($result1['id']);
// session('token',$token);
$this->auth->direct($result1['id']);
$token = $this->auth->getToken();
session('token',$token);
Cookie::set('token',$token);
session('user',$result1);
$this->success('SUCCESS');
}
/**
* 获取短信验证码
*/
public function get_code1(){
$mobile = $this->request->param('mobile');
if (empty($mobile)) {
$this->error('缺少必要参数');
}
$code = Random::numeric(6);
$codeModel = new Code();
$data = $codeModel->findData(['mobile' => $mobile]);
$arr1['mobile'] = $mobile;
$arr1['code'] = $code;
$arr1['pasttime'] = time() + 600;
$arr1['is_use'] = 2;
Db::startTrans();
if (empty($data)) {
$arr1['createtime'] = time();
$result1 = $codeModel->insertData($arr1);
} else {
$arr1['updatetime'] = time();
$result1 = $codeModel->updateData(['mobile' => $mobile], $arr1);
}
if (empty($result1)) {
$this->error('sql执行失败');
}
$content = array(
'content' => "【工企邦】您的验证码是:" . $code . ",请于10分钟内使用,如非本人操作,可忽略此消息。",//短信内容
'mobile' => $mobile,//手机号码
'tKey' => time(),
);
$result2 = json_decode(send_sms2($content),true);
if ($result2['code'] != 200) {
Db::rollback();
$this->error('发送失败');
}
Db::commit();
$this->success('SUCCESS','',['code'=>$code]);
}
/**
* 忘记密码
* @return mixed
*/
public function forget_password_view(){
$this->assign('title',"忘记密码");
return $this->fetch();
}
/**
* 获取短信验证码(需验证图形验证码)
*/
public function get_code2(){
$mobile = $this->request->param('mobile');
$image_code = $this->request->param('image_code');
if (empty($mobile) || empty($image_code)) {
$this->error('缺少必要参数');
}
$captcha = new Captcha();
if(!$captcha->check($image_code)){
$this->error('图形验证码错误');
}
$code = Random::numeric(6);
$codeModel = new Code();
$data = $codeModel->findData(['mobile' => $mobile]);
$arr1['mobile'] = $mobile;
$arr1['code'] = $code;
$arr1['pasttime'] = time() + 600;
$arr1['is_use'] = 2;
Db::startTrans();
if (empty($data)) {
$arr1['createtime'] = time();
$result1 = $codeModel->insertData($arr1);
} else {
$arr1['updatetime'] = time();
$result1 = $codeModel->updateData(['mobile' => $mobile], $arr1);
}
if (empty($result1)) {
$this->error('sql执行失败');
}
$content = array(
'content' => "【工企邦】您的验证码是:" . $code . ",请于10分钟内使用,如非本人操作,可忽略此消息。",//短信内容
'mobile' => $mobile,//手机号码
'tKey' => time(),
);
$result2 = json_decode(send_sms2($content),true);
if ($result2['code'] != 200) {
Db::rollback();
$this->error('发送失败');
}
Db::commit();
$this->success('SUCCESS','',['code'=>$code]);
}
/**
* 下一步
*/
public function next(){
$mobile = $this->request->param('mobile');
$code = $this->request->param('code');
if(empty($mobile) || empty($code)){
$this->error('缺少必要参数');
}
$userModel = new \app\index\model\User();
$result1 = $userModel->findData(['mobile'=>$mobile]);
if(empty($result1)){
$this->error('账号错误');
}
$codeModel = new Code();
$data = $codeModel->findData(['mobile'=>$mobile]);
if(empty($data)){
$this->error('请先获取验证码');
}
if($data['is_use'] != 2){
$this->error('验证码已失效');
}
if($data['pasttime'] < time()){
$this->error('验证码已过期');
}
if($data['code'] != $code){
$this->error('验证码错误');
}
$this->success('SUCCESS');
}
/**
* 设置密码
*/
public function setting_password(){
$mobile = $this->request->param('mobile');
$password = $this->request->param('password');
$affirm_password = $this->request->param('affirm_password');
if(empty($mobile) || empty($password) || empty($affirm_password)){
$this->error('缺少必要参数');
}
if($password != $affirm_password){
$this->error('两次密码输入不一致');
}
$userModel = new \app\index\model\User();
$data = $userModel->findData(['mobile'=>$mobile]);
if(empty($data)){
$this->error('该账户尚未注册');
}
Db::startTrans();
$arr1['pwd'] = $password;
$arr1['password'] = md5(md5($password) . $data['salt']);
$arr1['updatetime'] = time();
$result1 = $userModel->updateData(['id'=>$data['id']],$arr1);
if(empty($result1)){
Db::rollback();
$this->error('sql执行失败');
}
// $token = generate_user_token($result1);
// session('token',$token);
$this->auth->direct($data['id']);
$token = $this->auth->getToken();
session('token',$token);
Cookie::set('token',$token);
Db::commit();
$this->success('SUCCESS','index/index');
}
/**
* 注销登录
*/
public function logout()
{
//注销本站
$this->auth->logout();
session('token',null);
session('user',null);
Cookie::delete('token');
$this->redirect(url('index/index'));
// $this->success(__('Logout successful'), url('index/index'));
}
}