From 2a00d3d555669fc8f62aaffb6710695a850b26c2 Mon Sep 17 00:00:00 2001 From: wibus-wee <1596355173@qq.com> Date: Mon, 18 Jul 2022 16:50:36 +0800 Subject: [PATCH] feat(docorator): cross-platform cookie decorator --- src/common/decorator/cookie.decorator.ts | 27 ++++++++++++++++++++++++ src/modules/post/post.controller.ts | 13 ++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/common/decorator/cookie.decorator.ts diff --git a/src/common/decorator/cookie.decorator.ts b/src/common/decorator/cookie.decorator.ts new file mode 100644 index 000000000..55c6b39cc --- /dev/null +++ b/src/common/decorator/cookie.decorator.ts @@ -0,0 +1,27 @@ +/* + * @FilePath: /nx-core/src/common/decorator/cookie.decorator.ts + * @author: Wibus + * @Date: 2022-07-18 16:33:58 + * @LastEditors: Wibus + * @LastEditTime: 2022-07-18 16:49:42 + * Coding With IU + */ +import { createParamDecorator, ExecutionContext } from '@nestjs/common'; + +export const Cookies = createParamDecorator( + (data: string, ctx: ExecutionContext) => { + const request = ctx.switchToHttp().getRequest(); + // 获取 header 中的 cookie + const cookies = request.headers.cookie; + console.log(cookies); + // 解析 cookie + const cookie = cookies ? cookies.split('; ') : []; + const cookieObj = {}; + cookie.forEach(item => { + const [key, value] = item.split('='); + cookieObj[key] = value; + } + ); + return cookieObj; + }, +); \ No newline at end of file diff --git a/src/modules/post/post.controller.ts b/src/modules/post/post.controller.ts index 8709983cf..d05a95bdb 100644 --- a/src/modules/post/post.controller.ts +++ b/src/modules/post/post.controller.ts @@ -25,6 +25,7 @@ import { Types, PipelineStage } from "mongoose"; import { MongoIdDto } from "~/shared/dto/id.dto"; import { IsMaster } from "~/common/decorator/role.decorator"; import { md5 } from "~/utils/tools.util"; +import { Cookies } from "~/common/decorator/cookie.decorator"; @Controller("posts") @ApiName export class PostController { @@ -148,10 +149,12 @@ export class PostController { @Get("/:category/:slug") @ApiOperation({ summary: "根据分类名与自定义别名获取文章详情" }) - async getByCategoryAndSlug(@Param() params: CategoryAndSlugDto, @IsMaster() isMaster: boolean, @Body() body: any) { + async getByCategoryAndSlug(@Param() params: CategoryAndSlugDto, @IsMaster() isMaster: boolean, @Cookies() password: any) { const { category, slug } = params; const categoryDocument = await this.postService.getCategoryBySlug(category); - if (body === undefined || body.password === undefined || !body.password) body = { password: null }; + console.log(password); + if (password === undefined || !password) password = null; + console.log(password); if (!categoryDocument) { throw new NotFoundException("该分类不存在w"); } @@ -168,8 +171,10 @@ export class PostController { throw new CannotFindException(); } if (!isMaster && postDocument.password) { - if (!body.password || md5(body.password) !== postDocument.password) { // 将传入的 password 转换为 md5 字符串,与数据库中的 password 比较 - throw new BadRequestException("密码错误"); + if (!password || md5(password) !== postDocument.password) { // 将传入的 password 转换为 md5 字符串,与数据库中的 password 比较 + // 将text, summary改为"内容已被隐藏" + postDocument.text = "内容已被隐藏,请输入密码"; + postDocument.summary = "内容已被隐藏,请输入密码"; } } return postDocument;