Skip to content

Commit fa1416d

Browse files
committed
refactor:优化公共权限类的会员注册方法
1 parent cac2770 commit fa1416d

File tree

8 files changed

+36
-35
lines changed

8 files changed

+36
-35
lines changed

app/api/lang/en/account.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
<?php
22
return [
3-
'username' => 'Username',
43
'nickname' => 'Nickname',
54
'birthday' => 'Birthday',
6-
'email' => 'E-mail',
7-
'mobile' => 'Mobile Number',
8-
'password' => 'Password',
95
'captcha' => 'Captcha',
106
'Old password error' => 'Old password error',
117
'Data updated successfully~' => 'Data updated successfully',

app/api/lang/en/user.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<?php
22
return [
3-
'username' => 'Username',
4-
'email' => 'E-mail',
5-
'mobile' => 'Mobile Number',
6-
'password' => 'Password',
73
'captcha' => 'Captcha',
84
'captchaId' => 'Captcha ID',
95
'Please input correct username' => 'Please enter the correct username.',

app/api/lang/zh-cn.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
'Topic format error' => '上传存储子目录格式错误!',
2727
'Driver %s not supported' => '不支持的驱动:%s',
2828
// 文件上传-e
29+
'Username' => '用户名',
30+
'Email' => '邮箱',
31+
'Mobile' => '手机号',
32+
'Password' => '密码',
2933
'Login expired, please login again.' => '登录过期,请重新登录。',
3034
'Account not exist' => '帐户不存在',
3135
'Account disabled' => '帐户已禁用',

app/api/lang/zh-cn/account.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
<?php
22
return [
3-
'username' => '用户名',
43
'nickname' => '昵称',
54
'birthday' => '生日',
6-
'email' => '电子邮箱',
7-
'mobile' => '手机号',
8-
'password' => '密码',
95
'captcha' => '验证码',
106
'Old password error' => '旧密码错误',
117
'Data updated successfully~' => '资料更新成功~',

app/api/lang/zh-cn/user.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<?php
22
return [
3-
'username' => '用户名',
4-
'email' => '邮箱',
5-
'mobile' => '手机号',
6-
'password' => '密码',
73
'captcha' => '验证码',
84
'captchaId' => '验证码标识',
95
'Register type' => '注册类型',

app/api/validate/Account.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ class Account extends Validate
3131
public function __construct()
3232
{
3333
$this->field = [
34-
'username' => __('username'),
34+
'username' => __('Username'),
35+
'email' => __('Email'),
36+
'mobile' => __('Mobile'),
37+
'password' => __('Password'),
3538
'nickname' => __('nickname'),
3639
'birthday' => __('birthday'),
37-
'email' => __('email'),
38-
'mobile' => __('mobile'),
39-
'password' => __('password'),
4040
];
4141
$this->message = array_merge($this->message, [
4242
'nickname.chsDash' => __('nicknameChsDash'),

app/api/validate/User.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ public function sceneLogin(): User
4949
public function __construct()
5050
{
5151
$this->field = [
52-
'username' => __('username'),
53-
'email' => __('email'),
54-
'mobile' => __('mobile'),
55-
'password' => __('password'),
52+
'username' => __('Username'),
53+
'email' => __('Email'),
54+
'mobile' => __('Mobile'),
55+
'password' => __('Password'),
5656
'captcha' => __('captcha'),
5757
'captchaId' => __('captchaId'),
5858
'captchaInfo' => __('captcha'),

app/common/library/Auth.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,22 @@ public function init($token): bool
167167
}
168168

169169
/**
170-
* 会员注册
170+
* 会员注册,可使用关键词参数方式调用:$auth->register('u18888888888', email: '[email protected]')
171171
* @param string $username
172172
* @param string $password
173173
* @param string $mobile
174174
* @param string $email
175-
* @param int $group
176-
* @param array $extend
175+
* @param int $group 会员分组 ID 号
176+
* @param array $extend 扩展数据,如 ['status' => 'disable']
177177
* @return bool
178178
*/
179-
public function register(string $username, string $password, string $mobile = '', string $email = '', int $group = 1, array $extend = []): bool
179+
public function register(string $username, string $password = '', string $mobile = '', string $email = '', int $group = 1, array $extend = []): bool
180180
{
181181
$validate = Validate::rule([
182-
'mobile' => 'mobile|unique:user',
183-
'email' => 'email|unique:user',
184-
'username' => 'regex:^[a-zA-Z][a-zA-Z0-9_]{2,15}$|unique:user',
185-
'password' => 'regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
182+
'email|' . __('Email') => 'email|unique:user',
183+
'mobile|' . __('Mobile') => 'mobile|unique:user',
184+
'username|' . __('Username') => 'require|regex:^[a-zA-Z][a-zA-Z0-9_]{2,15}$|unique:user',
185+
'password|' . __('Password') => 'regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
186186
]);
187187
$params = [
188188
'username' => $username,
@@ -191,26 +191,39 @@ public function register(string $username, string $password, string $mobile = ''
191191
'email' => $email,
192192
];
193193
if (!$validate->check($params)) {
194-
$this->setError('Registration parameter error');
194+
$this->setError($validate->getError());
195195
return false;
196196
}
197197

198+
// 按需生成随机密码
199+
if (!$password) {
200+
$password = Random::build();
201+
}
202+
203+
// 用户昵称
204+
$nickname = preg_replace_callback('/1[3-9]\d{9}/', function ($matches) {
205+
// 对 username 中出现的所有手机号进行脱敏处理
206+
$mobile = $matches[0];
207+
return substr($mobile, 0, 3) . '****' . substr($mobile, 7);
208+
}, $username);
209+
198210
$ip = request()->ip();
199211
$time = time();
200212
$salt = Random::build('alnum', 16);
201213
$data = [
202214
'password' => encrypt_password($password, $salt),
203215
'group_id' => $group,
204-
'nickname' => preg_match("/^1[3-9]\d{9}$/", $username) ? substr_replace($username, '****', 3, 4) : $username,
216+
'nickname' => $nickname,
205217
'join_ip' => $ip,
206218
'join_time' => $time,
207219
'last_login_ip' => $ip,
208220
'last_login_time' => $time,
209221
'salt' => $salt,
210-
'status' => 'enable',
222+
'status' => 'enable', // 状态:enable=启用,disable=禁用,使用 string 存储可以自定义其他状态
211223
];
212224
$data = array_merge($params, $data);
213225
$data = array_merge($data, $extend);
226+
214227
Db::startTrans();
215228
try {
216229
$this->model = User::create($data);

0 commit comments

Comments
 (0)