From 5144f3452f3dcff364640e40b6a40d45fdab1dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E9=98=B3?= <3311118881@qq.com> Date: Sat, 31 Aug 2024 12:26:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/Article.ts | 39 ++++++++------- src/api/Cate.ts | 19 +------- src/api/Comment.ts | 19 +------- src/api/File.ts | 19 +------- src/api/Rss.ts | 2 +- src/api/Swiper.ts | 3 +- src/api/Tag.ts | 2 +- src/api/Web.ts | 2 +- src/components/FileUpload/index.tsx | 72 ++++++++++++++++----------- src/pages/Comment/index.tsx | 7 +-- src/utils/index.ts | 76 +++++++---------------------- 11 files changed, 94 insertions(+), 166 deletions(-) diff --git a/src/api/Article.ts b/src/api/Article.ts index d321d8e..016d415 100644 --- a/src/api/Article.ts +++ b/src/api/Article.ts @@ -1,5 +1,6 @@ import Request from "@/utils/request"; import { Article } from "@/types/app/article"; +import { getListAPI } from "@/utils"; // 新增文章 export const addArticleDataAPI = (data: Article) => @@ -16,22 +17,24 @@ export const editArticleDataAPI = (data: Article) => // 获取文章 export const getArticleDataAPI = (id?: number) => Request
("GET", `/article/${id}`) +// export const getArticleListAPI = (data?: QueryData) => { +// if (data?.pagination) { +// return Request>("POST", `/article/paging`, { +// data: { ...data?.query }, +// params: { +// sort: data.sort, +// ...data.pagination +// } +// }); +// } else { +// return Request("POST", `/article/list`, { +// data: { ...data?.query }, +// params: { +// sort: data?.sort +// } +// }); +// } +// }; + // 获取文章列表 -export const getArticleListAPI = (data?: QueryData) => { - if (data?.pagination) { - return Request>("POST", `/article/paging`, { - data: { ...data?.query }, - params: { - sort: data.sort, - ...data.pagination - } - }); - } else { - return Request("POST", `/article/list`, { - data: { ...data?.query }, - params: { - sort: data?.sort - } - }); - } -}; +export const getArticleListAPI = (data?: QueryData) => getListAPI
("/article", data) \ No newline at end of file diff --git a/src/api/Cate.ts b/src/api/Cate.ts index f11d575..2609f48 100644 --- a/src/api/Cate.ts +++ b/src/api/Cate.ts @@ -15,21 +15,4 @@ export const editCateDataAPI = (data: Cate) => Request("PATCH", "/cate", { export const getCateDataAPI = (id?: number) => Request("GET", `/cate/${id}`) // 获取分类列表 -export const getCateListAPI = (data?: QueryData) => { - if (data?.pagination) { - return Request>("POST", `/cate/paging`, { - data: { ...data?.query }, - params: { - sort: data.sort, - ...data.pagination - } - }); - } else { - return Request("POST", `/cate/list`, { - data: { ...data?.query }, - params: { - sort: data?.sort - } - }); - } - }; \ No newline at end of file +export const getCateListAPI = (data?: QueryData) => getListAPI("/cate", data) \ No newline at end of file diff --git a/src/api/Comment.ts b/src/api/Comment.ts index 3ce5102..fb56e04 100644 --- a/src/api/Comment.ts +++ b/src/api/Comment.ts @@ -18,21 +18,4 @@ export const editCommentDataAPI = (data: Comment) => Request("PATCH", " export const getCommentDataAPI = (id?: number) => Request>("GET", `/comment/${id}`) // 获取评论列表 -export const getCommentListAPI = (data?: QueryData) => { - if (data?.pagination) { - return Request>("POST", `/comment/paging`, { - data: { ...data?.query }, - params: { - sort: data.sort, - ...data.pagination - } - }); - } else { - return Request("POST", `/comment/list`, { - data: { ...data?.query }, - params: { - sort: data?.sort - } - }); - } -}; \ No newline at end of file +export const getCommentListAPI = (data?: QueryData) => getListAPI("/comment", data) \ No newline at end of file diff --git a/src/api/File.ts b/src/api/File.ts index c6dbd71..94afaea 100644 --- a/src/api/File.ts +++ b/src/api/File.ts @@ -1,5 +1,6 @@ import Request from '@/utils/request' import { File } from '@/types/app/file' +import { getListAPI } from '@/utils' // 删除文件 export const delFileDataAPI = (filePath: string) => Request("DELETE", `/file?filePath=${filePath}`) @@ -8,23 +9,7 @@ export const delFileDataAPI = (filePath: string) => Request("DELETE", `/fi export const getFileDataAPI = (filePath: string) => Request("GET", `/file/info?filePath=${filePath}`) // 获取文件列表 -export const getFileListAPI = (data?: QueryData) => { - if (data?.pagination) { - let sort = data?.sort ? `?sort=${data?.sort}` : '?' - let dir = data?.dir ? `dir=${data?.dir}` : '' - const { page, size } = data.pagination - const pagination = page && size ? `&page=${page}&size=${size}` : page && !size ? `&page=${page}` : size && !page ? `&size=${size}` : '' - if (!dir && !pagination) sort = '' - - return Request>("GET", `/file${sort}${pagination}${dir}`); - } else { - let sort = data?.sort ? `?sort=${data?.sort}` : '?' - let dir = data?.dir ? `dir=${data?.dir}` : '' - if (!dir) sort = '' - - return Request("GET", `/file/all${sort}${dir}`); - } -}; +export const getFileListAPI = (data?: QueryData) => getListAPI("/file", data) // 获取目录列表 export const getDirListAPI = () => Request("GET", '/file/dir');; \ No newline at end of file diff --git a/src/api/Rss.ts b/src/api/Rss.ts index 57ed650..6a8b918 100644 --- a/src/api/Rss.ts +++ b/src/api/Rss.ts @@ -2,4 +2,4 @@ import { Rss } from '@/types/app/rss'; import { getListAPI } from '@/utils'; // 获取订阅数据列表 -export const getRssListAPI = getListAPI("/rss") \ No newline at end of file +export const getRssListAPI = (data?: QueryData) => getListAPI("/rss", data) \ No newline at end of file diff --git a/src/api/Swiper.ts b/src/api/Swiper.ts index 05ef6e7..5b01fc6 100644 --- a/src/api/Swiper.ts +++ b/src/api/Swiper.ts @@ -15,4 +15,5 @@ export const editSwiperDataAPI = (data: Swiper) => Request("PATCH", "/sw export const getSwiperDataAPI = (id?: number) => Request("GET", `/swiper/${id}`) // 获取轮播图列表 -export const getSwiperListAPI = getListAPI("/swiper") \ No newline at end of file +// export const getSwiperListAPI = getListAPI("/swiper") +export const getSwiperListAPI = (data?: QueryData) => getListAPI("/swiper", data) \ No newline at end of file diff --git a/src/api/Tag.ts b/src/api/Tag.ts index c0bf299..8107cfc 100644 --- a/src/api/Tag.ts +++ b/src/api/Tag.ts @@ -15,4 +15,4 @@ export const editTagDataAPI = (data: Tag) => Request("PATCH", "/tag", { dat export const getTagDataAPI = (id?: number) => Request("GET", `/tag/${id}`) // 获取标签列表 -export const getTagListAPI = getListAPI("/tag") +export const getTagListAPI = (data?: QueryData) => getListAPI("/tag", data) \ No newline at end of file diff --git a/src/api/Web.ts b/src/api/Web.ts index e3e3c6a..3119b29 100644 --- a/src/api/Web.ts +++ b/src/api/Web.ts @@ -15,7 +15,7 @@ export const editLinkDataAPI = (data: Web) => Request("PATCH", "/link", { d export const getLinkDataAPI = (id?: number) => Request("GET", `/link/${id}`) // 获取网站列表 -export const getLinkListAPI = getListAPI("/link") +export const getLinkListAPI = (data?: QueryData) => getListAPI("/link", data) // 获取网站类型列表 export const getLinkTypeListAPI = () => { diff --git a/src/components/FileUpload/index.tsx b/src/components/FileUpload/index.tsx index 02dd609..69ee26d 100644 --- a/src/components/FileUpload/index.tsx +++ b/src/components/FileUpload/index.tsx @@ -6,6 +6,7 @@ import { useUserStore } from '@/stores'; import { baseURL } from '@/utils/request'; import Compressor from 'compressorjs'; import { useState } from 'react'; +import { UploadFile } from 'antd/es/upload'; const { Dragger } = Upload; @@ -20,7 +21,9 @@ export default ({ dir, open, onCancel, onSuccess }: UploadFileProps) => { const store = useUserStore(); const [quality, setQuality] = useState(1000); const [isCompressionUpload, setIsCompressionUpload] = useState(false); - const [isLoading, setIsLoading] = useState(false); // 添加加载状态 + const [isLoading, setIsLoading] = useState(false); + + const [fileList, setFileList] = useState([]); const uploadProps: UploadProps = { name: 'files', @@ -30,63 +33,74 @@ export default ({ dir, open, onCancel, onSuccess }: UploadFileProps) => { headers: { "Authorization": `Bearer ${store.token}` }, - showUploadList: false, // 不显示文件列表 + showUploadList: true, async onChange(info) { - const { status } = info.file; + const { status, response } = info.file; - let res; - if (status !== 'uploading') { - res = info?.file?.response; + if (status !== 'uploading' && response?.code === 400) return message.error(response.message); + + setFileList(info.fileList); - if (res?.code === 400) return message.error(res.message); - } if (status === 'done') { - // // 复制文件链接到剪贴板 - await navigator.clipboard.writeText(res.data.join("\n")); - console.log(5555, res.data.join("\n")); - - message.success(`🎉 文件上传成功,URL链接已复制到剪贴板`); - onSuccess(res.data.join("\n")); - onCloseModel() + message.success(`${info.file.name} 文件上传成功`); } else if (status === 'error') { - message.error(`文件上传失败:${res?.message}`); + message.error(`${info.file.name} 文件上传失败`); + setIsLoading(false); + return } - // setIsLoading(false); // 结束加载状态 + // if (info.fileList.some(file => file.status === "error")) { + // message.error(`${info.file.name} 文件上传失败`); + // setIsLoading(false); + // return + // } + + // 所有文件的状态都不为uploading就证明上传成功 + if (info.fileList.every(file => file.status !== 'uploading')) { + // 等待所有请求完毕后再执行 + const allResponses = await info.fileList.map(file => file.response?.data).filter(data => data); + const data = await allResponses.flat().join("\n"); + + // 把数据写入到剪贴板 + await navigator.clipboard.writeText(data); + message.success(`🎉 文件上传成功,URL链接已复制到剪贴板`); + onSuccess(data); + setIsLoading(false); + onCloseModel(); + } }, beforeUpload: async (file) => { - setIsLoading(true); // 开始加载状态 + setIsLoading(true); - if (quality === 1000) return file + if (quality === 1000) return file; - // 对图片进行压缩处理 return new Promise((resolve, reject) => { new Compressor(file, { quality, - success: (file) => { - resolve(file); + success: (compressedFile) => { + resolve(compressedFile); }, error: (err) => { reject(err); }, }); - }) + }); }, className: "py-4" }; - // 初始化操作 const onCloseModel = () => { setIsCompressionUpload(false); setQuality(1000); - setIsLoading(false); // 确保关闭时停止加载状态 + setIsLoading(false); + setFileList([]); onCancel(); - } + }; return ( <> - {/* 包裹内容的 Spin 组件 */} +
setIsCompressionUpload(e.target.value ? true : false)}> 无损上传 @@ -115,7 +129,7 @@ export default ({ dir, open, onCancel, onSuccess }: UploadFileProps) => { }
- +

@@ -126,4 +140,4 @@ export default ({ dir, open, onCancel, onSuccess }: UploadFileProps) => {
); -}; \ No newline at end of file +}; diff --git a/src/pages/Comment/index.tsx b/src/pages/Comment/index.tsx index 6e9fd83..3f25d29 100644 --- a/src/pages/Comment/index.tsx +++ b/src/pages/Comment/index.tsx @@ -23,10 +23,11 @@ const CommentPage = () => { setLoading(false) } - const auditCommentData = async (id: number) => { + const auditCommentData = async () => { setLoading(true) - await auditCommentDataAPI(id); + await auditCommentDataAPI(comment?.id!); getCommentList(); + setIsModalOpen(false) message.success('🎉 审核评论成功'); }; @@ -155,7 +156,7 @@ const CommentPage = () => { : 待审核} - {!comment?.auditStatus ? : null} + {!comment?.auditStatus ? : null} diff --git a/src/utils/index.ts b/src/utils/index.ts index 66b8d40..af351df 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -10,63 +10,21 @@ export const ObjectToUrlParam = (obj: Object): string => { ).toString(); } -// 查询数据相关逻辑 -// export const getListAPI = (api: string) => { -// return (data?: QueryData) => { -// if (data?.pagination) { -// let sort = data?.sort ? `?sort=${data?.sort}` : '?' -// const query = ObjectToUrlParam(data?.query as FilterData) ? "&" + ObjectToUrlParam(data?.query as FilterData) : '' -// const { page, size } = data.pagination -// const pagination = page && size ? `&page=${page}&size=${size}` : page && !size ? `&page=${page}` : size && !page ? `&size=${size}` : '' -// if (!query && !pagination) sort = '' - -// return Request>("GET", `${api}${sort}${pagination}${query}`); -// } else { -// return Request("GET", `${api}/all`, { -// sort: data?.sort, -// pattern: data?.pattern, -// ...data?.query -// }); -// } -// } -// }; - -const buildQueryParams = (params: Record): string => { - return Object.entries(params) - .filter(([_, value]) => value !== undefined && value !== null) - .map(([key, value]) => { - if (Array.isArray(value)) { - return `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(value))}`; +export const getListAPI = (api: string, data?: QueryData) => { + if (data?.pagination) { + return Request>("POST", `${api}/paging`, { + data: { ...data?.query }, + params: { + sort: data.sort, + ...data.pagination } - return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`; - }) - .join('&'); -}; - -export const getListAPI = (api: string) => { - return (data?: QueryData) => { - let queryParams: Record = {}; - - if (data?.sort) { - queryParams.sort = data.sort; - } - - if (data?.query) { - queryParams = { ...queryParams, ...data.query }; - } - - if (data?.pagination) { - const { page, size } = data.pagination; - if (page) queryParams.page = page; - if (size) queryParams.size = size; - } - - const queryString = buildQueryParams(queryParams); - - if (data?.pagination) { - return Request>("GET", `${api}?${queryString}`); - } else { - return Request("GET", `${api}/all?${queryString}`); - } - }; -}; + }); + } else { + return Request("POST", `${api}/list`, { + data: { ...data?.query }, + params: { + sort: data?.sort + } + }); + } +}; \ No newline at end of file