注册支持用户名/手机号/邮箱 + 镜像站部署脚本 + AI 助手 Tool Calling 重构

- Prisma User 模型新增 username 字段(唯一索引)
- 注册先查重复再创建,返回友好中文提示(非 500)
- 登录支持用户名/手机号/邮箱三种方式
- 前端注册表单增加用户名输入框,预校验 2-20 位格式
- 新增 scripts/deploy.sh:一键构建并部署主站+镜像站+重启后端+重载 Nginx
- 镜像站 www.yuzhiran.com.cn Nginx 配置与主站同步
- AI 助手架构升级:用户端/管理后台均采用完整 Tool Calling 架构
- 新增 UserAiAssistantService(18 工具)+ AiAssistantController
- admin 助手新增 search + mark-all-notifications-read 工具
- 修复注册 500 错误:catch Prisma P2002 → BadRequestException
- Baidu Analytics Script 注入 root layout
This commit is contained in:
yuzhiran-dev
2026-06-01 23:14:42 +08:00
parent 6f3fe50ee0
commit 538de50bb1
329 changed files with 8777 additions and 868 deletions
+2 -1
View File
@@ -3,6 +3,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { RegisterDto } from './dto/register.dto';
import { LoginDto } from './dto/login.dto';
@ApiTags('认证')
@Controller('auth')
@@ -15,7 +16,7 @@ export class AuthController {
}
@Post('login')
async login(@Body() body: { account: string; password: string }) {
async login(@Body() body: LoginDto) {
return this.authService.login(body.account, body.password);
}
View File
+25 -6
View File
@@ -1,4 +1,4 @@
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { Injectable, UnauthorizedException, BadRequestException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import * as bcrypt from 'bcryptjs';
import { PrismaService } from '../../prisma/prisma.service';
@@ -10,14 +10,32 @@ export class AuthService {
private jwtService: JwtService,
) {}
async register(data: { phone?: string; email?: string; password: string; nickname?: string }) {
async register(data: { username?: string; phone?: string; email?: string; password: string; nickname?: string }) {
if (!data.username && !data.phone && !data.email) {
throw new BadRequestException('请填写用户名、手机号或邮箱');
}
if (data.username) {
const exists = await this.prisma.user.findUnique({ where: { username: data.username } });
if (exists) throw new BadRequestException('用户名已被注册');
}
if (data.phone) {
const exists = await this.prisma.user.findUnique({ where: { phone: data.phone } });
if (exists) throw new BadRequestException('手机号已被注册');
}
if (data.email) {
const exists = await this.prisma.user.findUnique({ where: { email: data.email } });
if (exists) throw new BadRequestException('邮箱已被注册');
}
const passwordHash = await bcrypt.hash(data.password, 10);
const user = await this.prisma.user.create({
data: {
phone: data.phone,
email: data.email,
username: data.username || null,
phone: data.phone || null,
email: data.email || null,
passwordHash,
nickname: data.nickname || data.phone || data.email?.split('@')[0],
nickname: data.nickname || data.username || data.phone || data.email?.split('@')[0],
},
});
return this.generateTokens(user.id);
@@ -26,7 +44,7 @@ export class AuthService {
async login(account: string, password: string) {
const user = await this.prisma.user.findFirst({
where: {
OR: [{ phone: account }, { email: account }],
OR: [{ username: account }, { phone: account }, { email: account }],
deletedAt: null,
},
});
@@ -68,6 +86,7 @@ export class AuthService {
where: { id: userId },
select: {
id: true,
username: true,
phone: true,
email: true,
nickname: true,
+11
View File
@@ -0,0 +1,11 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class LoginDto {
@IsNotEmpty({ message: '请输入账号' })
@IsString()
account: string;
@IsNotEmpty({ message: '请输入密码' })
@IsString()
password: string;
}
+7 -3
View File
@@ -1,12 +1,16 @@
import { IsNotEmpty, IsOptional, IsString, MinLength } from 'class-validator';
import { IsNotEmpty, IsOptional, IsString, MinLength, Matches } from 'class-validator';
export class RegisterDto {
@IsOptional()
@IsString()
@Matches(/^[a-zA-Z0-9_\u4e00-\u9fa5]{2,20}$/, { message: '用户名格式不正确(2-20位,支持中英文、数字、下划线)' })
username?: string;
@IsOptional()
@Matches(/^1[3-9]\d{9}$/, { message: '手机号格式不正确' })
phone?: string;
@IsOptional()
@IsString()
@Matches(/^[^\s@]+@[^\s@]+\.[^\s@]+$/, { message: '邮箱格式不正确' })
email?: string;
@IsNotEmpty({ message: '密码不能为空' })
View File
View File