Skip to content

Commit

Permalink
feat(docorator): cross-platform cookie decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
wibus-wee committed Jul 18, 2022
1 parent 17f61a9 commit 2a00d3d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/common/decorator/cookie.decorator.ts
Original file line number Diff line number Diff line change
@@ -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;
},
);
13 changes: 9 additions & 4 deletions src/modules/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
}
Expand All @@ -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;
Expand Down

0 comments on commit 2a00d3d

Please sign in to comment.