Download apps/api/src/modules/users/services/user.service.ts from qf46578/omnics: direct link, hf CLI and curl.
- Browser
- Download file 3.76 kB
-
https://huggingface.co/spaces/qf46578/omnics/resolve/main/apps/api/src/modules/users/services/user.service.ts
- Command line
-
hf download hf://spaces/qf46578/omnics/apps/api/src/modules/users/services/user.service.ts
-
curl -L -o user.service.ts https://huggingface.co/spaces/qf46578/omnics/resolve/main/apps/api/src/modules/users/services/user.service.ts
3.76 kB
| import { Injectable, Logger, ConflictException, NotFoundException } from '@nestjs/common'; | |
| import { PrismaService } from '../../../database/prisma.service'; | |
| import { RoleService } from '../../roles/services/role.service'; | |
| import * as bcrypt from 'bcryptjs'; | |
| export interface CreateUserDto { | |
| email: string; | |
| username: string; | |
| password: string; | |
| roleName: string; | |
| } | |
| export interface UserResponse { | |
| id: string; | |
| email: string; | |
| username: string; | |
| role: string; | |
| displayName: string; | |
| isActive: boolean; | |
| createdAt: Date; | |
| } | |
| /** | |
| * 用户服务 | |
| */ | |
| () | |
| export class UserService { | |
| private readonly logger = new Logger(UserService.name); | |
| constructor( | |
| private readonly prisma: PrismaService, | |
| private readonly roleService: RoleService, | |
| ) {} | |
| /** | |
| * 创建用户 | |
| */ | |
| async create(dto: CreateUserDto): Promise<UserResponse> { | |
| // 检查用户是否存在 | |
| const existing = await this.prisma.user.findFirst({ | |
| where: { | |
| OR: [{ email: dto.email }, { username: dto.username }], | |
| }, | |
| }); | |
| if (existing) { | |
| throw new ConflictException('用户已存在'); | |
| } | |
| // 获取角色 | |
| const role = await this.prisma.role.findUnique({ | |
| where: { name: dto.roleName }, | |
| }); | |
| if (!role) { | |
| throw new NotFoundException(`角色不存在: ${dto.roleName}`); | |
| } | |
| // 加密密码 | |
| const hashedPassword = await bcrypt.hash(dto.password, 10); | |
| // 创建用户 | |
| const user = await this.prisma.user.create({ | |
| data: { | |
| email: dto.email, | |
| username: dto.username, | |
| password: hashedPassword, | |
| roleId: role.id, | |
| }, | |
| include: { role: true }, | |
| }); | |
| this.logger.log(`用户创建成功: ${user.username}`); | |
| return { | |
| id: user.id, | |
| email: user.email, | |
| username: user.username, | |
| role: user.role.name, | |
| displayName: user.role.displayName, | |
| isActive: user.isActive, | |
| createdAt: user.createdAt, | |
| }; | |
| } | |
| /** | |
| * 验证用户 | |
| */ | |
| async validateUser(username: string, password: string): Promise<UserResponse | null> { | |
| const user = await this.prisma.user.findUnique({ | |
| where: { username }, | |
| include: { role: true }, | |
| }); | |
| if (!user || !user.isActive) { | |
| return null; | |
| } | |
| const isValid = await bcrypt.compare(password, user.password); | |
| if (!isValid) { | |
| return null; | |
| } | |
| return { | |
| id: user.id, | |
| email: user.email, | |
| username: user.username, | |
| role: user.role.name, | |
| displayName: user.role.displayName, | |
| isActive: user.isActive, | |
| createdAt: user.createdAt, | |
| }; | |
| } | |
| /** | |
| * 获取用户 | |
| */ | |
| async findById(id: string): Promise<UserResponse | null> { | |
| const user = await this.prisma.user.findUnique({ | |
| where: { id }, | |
| include: { role: true }, | |
| }); | |
| if (!user) return null; | |
| return { | |
| id: user.id, | |
| email: user.email, | |
| username: user.username, | |
| role: user.role.name, | |
| displayName: user.role.displayName, | |
| isActive: user.isActive, | |
| createdAt: user.createdAt, | |
| }; | |
| } | |
| /** | |
| * 获取所有用户 | |
| */ | |
| async findAll(): Promise<UserResponse[]> { | |
| const users = await this.prisma.user.findMany({ | |
| include: { role: true }, | |
| orderBy: { createdAt: 'desc' }, | |
| }); | |
| return users.map(user => ({ | |
| id: user.id, | |
| email: user.email, | |
| username: user.username, | |
| role: user.role.name, | |
| displayName: user.role.displayName, | |
| isActive: user.isActive, | |
| createdAt: user.createdAt, | |
| })); | |
| } | |
| /** | |
| * 更新用户状态 | |
| */ | |
| async updateStatus(id: string, isActive: boolean): Promise<void> { | |
| await this.prisma.user.update({ | |
| where: { id }, | |
| data: { isActive }, | |
| }); | |
| } | |
| } | |