From 2821dc6dadb11c0784bf460a2872571e235b4996 Mon Sep 17 00:00:00 2001 From: louis <869322496@qq.com> Date: Wed, 28 Feb 2024 15:07:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=86=E9=A1=B5=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=97=B6=E6=9F=A5=E8=AF=A2=E6=9D=A1=E4=BB=B6=E7=9A=84number?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=A9=BA=E5=80=BC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 2 +- src/modules/system/menu/menu.service.ts | 4 ++-- src/modules/system/role/role.controller.ts | 4 ++-- src/modules/system/role/role.dto.ts | 12 ++++++++++- src/modules/system/role/role.service.ts | 24 +++++++++++++++++----- src/modules/system/task/task.service.ts | 4 ++-- src/modules/user/dto/user.dto.ts | 3 ++- src/modules/user/user.service.ts | 4 ++-- src/shared/database/typeorm-logger.ts | 2 +- 9 files changed, 42 insertions(+), 17 deletions(-) diff --git a/.env.development b/.env.development index ce6ed42..b33890f 100644 --- a/.env.development +++ b/.env.development @@ -19,7 +19,7 @@ DB_DATABASE = hxoa DB_USERNAME = root DB_PASSWORD = huaxin123 DB_SYNCHRONIZE = true -DB_LOGGING = ["error"] +DB_LOGGING = "all" # redis REDIS_PORT = 6379 diff --git a/src/modules/system/menu/menu.service.ts b/src/modules/system/menu/menu.service.ts index aa288cd..85ce773 100644 --- a/src/modules/system/menu/menu.service.ts +++ b/src/modules/system/menu/menu.service.ts @@ -2,7 +2,7 @@ import { InjectRedis } from '@liaoliaots/nestjs-redis' import { Injectable } from '@nestjs/common' import { InjectRepository } from '@nestjs/typeorm' import Redis from 'ioredis' -import { concat, isEmpty, uniq } from 'lodash' +import { concat, isEmpty, isNumber, uniq } from 'lodash' import { In, IsNull, Like, Not, Repository } from 'typeorm' @@ -45,7 +45,7 @@ export class MenuService { ...(path && { path: Like(`%${path}%`) }), ...(permission && { permission: Like(`%${permission}%`) }), ...(component && { component: Like(`%${component}%`) }), - ...(status && { status }), + ...(isNumber(status) ? { status } : null), }, order: { orderNo: 'ASC' }, }) diff --git a/src/modules/system/role/role.controller.ts b/src/modules/system/role/role.controller.ts index c3ac95b..1f5b7a0 100644 --- a/src/modules/system/role/role.controller.ts +++ b/src/modules/system/role/role.controller.ts @@ -19,7 +19,7 @@ import { RoleEntity } from '~/modules/system/role/role.entity' import { MenuService } from '../menu/menu.service' -import { RoleDto, RoleUpdateDto } from './role.dto' +import { RoleDto, RoleQueryDto, RoleUpdateDto } from './role.dto' import { RoleInfo } from './role.model' import { RoleService } from './role.service' @@ -44,7 +44,7 @@ export class RoleController { @ApiOperation({ summary: '获取角色列表' }) @ApiResult({ type: [RoleEntity], isPage: true }) @Perm(permissions.LIST) - async list(@Query() dto: PagerDto) { + async list(@Query() dto: RoleQueryDto) { return this.roleService.findAll(dto) } diff --git a/src/modules/system/role/role.dto.ts b/src/modules/system/role/role.dto.ts index 66d2178..2e9d0e2 100644 --- a/src/modules/system/role/role.dto.ts +++ b/src/modules/system/role/role.dto.ts @@ -1,12 +1,14 @@ -import { ApiProperty, PartialType } from '@nestjs/swagger' +import { ApiProperty, IntersectionType, PartialType } from '@nestjs/swagger' import { IsArray, IsIn, + IsInt, IsOptional, IsString, Matches, MinLength, } from 'class-validator' +import { PagerDto } from '~/common/dto/pager.dto' export class RoleDto { @ApiProperty({ description: '角色名称' }) @@ -36,3 +38,11 @@ export class RoleDto { } export class RoleUpdateDto extends PartialType(RoleDto) {} +export class RoleQueryDto extends IntersectionType(PagerDto, PartialType(RoleDto)) { + + @ApiProperty({ description: '状态', example: 0, required: false }) + @IsInt() + @IsOptional() + status?: number + +} diff --git a/src/modules/system/role/role.service.ts b/src/modules/system/role/role.service.ts index 3eaba39..2f0c807 100644 --- a/src/modules/system/role/role.service.ts +++ b/src/modules/system/role/role.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common' import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm' -import { isEmpty } from 'lodash' -import { EntityManager, In, Repository } from 'typeorm' +import { isEmpty, isNumber } from 'lodash' +import { EntityManager, In, Like, Repository } from 'typeorm' import { PagerDto } from '~/common/dto/pager.dto' import { ROOT_ROLE_ID } from '~/constants/system.constant' @@ -11,7 +11,7 @@ import { SseService } from '~/modules/sse/sse.service' import { MenuEntity } from '~/modules/system/menu/menu.entity' import { RoleEntity } from '~/modules/system/role/role.entity' -import { RoleDto, RoleUpdateDto } from './role.dto' +import { RoleDto, RoleQueryDto, RoleUpdateDto } from './role.dto' @Injectable() export class RoleService { @@ -31,8 +31,22 @@ export class RoleService { async findAll({ page, pageSize, - }: PagerDto): Promise> { - return paginate(this.roleRepository, { page, pageSize }) + name, + value, + status, + }: RoleQueryDto): Promise> { + const queryBuilder = this.roleRepository + .createQueryBuilder('role') + .where({ + ...(name ? { name: Like(`%${name}%`) } : null), + ...(value ? { value: Like(`%${value}%`) } : null), + ...(isNumber(status) ? { status } : null), + }) + + return paginate(queryBuilder, { + page, + pageSize, + }) } /** diff --git a/src/modules/system/task/task.service.ts b/src/modules/system/task/task.service.ts index 8cc0e28..eaba96d 100644 --- a/src/modules/system/task/task.service.ts +++ b/src/modules/system/task/task.service.ts @@ -12,7 +12,7 @@ import { UnknownElementException } from '@nestjs/core/errors/exceptions/unknown- import { InjectRepository } from '@nestjs/typeorm' import { Queue } from 'bull' import Redis from 'ioredis' -import { isEmpty } from 'lodash' +import { isEmpty, isNumber } from 'lodash' import { Like, Repository } from 'typeorm' import { BusinessException } from '~/common/exceptions/biz.exception' @@ -103,7 +103,7 @@ export class TaskService implements OnModuleInit { ...(name ? { name: Like(`%${name}%`) } : null), ...(service ? { service: Like(`%${service}%`) } : null), ...(type ? { type } : null), - ...(status ? { status } : null), + ...(isNumber(status) ? { status } : null), }) .orderBy('task.id', 'ASC') diff --git a/src/modules/user/dto/user.dto.ts b/src/modules/user/dto/user.dto.ts index 31e5ddc..60a44fa 100644 --- a/src/modules/user/dto/user.dto.ts +++ b/src/modules/user/dto/user.dto.ts @@ -27,7 +27,7 @@ export class UserDto { @ApiProperty({ description: '登录账号', example: 'admin' }) @IsString() @Matches(/^[a-z0-9A-Z\W_]+$/) - @MinLength(4) + @MinLength(1) @MaxLength(20) username: string @@ -95,4 +95,5 @@ export class UserQueryDto extends IntersectionType(PagerDto, PartialTyp @IsInt() @IsOptional() status?: number + } diff --git a/src/modules/user/user.service.ts b/src/modules/user/user.service.ts index 806526c..294c5ef 100644 --- a/src/modules/user/user.service.ts +++ b/src/modules/user/user.service.ts @@ -2,7 +2,7 @@ import { InjectRedis } from '@liaoliaots/nestjs-redis' import { BadRequestException, Injectable } from '@nestjs/common' import { InjectEntityManager, InjectRepository } from '@nestjs/typeorm' import Redis from 'ioredis' -import { isEmpty, isNil } from 'lodash' +import { isEmpty, isNil, isNumber } from 'lodash' import { EntityManager, In, Like, Repository } from 'typeorm' @@ -281,7 +281,7 @@ export class UserService { ...(username ? { username: Like(`%${username}%`) } : null), ...(nickname ? { nickname: Like(`%${nickname}%`) } : null), ...(email ? { email: Like(`%${email}%`) } : null), - ...(status ? { status } : null), + ...(isNumber(status) ? { status } : null), }) if (deptId) diff --git a/src/shared/database/typeorm-logger.ts b/src/shared/database/typeorm-logger.ts index 6db7638..434932b 100644 --- a/src/shared/database/typeorm-logger.ts +++ b/src/shared/database/typeorm-logger.ts @@ -3,7 +3,7 @@ import { Logger as ITypeORMLogger, LoggerOptions, QueryRunner } from 'typeorm' export class TypeORMLogger implements ITypeORMLogger { private logger = new Logger(TypeORMLogger.name) - + constructor(private options: LoggerOptions) {} logQuery(query: string, parameters?: any[], _queryRunner?: QueryRunner) {