优化会员注册模块

优化mobilelogin默认用户名和默认昵称
pull/515/head
Karson 2025-06-06 23:47:22 +08:00
parent 4e48b3a736
commit 211a54b4c3
4 changed files with 70 additions and 46 deletions

View File

@ -133,21 +133,23 @@ class Auth
*/ */
public function register($username, $password, $email = '', $mobile = '', $extend = []) public function register($username, $password, $email = '', $mobile = '', $extend = [])
{ {
// 检测用户名、昵称、邮箱、手机号是否存在 //账号注册时需要开启事务,避免出现垃圾数据
if (User::getByUsername($username)) { Db::startTrans();
try {
// 检测用户名、邮箱、手机号是否存在
if ($username && User::checkExists('username', $username)) {
$this->setError('Username already exist'); $this->setError('Username already exist');
Db::rollback();
return false; return false;
} }
if (User::getByNickname($username)) { if ($email && User::checkExists('email', $email)) {
$this->setError('Nickname already exist');
return false;
}
if ($email && User::getByEmail($email)) {
$this->setError('Email already exist'); $this->setError('Email already exist');
Db::rollback();
return false; return false;
} }
if ($mobile && User::getByMobile($mobile)) { if ($mobile && User::checkExists('mobile', $mobile)) {
$this->setError('Mobile already exist'); $this->setError('Mobile already exist');
Db::rollback();
return false; return false;
} }
@ -176,9 +178,6 @@ class Auth
$params['password'] = $this->getEncryptPassword($password, $params['salt']); $params['password'] = $this->getEncryptPassword($password, $params['salt']);
$params = array_merge($params, $extend); $params = array_merge($params, $extend);
//账号注册时需要开启事务,避免出现垃圾数据
Db::startTrans();
try {
$user = User::create($params, true); $user = User::create($params, true);
$this->_user = User::get($user->id); $this->_user = User::get($user->id);

View File

@ -82,6 +82,16 @@ class User extends Model
return $value; 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 余额 * @param int $money 余额

View File

@ -224,9 +224,9 @@ class User extends Frontend
//如果已经有账号则直接登录 //如果已经有账号则直接登录
$ret = $this->auth->direct($user->id); $ret = $this->auth->direct($user->id);
} else { } else {
$ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []); $username = \fast\Random::username();
//如果是手机号首次注册则直接设定为已验证 $nickname = '用户' . substr($mobile, -4);
$this->auth->getUser()->save(['verification' => ['email' => 0, 'mobile' => 1]]); $ret = $this->auth->register($username, Random::alnum(), '', $mobile, ['nickname' => $nickname, 'verification' => ['mobile' => 1]]);
} }
if ($ret) { if ($ret) {
Sms::flush($mobile, 'mobilelogin'); Sms::flush($mobile, 'mobilelogin');
@ -321,6 +321,9 @@ class User extends Frontend
return $this->view->fetch(); return $this->view->fetch();
} }
/**
* 附件管理
*/
public function attachment() public function attachment()
{ {
//设置过滤方法 //设置过滤方法
@ -388,6 +391,9 @@ class User extends Frontend
return $this->view->fetch(); return $this->view->fetch();
} }
/**
* 用户协议
*/
public function agreement() public function agreement()
{ {
$this->view->assign('title', __('User agreement')); $this->view->assign('title', __('User agreement'));

View File

@ -89,6 +89,15 @@ class Random
} }
} }
/**
* 生成随机用户名
* @return string
*/
public static function username(): string
{
return 'user_'.bin2hex(random_bytes(8)) . substr(uniqid(), -8);
}
/** /**
* 获取全球唯一标识 * 获取全球唯一标识
* @return string * @return string