From 211a54b4c36da27fef8027ee43f15d56ee91e61d Mon Sep 17 00:00:00 2001 From: Karson Date: Fri, 6 Jun 2025 23:47:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BC=9A=E5=91=98=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化mobilelogin默认用户名和默认昵称 --- application/common/library/Auth.php | 85 +++++++++++++-------------- application/common/model/User.php | 10 ++++ application/index/controller/User.php | 12 +++- extend/fast/Random.php | 9 +++ 4 files changed, 70 insertions(+), 46 deletions(-) diff --git a/application/common/library/Auth.php b/application/common/library/Auth.php index 17e95586..63dc7459 100644 --- a/application/common/library/Auth.php +++ b/application/common/library/Auth.php @@ -133,52 +133,51 @@ class Auth */ public function register($username, $password, $email = '', $mobile = '', $extend = []) { - // 检测用户名、昵称、邮箱、手机号是否存在 - if (User::getByUsername($username)) { - $this->setError('Username already exist'); - return false; - } - if (User::getByNickname($username)) { - $this->setError('Nickname already exist'); - return false; - } - if ($email && User::getByEmail($email)) { - $this->setError('Email already exist'); - return false; - } - if ($mobile && User::getByMobile($mobile)) { - $this->setError('Mobile already exist'); - return false; - } - - $ip = request()->ip(); - $time = time(); - - $data = [ - 'username' => $username, - 'password' => $password, - 'email' => $email, - 'mobile' => $mobile, - 'level' => 1, - 'score' => 0, - 'avatar' => '', - ]; - $params = array_merge($data, [ - 'nickname' => preg_match("/^1[3-9]{1}\d{9}$/", $username) ? substr_replace($username, '****', 3, 4) : $username, - 'salt' => Random::alnum(), - 'jointime' => $time, - 'joinip' => $ip, - 'logintime' => $time, - 'loginip' => $ip, - 'prevtime' => $time, - 'status' => 'normal' - ]); - $params['password'] = $this->getEncryptPassword($password, $params['salt']); - $params = array_merge($params, $extend); - //账号注册时需要开启事务,避免出现垃圾数据 Db::startTrans(); try { + // 检测用户名、邮箱、手机号是否存在 + if ($username && 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(); + return false; + } + + $ip = request()->ip(); + $time = time(); + + $data = [ + 'username' => $username, + 'password' => $password, + 'email' => $email, + 'mobile' => $mobile, + 'level' => 1, + 'score' => 0, + 'avatar' => '', + ]; + $params = array_merge($data, [ + 'nickname' => preg_match("/^1[3-9]{1}\d{9}$/", $username) ? substr_replace($username, '****', 3, 4) : $username, + 'salt' => Random::alnum(), + 'jointime' => $time, + 'joinip' => $ip, + 'logintime' => $time, + 'loginip' => $ip, + 'prevtime' => $time, + 'status' => 'normal' + ]); + $params['password'] = $this->getEncryptPassword($password, $params['salt']); + $params = array_merge($params, $extend); + $user = User::create($params, true); $this->_user = User::get($user->id); diff --git a/application/common/model/User.php b/application/common/model/User.php index 44a4a838..64211c02 100644 --- a/application/common/model/User.php +++ b/application/common/model/User.php @@ -82,6 +82,16 @@ class User extends Model return $value; } + /** + * 判断指定字段的值是否存在 + * @param string $field 字段名 + * @param string $value 字段值 + */ + public static function checkExists($field, $value) + { + return self::lock(true)->where($field, $value)->find(); + } + /** * 变更会员余额 * @param int $money 余额 diff --git a/application/index/controller/User.php b/application/index/controller/User.php index dc7e6d1d..fef694fe 100644 --- a/application/index/controller/User.php +++ b/application/index/controller/User.php @@ -224,9 +224,9 @@ class User extends Frontend //如果已经有账号则直接登录 $ret = $this->auth->direct($user->id); } else { - $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []); - //如果是手机号首次注册则直接设定为已验证 - $this->auth->getUser()->save(['verification' => ['email' => 0, 'mobile' => 1]]); + $username = \fast\Random::username(); + $nickname = '用户' . substr($mobile, -4); + $ret = $this->auth->register($username, Random::alnum(), '', $mobile, ['nickname' => $nickname, 'verification' => ['mobile' => 1]]); } if ($ret) { Sms::flush($mobile, 'mobilelogin'); @@ -321,6 +321,9 @@ class User extends Frontend return $this->view->fetch(); } + /** + * 附件管理 + */ public function attachment() { //设置过滤方法 @@ -388,6 +391,9 @@ class User extends Frontend return $this->view->fetch(); } + /** + * 用户协议 + */ public function agreement() { $this->view->assign('title', __('User agreement')); diff --git a/extend/fast/Random.php b/extend/fast/Random.php index db1f6f9a..a9c3b4f8 100644 --- a/extend/fast/Random.php +++ b/extend/fast/Random.php @@ -89,6 +89,15 @@ class Random } } + /** + * 生成随机用户名 + * @return string + */ + public static function username(): string + { + return 'user_'.bin2hex(random_bytes(8)) . substr(uniqid(), -8); + } + /** * 获取全球唯一标识 * @return string