import { Controller, Get, UsePipes, UseGuards, Query, Post, Body, Delete, Param, Put, NotFoundException, } from '@nestjs/common'; import { ApiBearerAuth, ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger'; import { InjectRepository } from '@nestjs/typeorm'; import { DataSource, Repository, } from 'typeorm'; import { AuthGuard } from '@nestjs/passport'; import * as bcrypt from 'bcryptjs'; import { ValidationPipe, User, Roles, RolesGuard, BCRYPT_HASH_ROUNDS, } from '../common'; import { UserEntity } from './user.entity'; import { UserService } from './user.service'; import { QueryUserDto } from './query-user.dto'; import { CreateUser, UpdatePassword, UpdateUser } from './update-user.dto'; import { RoleEntity } from 'src/role/entities/role.entity'; @ApiTags('系统用户') @ApiBearerAuth() @Controller('users') export class UserController { constructor( @InjectRepository(UserEntity) private readonly userRepository: Repository, @InjectRepository(RoleEntity) private readonly roleRepository: Repository, private readonly userService: UserService, private readonly dataSource: DataSource, // @Inject(forwardRef(() => AuthService)) // private readonly authService: AuthService, ) { } @Get('/viewer') @ApiOperation({ summary: '获取个人信息' }) @UseGuards(AuthGuard('jwt'), RolesGuard) @UsePipes(new ValidationPipe()) // @Roles('company', 'tester', 'admin') @ApiBearerAuth() @ApiResponse({ status: 200, description: '返回参数说明', }) async viewer(@User() viewer: UserEntity) { const result = await this.userRepository.findOne({ where: { id: viewer.id } }) const role = await this.roleRepository.findOne({ where: { id: result.role_id } }) if (role) { (result as any).permissions = role.permissions.split('、'); } return result; } @Get("/list") @ApiOperation({ summary: '查看管理员' }) @UseGuards(AuthGuard('jwt'), RolesGuard) @UsePipes(new ValidationPipe()) @Roles('super_admin', '查看管理员') @ApiBearerAuth() async areas(@Query() query_data: QueryUserDto) { const where: any = {}; if (query_data.true_name) { where['true_name'] = query_data.true_name; } const query = this.userRepository.createQueryBuilder('user'); query.where(where); if (query_data.search) { const string = `%${query_data.search}%`; const fields = ['true_name']; const searchString = fields.join(' like :search OR user.'); query.where(`user.${searchString} like :search`, { search: string, }); } const order_key = 'user.created_date'; let order_value: any = 'DESC'; const [list, count] = await query .skip(query_data.skip) .take(query_data.take) .orderBy(order_key, order_value) .getManyAndCount(); return { list, count } } @Post("/") @ApiOperation({ summary: '创建管理员' }) @UseGuards(AuthGuard('jwt'), RolesGuard) @UsePipes(new ValidationPipe()) @Roles('super_admin', '创建管理员') @ApiBearerAuth() async createUser(@Body() data: CreateUser) { const new_password = await bcrypt.hash(data.password, BCRYPT_HASH_ROUNDS); return await this.dataSource.transaction(async transactionalEntityManager => { const user = new UserEntity(); user.true_name = data.true_name; user.role = data.role; user.role_id = data.role_id; user.mobile = data.mobile; user.avatar = data.avatar; user.area_id = data.area_id; user.area = data.area; user.password = new_password; const result = await transactionalEntityManager.save(user); return { statusCode: 201, data: result }; }); } @Put("/:id") @ApiOperation({ summary: '更新管理员' }) @UseGuards(AuthGuard('jwt'), RolesGuard) @UsePipes(new ValidationPipe()) @Roles('super_admin', '更新管理员') @ApiBearerAuth() async updateUser(@Param() { id }: any, @Body() data: UpdateUser) { const user = await this.userRepository.findOne({ where: { id } }); if (!user) { throw new NotFoundException("user not found"); } user.true_name = data.true_name; user.role = data.role; user.role_id = data.role_id; user.mobile = data.mobile; user.avatar = data.avatar; user.area_id = data.area_id; user.area = data.area; const result = await this.userRepository.save(user); return result; } @Put("/:id/password") @ApiOperation({ summary: '更新管理员' }) @UseGuards(AuthGuard('jwt'), RolesGuard) @UsePipes(new ValidationPipe()) @Roles('super_admin', '更新管理员') @ApiBearerAuth() async passwordUser(@Param() { id }: any, @Body() data: UpdatePassword) { const user = await this.userRepository.findOne({ where: { id } }); if (!user) { throw new NotFoundException("user not found"); } const new_password = await bcrypt.hash(data.password, BCRYPT_HASH_ROUNDS); user.password = new_password; const result = await this.userRepository.save(user); return result; } @Delete("/:id") @ApiOperation({ summary: '删除管理员' }) @UseGuards(AuthGuard('jwt'), RolesGuard) @UsePipes(new ValidationPipe()) @Roles('super_admin', '删除管理员') @ApiBearerAuth() async deleteUser(@Param() { id }: any) { const result = await this.userRepository.findOne({ where: { id } }); if (!result) { throw new NotFoundException("user not found"); } await this.userRepository.delete(id); return result; } }