LoginController.php
10.6 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
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;
use app\admin\model\BabyModel;
use app\member\model\MemberModel;
use app\member\model\MemberSalesModel;
use think\Validate;
use cmf\controller\HomeBaseController;
use app\user\model\UserModel;
class LoginController extends HomeBaseController
{
/**
* 登录
*/
public function index()
{
$url = url('index','','',true);
$qrcode = $this->createQRcode($url,'Q',9);
$name = '用户登录页';
$this->qrcodePage($url,$name,$qrcode);
$redirect = $this->request->post("redirect");
if (empty($redirect)) {
$redirect = $this->request->server('HTTP_REFERER');
} else {
$redirect = base64_decode($redirect);
}
session('login_http_referer', $redirect);
if (cmf_is_user_login()) { //已经登录时直接跳到首页
return redirect(url('user/Login/chooseAge'));
} else {
return $this->fetch(":login");
}
}
/**
* 登录验证提交
*/
public function doLogin()
{
if ($this->request->isPost()) {
$validate = new Validate([
// 'captcha' => 'require',
'mobile' => 'require',
'code' => 'require|number|length:6',
]);
$validate->message([
'mobile.require' => '用户名不能为空',
'code.require' => '验证码不能为空',
'code.number' => '验证码必须为数字',
'code.length' => '验证码长度必须为6位数字',
// 'captcha.require' => '验证码不能为空',
]);
$data = $this->request->post();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
// if (!cmf_captcha_check($data['captcha'])) {
// $this->error('验证码错误');
// }
// $userModel = new UserModel();
$userModel = new MemberModel();
$user['code'] = $data['code'];
// if (Validate::is($data['username'], 'email')) {
// $user['user_email'] = $data['username'];
// $log = $userModel->doEmail($user);
// } else
if (preg_match('/^1(3|4|5|7|8|9)\d{9}$/', $data['mobile'])) {
$user['mobile'] = $data['mobile'];
$log = $userModel->doMobile($user);
}
// else {
// $user['user_login'] = $data['username'];
// $log = $userModel->doName($user);
// }
$session_login_http_referer = session('login_http_referer');
$redirect = empty($session_login_http_referer) ? $this->request->root() : $session_login_http_referer;
switch ($log) {
case 0:
cmf_user_action('login');
// $this->success('登录成功', $redirect);
$this->success('登录成功', url('user/Login/chooseAge'));
break;
case 1:
$this->error('登录验证码错误');
break;
case 2:
$this->error('账户不存在');
break;
case 3:
$this->error('账号被禁止访问系统');
break;
default :
$this->error('未受理的请求');
}
} else {
$this->error("请求错误");
}
}
/**
* 登录选择宝宝年龄段
*/
public function chooseAge() {
if (cmf_is_user_login()) {
// if(!session('baby_age')) {
$baby_model = new BabyModel();
$where = [
'delete_time' => 0
];
$list = [];
$babyList = $baby_model->where($where)->select();
foreach ($babyList as $kg=>$vg) {
$info = [
'label' => $vg['age'],
'value' => $vg['id']
];
$list[] = $info;
}
$this->assign('babyList',json_encode($list));
return $this->fetch(':choose');
// } else {
// return redirect($this->request->root() . '/');
// }
} else {
return redirect(url('user/Login/index'));
}
}
/**
* 选择宝宝年龄
*/
public function chooseSubmit() {
if($this->request->isAjax()) {
$id = $this->request->param('id');
$baby_model = new BabyModel();
$where = [
'id' => $id
];
$babyInfo = $baby_model->where($where)->find();
if(!$babyInfo) {
$this->error('选择的年龄段不存在');
}
session('baby_age',$id);
$this->success('成功',url('portal/Index/detail',['id'=>$id]));
}
}
/**
* 登录验证提交(销售人员)
*/
public function doLoginSales()
{
if ($this->request->isPost()) {
$validate = new Validate([
// 'captcha' => 'require',
'user_login' => 'require',
'user_pass' => 'require',
]);
$validate->message([
'user_login.require' => '用户名不能为空',
'user_pass.require' => '密码不能为空',
// 'captcha.require' => '验证码不能为空',
]);
$data = $this->request->post();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
// if (!cmf_captcha_check($data['captcha'])) {
// $this->error('验证码错误');
// }
// $userModel = new UserModel();
$memberSalesModel = new MemberSalesModel();
// $user['code'] = $data['code'];
// if (Validate::is($data['username'], 'email')) {
// $user['user_email'] = $data['username'];
// $log = $userModel->doEmail($user);
// } else
// if (preg_match('/(^(1[3|4|5|8|9][0-9]\d{4,8})$/', $data['username'])) {
$user['user_login'] = $data['user_login'];
$user['user_pass'] = $data['user_pass'];
$log = $memberSalesModel->doName($user);
// }
// else {
// $user['user_login'] = $data['username'];
// $log = $userModel->doName($user);
// }
$session_login_http_referer = session('login_http_referer');
$redirect = empty($session_login_http_referer) ? $this->request->root() : $session_login_http_referer;
switch ($log) {
case 0:
cmf_user_action('login');
// $this->success('登录成功', $redirect);
$this->success('登录成功', url('portal/MemberSales/order_type'));
break;
case 1:
$this->error('登录账号或密码错误');
break;
case 2:
$this->error('账户不存在');
break;
case 3:
$this->error('账号被禁止访问系统');
break;
default :
$this->error('未受理的请求');
}
} else {
$this->error("请求错误");
}
}
/**
* 找回密码
*/
public function findPassword()
{
return $this->fetch('/find_password');
}
/**
* 用户密码重置
*/
public function passwordReset()
{
if ($this->request->isPost()) {
$validate = new Validate([
'captcha' => 'require',
'verification_code' => 'require',
'password' => 'require|min:6|max:32',
]);
$validate->message([
'verification_code.require' => '验证码不能为空',
'password.require' => '密码不能为空',
'password.max' => '密码不能超过32个字符',
'password.min' => '密码不能小于6个字符',
'captcha.require' => '验证码不能为空',
]);
$data = $this->request->post();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
if (!cmf_captcha_check($data['captcha'])) {
$this->error('验证码错误');
}
$errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
if (!empty($errMsg)) {
$this->error($errMsg);
}
$userModel = new UserModel();
if ($validate::is($data['username'], 'email')) {
$log = $userModel->emailPasswordReset($data['username'], $data['password']);
} else if (preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['username'])) {
$user['mobile'] = $data['username'];
$log = $userModel->mobilePasswordReset($data['username'], $data['password']);
} else {
$log = 2;
}
switch ($log) {
case 0:
$this->success('密码重置成功', $this->request->root());
break;
case 1:
$this->error("您的账户尚未注册");
break;
case 2:
$this->error("您输入的账号格式错误");
break;
default :
$this->error('未受理的请求');
}
} else {
$this->error("请求错误");
}
}
}