优化用户名判断

优化昵称自动生成
pull/499/MERGE
Karson 2025-07-03 16:38:08 +08:00
parent 6b01a13288
commit 8751d5f9bd
5 changed files with 19 additions and 5 deletions

View File

@ -90,7 +90,7 @@ class User extends Api
//如果已经有账号则直接登录 //如果已经有账号则直接登录
$ret = $this->auth->direct($user->id); $ret = $this->auth->direct($user->id);
} else { } else {
$ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []); $ret = $this->auth->register('', Random::alnum(16), '', $mobile, []);
$this->auth->getUser()->save(['verification' => ['email' => 0, 'mobile' => 1]]); $this->auth->getUser()->save(['verification' => ['email' => 0, 'mobile' => 1]]);
} }
if ($ret) { if ($ret) {
@ -132,6 +132,9 @@ class User extends Api
if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) { if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
$this->error(__('Mobile is incorrect')); $this->error(__('Mobile is incorrect'));
} }
if (Validate::regex($username, '/^1\d{10}$/')) {
$this->error(__('Username can not be mobile'));
}
$ret = Sms::check($mobile, $code, 'register'); $ret = Sms::check($mobile, $code, 'register');
if (!$ret) { if (!$ret) {
$this->error(__('Captcha is incorrect')); $this->error(__('Captcha is incorrect'));

View File

@ -8,6 +8,7 @@ return [
'Username can not be empty' => '用户名不能为空', 'Username can not be empty' => '用户名不能为空',
'Username must be 3 to 30 characters' => '用户名必须3-30个字符', 'Username must be 3 to 30 characters' => '用户名必须3-30个字符',
'Username must be 6 to 30 characters' => '用户名必须6-30个字符', 'Username must be 6 to 30 characters' => '用户名必须6-30个字符',
'Username can not be mobile' => '用户名不能使用手机号',
'Password can not be empty' => '密码不能为空', 'Password can not be empty' => '密码不能为空',
'Password must be 6 to 30 characters' => '密码必须6-30个字符', 'Password must be 6 to 30 characters' => '密码必须6-30个字符',
'Mobile is incorrect' => '手机格式不正确', 'Mobile is incorrect' => '手机格式不正确',

View File

@ -134,22 +134,28 @@ class Auth
* @param array $extend 扩展参数 * @param array $extend 扩展参数
* @return boolean * @return boolean
*/ */
public function register($username, $password, $email = '', $mobile = '', $extend = []) public function register($username = '', $password = '', $email = '', $mobile = '', $extend = [])
{ {
//账号注册时需要开启事务,避免出现垃圾数据 //账号注册时需要开启事务,避免出现垃圾数据
Db::startTrans(); Db::startTrans();
try { try {
// 检测用户名、邮箱、手机号是否存在 $username = $username ?: Random::username();
if ($username && User::checkExists('username', $username)) { $password = $password ?: Random::alnum(16);
$nickname = $extend['nickname'] ?? '用户' . mb_substr($mobile ?: strtoupper(Random::alnum(4)), -4);
// 检测用户名
if (User::checkExists('username', $username)) {
$this->setError('Username already exist'); $this->setError('Username already exist');
Db::rollback(); Db::rollback();
return false; return false;
} }
// 检测邮箱
if ($email && User::checkExists('email', $email)) { if ($email && User::checkExists('email', $email)) {
$this->setError('Email already exist'); $this->setError('Email already exist');
Db::rollback(); Db::rollback();
return false; return false;
} }
// 检测手机号
if ($mobile && User::checkExists('mobile', $mobile)) { if ($mobile && User::checkExists('mobile', $mobile)) {
$this->setError('Mobile already exist'); $this->setError('Mobile already exist');
Db::rollback(); Db::rollback();
@ -169,7 +175,7 @@ class Auth
'avatar' => '', 'avatar' => '',
]; ];
$params = array_merge($data, [ $params = array_merge($data, [
'nickname' => preg_match("/^1[3-9]{1}\d{9}$/", $username) ? substr_replace($username, '****', 3, 4) : $username, 'nickname' => $nickname,
'salt' => Random::alnum(), 'salt' => Random::alnum(),
'jointime' => $time, 'jointime' => $time,
'joinip' => $ip, 'joinip' => $ip,

View File

@ -96,6 +96,9 @@ class User extends Frontend
'email' => 'Email is incorrect', 'email' => 'Email is incorrect',
'mobile' => 'Mobile is incorrect', 'mobile' => 'Mobile is incorrect',
]; ];
if (Validate::regex($username, '/^1\d{10}$/')) {
$this->error(__('Username can not be mobile'));
}
$data = [ $data = [
'username' => $username, 'username' => $username,
'password' => $password, 'password' => $password,

View File

@ -28,6 +28,7 @@ return [
'Username can not be empty' => '用户名不能为空', 'Username can not be empty' => '用户名不能为空',
'Username must be 3 to 30 characters' => '用户名必须3-30个字符', 'Username must be 3 to 30 characters' => '用户名必须3-30个字符',
'Username must be 6 to 30 characters' => '用户名必须6-30个字符', 'Username must be 6 to 30 characters' => '用户名必须6-30个字符',
'Username can not be mobile' => '用户名不能使用手机号',
'Account must be 3 to 50 characters' => '账户必须3-50个字符', 'Account must be 3 to 50 characters' => '账户必须3-50个字符',
'Password can not be empty' => '密码不能为空', 'Password can not be empty' => '密码不能为空',
'Password must be 6 to 30 characters' => '密码必须6-30个字符', 'Password must be 6 to 30 characters' => '密码必须6-30个字符',