实现管理段用户增删改查改密码

This commit is contained in:
2025-05-08 22:07:06 +08:00
parent 8e33f1b61b
commit d2287bc363
6 changed files with 132 additions and 3 deletions

View File

@@ -1,7 +1,9 @@
import { Injectable } from '@nestjs/common';
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { Repository } from 'typeorm';
import { createHash } from 'crypto';
import { v4 as uuid } from 'uuid';
type UserFindOptions = Partial<Pick<User, 'userId' | 'username' | 'phone' | 'email'>>;
@@ -23,4 +25,40 @@ export class UserService {
const newUser = this.userRepository.create(user);
return this.userRepository.save(newUser);
}
async update(userId: string, user: Partial<User>): Promise<User> {
const existingUser = await this.userRepository.findOne({ where: { userId } });
if (!existingUser) {
throw new BadRequestException('User not found');
}
Object.assign(existingUser, user);
return this.userRepository.save(existingUser);
}
async delete(userId: string): Promise<void> {
const existingUser = await this.userRepository.findOne({ where: { userId } });
if (!existingUser) {
throw new BadRequestException('User not found');
}
await this.userRepository.softDelete(existingUser.id);
}
hashPassword(password: string, salt: string): string {
return createHash('sha256').update(`${password}${salt}`).digest('hex');
}
generateSalt(): string {
return uuid().replace(/-/g, '');
}
async setPassword(userId: string, password: string): Promise<User> {
const user = await this.userRepository.findOne({ where: { userId } });
if (!user) {
throw new BadRequestException('User not found');
}
const salt = this.generateSalt();
user.password_hash = this.hashPassword(password, salt);
user.salt = salt;
return this.userRepository.save(user);
}
}