diff --git a/config/development.ts b/config/development.ts index 9187605..9560282 100644 --- a/config/development.ts +++ b/config/development.ts @@ -20,7 +20,7 @@ export default { host: 'localhost', port: 3306, username: 'root', - password: 'root', + password: 'Hxl1314521', database: 'Starlight', }, }, diff --git a/src/Gdecorator/userinfoDecorator/userinfoDecorator.decorator.ts b/src/Gdecorator/userinfoDecorator/userinfoDecorator.decorator.ts new file mode 100644 index 0000000..022ad6d --- /dev/null +++ b/src/Gdecorator/userinfoDecorator/userinfoDecorator.decorator.ts @@ -0,0 +1,30 @@ +import { + createParamDecorator, + ExecutionContext, + SetMetadata, +} from '@nestjs/common'; + +export const UserInfoDecorator = (...args: string[]) => + SetMetadata('userinfoDecorator', args); + +// nest g d [name] +// 自定义装饰器 + +// ? ? +// ? 函数名称: userinfoDto +// ? 函数描述: 限制userinfo数据格式 +// ? ? +export class userinfoDto { + uuid: string; +} + +// ? ? +// ? 函数名称: getUserinfo +// ? 函数描述: 获取token用户信息 +// ? ? +export const getUserinfo = createParamDecorator( + (data: string, ctx: ExecutionContext) => { + const req = ctx.switchToHttp().getRequest(); + return req.userinfo as userinfoDto; + }, +); diff --git a/src/starlight/dto/position.dto.ts b/src/starlight/dto/position.dto.ts new file mode 100644 index 0000000..2a8ed4d --- /dev/null +++ b/src/starlight/dto/position.dto.ts @@ -0,0 +1,32 @@ +import { IsNumber, IsString, Length } from 'class-validator'; + +export class PositionCreateDTO { + // @ 父id + @IsNumber({}, { message: '父id必须为数字!' }) + fatherid: number; + + // @ 定位类型 + @Length(1, 128, { message: '请将角色定位类型长度控制在8到128位之间!' }) + @IsString({ message: '角色定位类型应为字符串格式!' }) + type: string; + + // @ 角色名称 + @Length(1, 128, { message: '请将角色定位名称长度控制在8到128位之间!' }) + @IsString({ message: '角色定位名称应为字符串格式!' }) + name: string; + + // @ 角色标识 + @Length(1, 128, { message: '请将角色定位标识长度控制在8到128位之间!' }) + @IsString({ message: '角色定位标识应为字符串格式!' }) + index: string; + + // @ 描述 + @Length(1, 128, { message: '请将角色定位描述长度控制在8到128位之间!' }) + @IsString({ message: '角色定位描述应为字符串格式!' }) + describe: string; + + // @ 图标 + @Length(8, 255, { message: '请将角色定位图标地址长度控制在8到128位之间!' }) + @IsString({ message: '角色定位图标地址应为字符串格式!' }) + ico: string; +} diff --git a/src/starlight/starlight.controller.ts b/src/starlight/starlight.controller.ts index 0742c72..81b6e92 100644 --- a/src/starlight/starlight.controller.ts +++ b/src/starlight/starlight.controller.ts @@ -34,18 +34,13 @@ import { RegisterEmailCheckoutUsernameDto, RegisterEmailSignUpDto, } from '@/starlight/dto/register.dto'; +import { getUserinfo } from '@/Gdecorator/userinfoDecorator/userinfoDecorator.decorator'; import { SignInEmailEntryDto, SignInPasswdEntryDto, } from '@/starlight/dto/signIn.dto'; import { VerifyGuard } from '@/starlight/verify.guard'; - -// nest g d [name] -// 自定义装饰器 -const getUser = createParamDecorator((data: string, ctx: ExecutionContext) => { - const req = ctx.switchToHttp().getRequest(); - return req.user; -}); +import { PositionCreateDTO } from '@/starlight/dto/position.dto'; // C C // C 类名称: StarlightController @@ -148,12 +143,28 @@ export class StarlightController { // ? ? @UseGuards(VerifyGuard) @Get('/signIn/testToken') - testToken(@getUser() user) { + testToken(@getUserinfo() user) { this.logger.info(user); return {}; } - //#endredion + //#endregion + + //#region + // ? ? + // ? 函数名称: positionCreate + // ? 函数描述: 创建角色 + // ? ? + @UseGuards(VerifyGuard) + @Post('position/create') + positionCreate( + @Body() body: PositionCreateDTO, + @getUserinfo() userInfo, + ): Promise { + return this.starlightService.positionCreate(body, userInfo); + } + + //#endregion //#region 测试 @Post() diff --git a/src/starlight/starlight.service.ts b/src/starlight/starlight.service.ts index a9e9fc7..4fd2d21 100644 --- a/src/starlight/starlight.service.ts +++ b/src/starlight/starlight.service.ts @@ -13,6 +13,7 @@ import { RegisterEmailSignUpDto, sex, } from './dto/register.dto'; +import { PositionCreateDTO } from './dto/position.dto'; import { ConfigService } from '@nestjs/config'; import { GloggerService } from '@/Gservice/GLOGGER/glogger.service'; import { GdatabaseService } from '@/Gservice/GDATABASE/gdatabase.service'; @@ -23,6 +24,7 @@ import { SignInEmailEntryDto, SignInPasswdEntryDto, } from '@/starlight/dto/signIn.dto'; +import { userinfoDto } from '@/Gdecorator/userinfoDecorator/userinfoDecorator.decorator'; // C C // C 类名称: StarlightService @@ -44,6 +46,7 @@ export class StarlightService { // @ 工具服务 private readonly tools: GtoolsService, ) {} + //#region 邮箱注册 // ? ? // ? 函数名称: registerEmailCheckoutEmail @@ -592,6 +595,31 @@ export class StarlightService { } //#endregion + //#region 角色定位 + // ? ? + // ? 函数名称: positionCreate + // ? 函数描述: 创建角色 + // ? ? + public async positionCreate( + body: PositionCreateDTO, + userinfo: userinfoDto, + ): Promise { + // 去除空格,小写标识位 + const fatherid = body.fatherid, + type = body.type.trim(), + name = body.name.trim(), + index = body.index.trim().toLowerCase(), + describe = body.describe.trim(), + ico = body.ico.trim(); + this.logger.info(userinfo); + this.logger.info(fatherid, type, name, index, describe, ico); + // 查重标识位 + // 写入标识位 + return {}; + } + + //#endregion + //#region 测试啊 create(createStarlightDto: CreateStarlightDto) { diff --git a/src/starlight/verify.guard.ts b/src/starlight/verify.guard.ts index 39815de..1b9e889 100644 --- a/src/starlight/verify.guard.ts +++ b/src/starlight/verify.guard.ts @@ -7,6 +7,7 @@ import { import { Observable } from 'rxjs'; import { GloggerService } from '@/Gservice/GLOGGER/glogger.service'; import { GtoolsService } from '@/Gservice/GTOOLS/gtools.service'; +import { userinfoDto } from '@/Gdecorator/userinfoDecorator/userinfoDecorator.decorator'; // 此文件为守卫 @Injectable() export class VerifyGuard implements CanActivate { @@ -25,7 +26,10 @@ export class VerifyGuard implements CanActivate { const data = this.tools.resolveToken(token); // this.logger.info(data); if (data.token) { - context.switchToHttp().getRequest().user = '32'; + const userinfo: userinfoDto = { + uuid: 'xx', + }; + context.switchToHttp().getRequest().userinfo = userinfo; // this.logger.info(request.user); } // console.log(request);