RegisterController.php
4.0 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
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;
use cmf\controller\HomeBaseController;
use think\Validate;
use app\user\model\UserModel;
class RegisterController extends HomeBaseController
{
/**
* 前台用户注册
*/
public function index()
{
$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($this->request->root() . '/');
} else {
return $this->fetch(":register");
}
}
/**
* 前台用户注册提交
*/
public function doRegister()
{
if ($this->request->isPost()) {
$rules = [
'captcha' => 'require',
'code' => 'require',
'password' => 'require|min:6|max:32',
];
$isOpenRegistration = cmf_is_open_registration();
if ($isOpenRegistration) {
unset($rules['code']);
}
$validate = new Validate($rules);
$validate->message([
'code.require' => '验证码不能为空',
'password.require' => '密码不能为空',
'password.max' => '密码不能超过32个字符',
'password.min' => '密码不能小于6个字符',
'captcha.require' => '验证码不能为空',
]);
$data = $this->request->post();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$captchaId = empty($data['_captcha_id']) ? '' : $data['_captcha_id'];
if (!cmf_captcha_check($data['captcha'], $captchaId)) {
$this->error('验证码错误');
}
if (!$isOpenRegistration) {
$errMsg = cmf_check_verification_code($data['username'], $data['code']);
if (!empty($errMsg)) {
$this->error($errMsg);
}
}
$register = new UserModel();
$user['user_pass'] = $data['password'];
if (Validate::is($data['username'], 'email')) {
$user['user_email'] = $data['username'];
$log = $register->registerEmail($user);
} else if (preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['username'])) {
$user['mobile'] = $data['username'];
$log = $register->registerMobile($user);
} else {
$log = 2;
}
$sessionLoginHttpReferer = session('login_http_referer');
$redirect = empty($sessionLoginHttpReferer) ? cmf_get_root() . '/' : $sessionLoginHttpReferer;
switch ($log) {
case 0:
$this->success('注册成功', $redirect);
break;
case 1:
$this->error("您的账户已注册过");
break;
case 2:
$this->error("您输入的账号格式错误");
break;
default :
$this->error('未受理的请求');
}
} else {
$this->error("请求错误");
}
}
}