优化会员注册模块

优化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,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);

View File

@ -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 余额

View File

@ -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'));

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