diff --git a/src/constants/error.constant.ts b/src/constants/error.constant.ts index 23580683b899350190d4b15a941efa41ca3066aa..88c94305e52269fd123f23e695bbbe4c088d5a63 100644 --- a/src/constants/error.constant.ts +++ b/src/constants/error.constant.ts @@ -25,6 +25,7 @@ export enum ErrorEnum { USER_USERNAME_EXIST = '2007:用户名已存在', USER_NOT_ALLOWED_TO_DISABLE_ADMIN = '2008:不允许禁用管理员', USER_FORCED_OFFLINE = '2009:您被强制下线 请十分钟后再登录', + USER_PASSWORD_ERROR_RULE = '2010:密码格式不正确,6-18位字符,包含字母、数字、特殊字符', // dept DEPT_EXIST = '3000:部门已存在', diff --git a/src/constants/reg.ts b/src/constants/reg.ts new file mode 100644 index 0000000000000000000000000000000000000000..7a88e2de62aab37faf9f9fa23774a8797480d2b0 --- /dev/null +++ b/src/constants/reg.ts @@ -0,0 +1,25 @@ +export const REG_USER_NAME = /^[\u4E00-\u9FA5a-zA-Z0-9_-]{4,16}$/; + +/** Phone reg */ +export const REG_PHONE = + /^[1](([3][0-9])|([4][01456789])|([5][012356789])|([6][2567])|([7][0-8])|([8][0-9])|([9][012356789]))[0-9]{8}$/; + +/** + * Password reg + * + * 6-18 characters, including letters, numbers, and underscores + */ +export const REG_PWD = /^[\w@$!%*?&.]{6,18}$/; + +/** Email reg */ +export const REG_EMAIL = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; + +/** Six digit code reg */ +export const REG_CODE_SIX = /^\d{6}$/; + +/** Four digit code reg */ +export const REG_CODE_FOUR = /^[A-Za-z0-9]{4}$/; + +/** Url reg */ +export const REG_URL = + /(((^https?:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[\w]*))?)$/; diff --git a/src/modules/user/dto/user.dto.ts b/src/modules/user/dto/user.dto.ts index 13a6412a471fe0a4c354462fefe06bd2ee4c32a4..489d7df3f0f2cca2572eee104afa4e19eaabf16f 100644 --- a/src/modules/user/dto/user.dto.ts +++ b/src/modules/user/dto/user.dto.ts @@ -274,3 +274,13 @@ export class UserPasswordDto { @IsString() newPassword: string; } + +export class UserResetPasswordDto { + @ApiProperty({ + type: String, + description: '重置的密码', + required: true, + }) + @IsString() + password: string; +} diff --git a/src/modules/user/user.controller.ts b/src/modules/user/user.controller.ts index 2bad8024f89e5367d4164007a6acc28c27025bda..11e07ed8edfeb9b96b79ba687218f64919aaa06e 100644 --- a/src/modules/user/user.controller.ts +++ b/src/modules/user/user.controller.ts @@ -16,6 +16,7 @@ import { UserQueryDto, UserStatusDto, UserUpdateDto, + UserResetPasswordDto } from './dto/user.dto'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { ApiResult } from '~/common/decorators/api-result.decorator'; @@ -43,7 +44,7 @@ export const permissions = definePermission('system:user', { @ApiTags('System - 用户模块') @Controller('user') export class UserController { - constructor(private readonly userService: UserService) {} + constructor(private readonly userService: UserService) { } @Get('list') @ApiOperation({ summary: '分页获取用户列表' }) @@ -84,8 +85,8 @@ export class UserController { @Put('resetPassword/:id') @ApiOperation({ summary: '重置用户密码' }) @Perm(permissions.PASSWORD_RESET) - async resetPassword(@ParamId() id: number): Promise { - await this.userService.resetPassword(id); + async resetPassword(@ParamId() id: number, @Body() resetPasswordDto: UserResetPasswordDto): Promise { + await this.userService.resetPassword(id, resetPasswordDto); } @Put('updateProfile/:id') diff --git a/src/modules/user/user.service.ts b/src/modules/user/user.service.ts index dba1b310ed1ce7ec75ac68a09828b82e71ebf271..1c7b43c960866f522ebbb26cf997e64cc22f735e 100644 --- a/src/modules/user/user.service.ts +++ b/src/modules/user/user.service.ts @@ -9,6 +9,7 @@ import { UserPasswordDto, UserProfileDto, UserQueryDto, + UserResetPasswordDto, UserStatusDto, UserUpdateDto, } from './dto/user.dto'; @@ -35,6 +36,7 @@ import { UserInfo } from './user.model'; import { isNil, isEmpty } from 'lodash'; import { AuthService } from '../auth/auth.service'; import { ISecurityConfig, SecurityConfig } from '~/config'; +import { REG_PWD } from '~/constants/reg'; @Injectable() export class UserService { @@ -47,7 +49,7 @@ export class UserService { private readonly profileRepository: Repository, @Inject(SecurityConfig.KEY) private readonly securityConfig: ISecurityConfig, - ) {} + ) { } async list({ currentPage, @@ -242,19 +244,19 @@ export class UserService { ...user, roles: !isEmpty(roleIds) ? await manager.find(RoleEntity, { - where: { - id: In(roleIds), - }, - }) + where: { + id: In(roleIds), + }, + }) : defaultRole ? [defaultRole] : [], dept: deptId ? await manager.findOne(DeptEntity, { - where: { - id: deptId, - }, - }) + where: { + id: deptId, + }, + }) : defaultDept, profile: profile, }); @@ -293,10 +295,10 @@ export class UserService { user.roles = !isEmpty(roleIds) ? await manager.find(RoleEntity, { - where: { - id: In(roleIds), - }, - }) + where: { + id: In(roleIds), + }, + }) : defaultRole ? [defaultRole] : []; @@ -304,10 +306,10 @@ export class UserService { // 保存用户的部门 user.dept = deptId ? await manager.findOne(DeptEntity, { - where: { - id: deptId, - }, - }) + where: { + id: deptId, + }, + }) : defaultDept; await this.updateProfile(user.id, profile); @@ -338,13 +340,18 @@ export class UserService { await this.userRepository.save(user); } - async resetPassword(id: number): Promise { + async resetPassword(id: number, dto: UserResetPasswordDto): Promise { + if (!REG_PWD.test(dto.password)) { + throw new BizException(ErrorEnum.USER_PASSWORD_ERROR_RULE); + } + + console.log(id, dto, '123aa') const user = await this.findUserInfo(id); if (!user) { throw new BizException(ErrorEnum.USER_NOT_EXIST); } - user.password = await argon2.hash('123456'); + user.password = await argon2.hash(dto.password); await this.userRepository.save(user); }