修复了几个damn的bug,终于可以用户端访问了
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
|||||||
Param,
|
Param,
|
||||||
ParseUUIDPipe,
|
ParseUUIDPipe,
|
||||||
Post,
|
Post,
|
||||||
|
Query,
|
||||||
Req,
|
Req,
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
@@ -31,14 +32,14 @@ export class BlogController {
|
|||||||
@Get(':id')
|
@Get(':id')
|
||||||
async getBlog(
|
async getBlog(
|
||||||
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||||
@Param('p') password: string,
|
@Query('p') password?: string,
|
||||||
) {
|
) {
|
||||||
const blog = await this.blogService.findById(id);
|
const blog = await this.blogService.findById(id);
|
||||||
if (!blog) throw new BadRequestException('文章不存在或无权限访问');
|
if (!blog) throw new BadRequestException('文章不存在或无权限访问');
|
||||||
|
|
||||||
if (!blog.permissions.includes(BlogPermission.Public)) {
|
if (!blog.permissions.includes(BlogPermission.Public)) {
|
||||||
// 无公开权限,则进一步检查是否有密码保护
|
// 无公开权限,则进一步检查是否有密码保护
|
||||||
if (blog.permissions.includes(BlogPermission.ByPassword)) {
|
if (!blog.permissions.includes(BlogPermission.ByPassword)) {
|
||||||
throw new BadRequestException('文章不存在或无权限访问');
|
throw new BadRequestException('文章不存在或无权限访问');
|
||||||
} else {
|
} else {
|
||||||
// 判断密码是否正确
|
// 判断密码是否正确
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { BlogApi } from "@/lib/api";
|
import { BlogApi } from "@/lib/api";
|
||||||
import { base62 } from "@/lib/utils";
|
import { base62 } from "@/lib/utils";
|
||||||
import { useParams } from "next/navigation";
|
import { useParams, useSearchParams } from "next/navigation";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
import ReactMarkdown from 'react-markdown'
|
import ReactMarkdown from 'react-markdown'
|
||||||
import remarkGfm from 'remark-gfm'
|
import remarkGfm from 'remark-gfm'
|
||||||
@@ -17,6 +17,8 @@ import Image from "next/image";
|
|||||||
|
|
||||||
export default function Blog() {
|
export default function Blog() {
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
const hex = Array.from(base62.decode(params.id as string)).map(b => b.toString(16).padStart(2, '0')).join('');
|
const hex = Array.from(base62.decode(params.id as string)).map(b => b.toString(16).padStart(2, '0')).join('');
|
||||||
const id = [
|
const id = [
|
||||||
hex.slice(0, 8),
|
hex.slice(0, 8),
|
||||||
@@ -26,9 +28,12 @@ export default function Blog() {
|
|||||||
hex.slice(20, 32)
|
hex.slice(20, 32)
|
||||||
].join('-');
|
].join('-');
|
||||||
|
|
||||||
|
const password = searchParams.get('p');
|
||||||
const { data, error, isLoading } = useSWR(
|
const { data, error, isLoading } = useSWR(
|
||||||
`/api/blog/${id}`,
|
`/api/blog/${id}`,
|
||||||
() => BlogApi.get(id),
|
() => BlogApi.get(id, {
|
||||||
|
password: password || undefined,
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -59,9 +64,9 @@ export default function Blog() {
|
|||||||
h5: ({ ...props }) => <h5 className="text-md font-bold" {...props} />,
|
h5: ({ ...props }) => <h5 className="text-md font-bold" {...props} />,
|
||||||
p: ({ ...props }) => <p className="py-1 text-zinc-700" {...props} />,
|
p: ({ ...props }) => <p className="py-1 text-zinc-700" {...props} />,
|
||||||
img: ({ src }) => (
|
img: ({ src }) => (
|
||||||
<PhotoProvider>
|
<PhotoProvider className="w-full">
|
||||||
<PhotoView src={src as string}>
|
<PhotoView src={src as string}>
|
||||||
<Image src={src as string} alt="加载失败" />
|
<Image src={src as string} fill alt="加载失败" />
|
||||||
</PhotoView>
|
</PhotoView>
|
||||||
</PhotoProvider>
|
</PhotoProvider>
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import fetcher from "../fetcher";
|
import fetcher from "../fetcher";
|
||||||
|
|
||||||
export async function get(id: string) {
|
export async function get(id: string, option: {
|
||||||
|
password?: string;
|
||||||
|
} = {}) {
|
||||||
|
const { password } = option;
|
||||||
return fetcher<{
|
return fetcher<{
|
||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
content: string;
|
content: string;
|
||||||
}>(`/api/blog/${id}`);
|
}>(`/api/blog/${id}` + (password ? `?p=${password}` : ''));
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user