diff --git a/application/api/controller/User.php b/application/api/controller/User.php index b74b9c5c..c7b2fa32 100644 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -90,7 +90,7 @@ class User extends Api //如果已经有账号则直接登录 $ret = $this->auth->direct($user->id); } 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]]); } if ($ret) { @@ -132,6 +132,9 @@ class User extends Api if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) { $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'); if (!$ret) { $this->error(__('Captcha is incorrect')); diff --git a/application/api/lang/zh-cn/user.php b/application/api/lang/zh-cn/user.php index 7cb4cb90..a511b0d2 100644 --- a/application/api/lang/zh-cn/user.php +++ b/application/api/lang/zh-cn/user.php @@ -8,6 +8,7 @@ return [ 'Username can not be empty' => '用户名不能为空', 'Username must be 3 to 30 characters' => '用户名必须3-30个字符', 'Username must be 6 to 30 characters' => '用户名必须6-30个字符', + 'Username can not be mobile' => '用户名不能使用手机号', 'Password can not be empty' => '密码不能为空', 'Password must be 6 to 30 characters' => '密码必须6-30个字符', 'Mobile is incorrect' => '手机格式不正确', diff --git a/application/common/library/Auth.php b/application/common/library/Auth.php index 45f74995..104c9d6a 100644 --- a/application/common/library/Auth.php +++ b/application/common/library/Auth.php @@ -134,22 +134,28 @@ class Auth * @param array $extend 扩展参数 * @return boolean */ - public function register($username, $password, $email = '', $mobile = '', $extend = []) + public function register($username = '', $password = '', $email = '', $mobile = '', $extend = []) { //账号注册时需要开启事务,避免出现垃圾数据 Db::startTrans(); try { - // 检测用户名、邮箱、手机号是否存在 - if ($username && User::checkExists('username', $username)) { + $username = $username ?: Random::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'); Db::rollback(); return false; } + // 检测邮箱 if ($email && User::checkExists('email', $email)) { $this->setError('Email already exist'); Db::rollback(); return false; } + // 检测手机号 if ($mobile && User::checkExists('mobile', $mobile)) { $this->setError('Mobile already exist'); Db::rollback(); @@ -169,7 +175,7 @@ class Auth 'avatar' => '', ]; $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(), 'jointime' => $time, 'joinip' => $ip, diff --git a/application/index/controller/User.php b/application/index/controller/User.php index fef694fe..a12ee7e6 100644 --- a/application/index/controller/User.php +++ b/application/index/controller/User.php @@ -96,6 +96,9 @@ class User extends Frontend 'email' => 'Email is incorrect', 'mobile' => 'Mobile is incorrect', ]; + if (Validate::regex($username, '/^1\d{10}$/')) { + $this->error(__('Username can not be mobile')); + } $data = [ 'username' => $username, 'password' => $password, diff --git a/application/index/lang/zh-cn/user.php b/application/index/lang/zh-cn/user.php index 8c9763b5..b9089b7e 100755 --- a/application/index/lang/zh-cn/user.php +++ b/application/index/lang/zh-cn/user.php @@ -28,6 +28,7 @@ return [ 'Username can not be empty' => '用户名不能为空', 'Username must be 3 to 30 characters' => '用户名必须3-30个字符', 'Username must be 6 to 30 characters' => '用户名必须6-30个字符', + 'Username can not be mobile' => '用户名不能使用手机号', 'Account must be 3 to 50 characters' => '账户必须3-50个字符', 'Password can not be empty' => '密码不能为空', 'Password must be 6 to 30 characters' => '密码必须6-30个字符',