特性:优化注册与改名卡逻辑,在触发敏感词或拦截重名时明确提示具体是触发了哪个词汇

This commit is contained in:
2026-04-02 16:38:17 +08:00
parent 63292ab810
commit c4edda8b4e
3 changed files with 45 additions and 26 deletions

View File

@@ -101,8 +101,13 @@ class AuthController extends Controller
}
// 检测用户名是否在禁用词列表(永久禁用 或 改名临时保留期内)
if (UsernameBlacklist::isBlocked($username)) {
return response()->json(['status' => 'error', 'message' => '该用户名已被系统禁止注册,请更换其他名称。'], 422);
if ($blockingRecord = UsernameBlacklist::getBlockingRecord($username)) {
$reason = '';
if ($blockingRecord->type === 'permanent') {
$reason = "(包含违禁敏感词:{$blockingRecord->username}";
}
return response()->json(['status' => 'error', 'message' => "该用户名已被系统禁止注册{$reason},请更换其他名称。"], 422);
}
// --- 提取邀请人 Cookie ---

View File

@@ -40,34 +40,41 @@ class UsernameBlacklist extends Model
// ──────────────────────────────────────────
/**
* 判断给定名称是否被禁止使用
* 获取拦截该名称的具体黑名单记录(如果有)
*
* 满足以下任一条件时返回 true
* 1. 存在 type=permanent 的永久禁用记录
* 2. 存在 type=temp reserved_until 尚未过期的临时保留记录
* @param string $username 要检测的用户名
* @return static|null
*/
public static function getBlockingRecord(string $username): ?self
{
// 1. 检查是否存在精确匹配且未过期的“临时改名保留”名称
$tempRecord = static::where('type', 'temp')
->where('username', $username)
->where('reserved_until', '>', now())
->first();
if ($tempRecord) {
return $tempRecord;
}
// 2. 检查永久禁用词,改为【模糊匹配】(只要新注册的名字中包含禁用词,拦截)
// 比如数据库禁用了 "admin",那么 "admin123" 也会触发拦截
$permanentRecord = static::where('type', 'permanent')
->where('username', '!=', '')
->whereRaw('? LIKE CONCAT("%", username, "%")', [$username])
->first();
return $permanentRecord;
}
/**
* 判断给定名称是否被禁止使用。
*
* @param string $username 要检测的用户名
*/
public static function isBlocked(string $username): bool
{
// 1. 检查是否存在精确匹配且未过期的“临时改名保留”名称
$hasTemp = static::where('type', 'temp')
->where('username', $username)
->where('reserved_until', '>', now())
->exists();
if ($hasTemp) {
return true;
}
// 2. 检查永久禁用词,改为【模糊匹配】(只要新注册的名字中包含禁用词,拦截)
// 比如数据库禁用了 "admin",那么 "admin123" 也会触发拦截
$hasPermanent = static::where('type', 'permanent')
->where('username', '!=', '')
->whereRaw('? LIKE CONCAT("%", username, "%")', [$username])
->exists();
return $hasPermanent;
return static::getBlockingRecord($username) !== null;
}
/**

View File

@@ -151,8 +151,15 @@ class ShopService
}
// 不能在黑名单保留期内
if (UsernameBlacklist::isReserved($newName)) {
return ['ok' => false, 'message' => '该昵称处于保护期,暂时无法使用。'];
if ($blockingRecord = UsernameBlacklist::getBlockingRecord($newName)) {
$reason = '';
if ($blockingRecord->type === 'permanent') {
$reason = "(包含敏感词:{$blockingRecord->username}";
} else {
$reason = '(处于曾用名保护期)';
}
return ['ok' => false, 'message' => "该昵称已被系统禁止使用{$reason}"];
}
// 查找有效的改名卡记录