import { Injectable } from '@nestjs/common'; import bcrypt from 'bcryptjs'; import { BaseCrudService } from '../common/base-crud.service.js'; import { PrismaService } from '../prisma/prisma.service.js'; import { getRequestContext } from '../common/context/request-context.js'; import { CreateUserDto, ResetPasswordDto, UpdateUserDto } from './dto.js'; import { PageQuery } from '../common/page.dto.js'; @Injectable() export class UsersService extends BaseCrudService { constructor(prisma: PrismaService) { super(prisma, 'user'); } async createUser(dto: CreateUserDto) { const passwordHash = await bcrypt.hash(dto.password, 10); const { password: _p, ...rest } = dto; return this.create({ ...rest, passwordHash }); } async listUsers(q: PageQuery) { const ctx = getRequestContext(); const where: Record = {}; // 数据权限:orgCodes 为空表示「全部可见」,否则限制在可见部门集合 if (ctx && ctx.orgCodes.length > 0) { where.orgCode = { in: ctx.orgCodes }; } return this.paginate(q.page, q.pageSize, where); } async updateUser(id: string, dto: UpdateUserDto) { return this.update(id, { ...dto }); } async resetPassword(id: string, dto: ResetPasswordDto) { const passwordHash = await bcrypt.hash(dto.password, 10); return this.update(id, { passwordHash }); } }