This commit is contained in:
2025-06-23 01:30:02 +08:00
parent d96c4c9adf
commit 660cacbd53
6 changed files with 45 additions and 34 deletions

View File

@@ -1,5 +1,4 @@
import { import {
BadRequestException,
Body, Body,
Controller, Controller,
Delete, Delete,
@@ -11,14 +10,11 @@ import {
UseGuards, UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport'; import { AuthGuard } from '@nestjs/passport';
import { UpdatePasswordDto } from 'src/admin/dto/admin-user/update-password.dto';
import { CreateBlogDto } from 'src/admin/dto/admin-web/create-blog.dto'; import { CreateBlogDto } from 'src/admin/dto/admin-web/create-blog.dto';
import { SetBlogPasswordDto } from 'src/admin/dto/admin-web/set-blog-password.dto'; import { SetBlogPasswordDto } from 'src/admin/dto/admin-web/set-blog-password.dto';
import { UpdateBlogDto } from 'src/admin/dto/admin-web/update-blog.dto'; import { UpdateBlogDto } from 'src/admin/dto/admin-web/update-blog.dto';
import { Role } from 'src/auth/role.enum'; import { Role } from 'src/auth/role.enum';
import { BlogPermission } from 'src/blog/Blog.Permission.enum';
import { BlogService } from 'src/blog/blog.service'; import { BlogService } from 'src/blog/blog.service';
import { Blog } from 'src/blog/entity/Blog.entity';
import { Roles } from 'src/common/decorators/role.decorator'; import { Roles } from 'src/common/decorators/role.decorator';
import { RolesGuard } from 'src/common/guard/roles.guard'; import { RolesGuard } from 'src/common/guard/roles.guard';
@@ -26,7 +22,7 @@ import { RolesGuard } from 'src/common/guard/roles.guard';
@UseGuards(AuthGuard('jwt'), RolesGuard) @UseGuards(AuthGuard('jwt'), RolesGuard)
@Roles(Role.Admin) @Roles(Role.Admin)
export class AdminWebBlogController { export class AdminWebBlogController {
constructor(private readonly adminWebBlogService: BlogService) { } constructor(private readonly adminWebBlogService: BlogService) {}
@Get() @Get()
async list() { async list() {

View File

@@ -15,5 +15,5 @@ export class CreateBlogDto {
permissions: BlogPermission[]; permissions: BlogPermission[];
@IsString() @IsString()
password: string;// 允许空串 password: string; // 允许空串
} }

View File

@@ -1,6 +1,6 @@
import { IsString } from "class-validator"; import { IsString } from 'class-validator';
export class SetBlogPasswordDto { export class SetBlogPasswordDto {
@IsString() @IsString()
password: string; password: string;
} }

View File

@@ -22,7 +22,7 @@ export class BlogController {
constructor( constructor(
private readonly blogService: BlogService, private readonly blogService: BlogService,
private readonly userService: UserService, private readonly userService: UserService,
) { } ) {}
@Get() @Get()
getBlogs() { getBlogs() {
@@ -43,7 +43,10 @@ export class BlogController {
throw new BadRequestException('文章不存在或无权限访问'); throw new BadRequestException('文章不存在或无权限访问');
} else { } else {
// 判断密码是否正确 // 判断密码是否正确
if (!password || this.blogService.hashPassword(password) !== blog.password_hash) { if (
!password ||
this.blogService.hashPassword(password) !== blog.password_hash
) {
throw new BadRequestException('文章不存在或无权限访问'); throw new BadRequestException('文章不存在或无权限访问');
} }
} }

View File

@@ -1,5 +1,5 @@
export enum BlogPermission { export enum BlogPermission {
Public = 'Public', Public = 'Public',
ByPassword = 'ByPassword', ByPassword = 'ByPassword',
List = 'List', List = 'List',
} }

View File

@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Blog } from './entity/Blog.entity'; import { Blog } from './entity/Blog.entity';
import { ArrayContains, Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { BlogComment } from './entity/BlogComment.entity'; import { BlogComment } from './entity/BlogComment.entity';
import { BlogPermission } from './Blog.Permission.enum'; import { BlogPermission } from './Blog.Permission.enum';
import { createHash } from 'crypto'; import { createHash } from 'crypto';
@@ -13,18 +13,24 @@ export class BlogService {
private readonly blogRepository: Repository<Blog>, private readonly blogRepository: Repository<Blog>,
@InjectRepository(BlogComment) @InjectRepository(BlogComment)
private readonly blogCommentRepository: Repository<BlogComment>, private readonly blogCommentRepository: Repository<BlogComment>,
) { } ) {}
async list(option: { async list(
withAll?: boolean; option: {
} = {}) { withAll?: boolean;
return (await this.blogRepository.find({ } = {},
order: { ) {
createdAt: 'DESC', return (
}, await this.blogRepository.find({
})) order: {
.filter(i => option.withAll || i.permissions.includes(BlogPermission.List)) createdAt: 'DESC',
.map(i => { },
})
)
.filter(
(i) => option.withAll || i.permissions.includes(BlogPermission.List),
)
.map((i) => {
if (option.withAll) { if (option.withAll) {
return i; return i;
} }
@@ -36,7 +42,7 @@ export class BlogService {
id, id,
title, title,
viewCount, viewCount,
} };
}); });
} }
@@ -44,7 +50,9 @@ export class BlogService {
const { password, ...blog } = dto; const { password, ...blog } = dto;
if (blog.permissions.includes(BlogPermission.ByPassword)) { if (blog.permissions.includes(BlogPermission.ByPassword)) {
if (password) { if (password) {
blog.password_hash = createHash('sha256').update(`${password}`).digest('hex'); blog.password_hash = createHash('sha256')
.update(`${password}`)
.digest('hex');
} }
} }
@@ -58,10 +66,14 @@ export class BlogService {
throw new Error('博客不存在'); throw new Error('博客不存在');
} }
return (await this.blogRepository.update(id, { return (
...blog, (
password_hash: this.hashPassword(password), await this.blogRepository.update(id, {
})).affected > 0; ...blog,
password_hash: this.hashPassword(password),
})
).affected > 0
);
} }
async update(id: string, blog: Partial<Blog>) { async update(id: string, blog: Partial<Blog>) {