From 26d32d2086f6afebac441ad6232a4c1e63813d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E9=98=B3?= <3311118881@qq.com> Date: Sat, 12 Oct 2024 15:36:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=B7=B2=E7=9F=A5=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Create/components/PublishForm/index.tsx | 25 ++++++++++++++----- src/utils/request.ts | 4 +-- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/pages/Create/components/PublishForm/index.tsx b/src/pages/Create/components/PublishForm/index.tsx index 68227e4..64b4e32 100644 --- a/src/pages/Create/components/PublishForm/index.tsx +++ b/src/pages/Create/components/PublishForm/index.tsx @@ -7,7 +7,7 @@ import { RuleObject } from "antd/es/form"; import { addArticleDataAPI, editArticleDataAPI } from '@/api/Article' import { getCateListAPI } from '@/api/Cate' -import { getTagListAPI } from '@/api/Tag' +import { addTagDataAPI, getTagListAPI } from '@/api/Tag' import { Cate } from "@/types/app/cate"; import { Tag } from "@/types/app/tag"; @@ -21,7 +21,7 @@ interface FieldType { title: string, createTime: number; cateIds: number[]; - tagIds: number[] | string; + tagIds: (number | string)[]; cover: string; description: string; } @@ -79,16 +79,28 @@ const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => vo }; const onSubmit: FormProps['onFinish'] = async (values) => { + // 如果是文章标签,则先判断是否存在,如果不存在则添加 + let tagIds: number[] = [] + for (const item of values.tagIds) { + if (typeof item === "string") { + await addTagDataAPI({ name: item }); + const { data: tagList } = await getTagListAPI(); + // 添加成功后查找对应的标签id + const tag = tagList.find(t => t.name === item)?.id; + if (tag !== undefined) tagIds.push(tag); + } else { + tagIds.push(item); + } + } + values.createTime = values.createTime.valueOf() values.cateIds = [...new Set(values.cateIds?.flat())] - values.tagIds = values.tagIds ? (values.tagIds as number[]).join(',') : "" - if (id) { - await editArticleDataAPI({ id, ...values, content: data.content } as any) + await editArticleDataAPI({ id, ...values, content: data.content, tagIds: tagIds.join(',') } as any) message.success("🎉 编辑成功") } else { - await addArticleDataAPI({ id, ...values, content: data.content } as any) + await addArticleDataAPI({ id, ...values, content: data.content, tagIds: tagIds.join(',') } as any) message.success("🎉 发布成功") } @@ -144,6 +156,7 @@ const PublishForm = ({ data, closeModel }: { data: Article, closeModel: () => vo fieldNames={{ label: 'name', value: 'id' }} filterOption={(input, option) => !!option?.name.includes(input)} placeholder="请选择文章标签" + // onChange={(value, a) => console.log(value, a)} className="w-full" /> diff --git a/src/utils/request.ts b/src/utils/request.ts index efa6c96..47363fc 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -3,8 +3,8 @@ import { Modal, notification } from "antd"; import { useUserStore } from "@/stores"; // 配置项目API域名 -// export const baseURL = "http://localhost:9999/api"; -export const baseURL = "http://api.liuyuyang.net/api"; +export const baseURL = "http://localhost:9003/api"; +// export const baseURL = "http://api.liuyuyang.net/api"; // 创建 axios 实例 export const instance = axios.create({