大改动

This commit is contained in:
宇阳
2024-08-31 12:26:20 +08:00
parent 8b01fd0514
commit 5144f3452f
11 changed files with 94 additions and 166 deletions

View File

@@ -1,5 +1,6 @@
import Request from "@/utils/request"; import Request from "@/utils/request";
import { Article } from "@/types/app/article"; import { Article } from "@/types/app/article";
import { getListAPI } from "@/utils";
// 新增文章 // 新增文章
export const addArticleDataAPI = (data: Article) => export const addArticleDataAPI = (data: Article) =>
@@ -16,22 +17,24 @@ export const editArticleDataAPI = (data: Article) =>
// 获取文章 // 获取文章
export const getArticleDataAPI = (id?: number) => Request<Article>("GET", `/article/${id}`) export const getArticleDataAPI = (id?: number) => Request<Article>("GET", `/article/${id}`)
// export const getArticleListAPI = (data?: QueryData) => {
// if (data?.pagination) {
// return Request<Paginate<Article[]>>("POST", `/article/paging`, {
// data: { ...data?.query },
// params: {
// sort: data.sort,
// ...data.pagination
// }
// });
// } else {
// return Request<Article[]>("POST", `/article/list`, {
// data: { ...data?.query },
// params: {
// sort: data?.sort
// }
// });
// }
// };
// 获取文章列表 // 获取文章列表
export const getArticleListAPI = (data?: QueryData) => { export const getArticleListAPI = (data?: QueryData) => getListAPI<Article>("/article", data)
if (data?.pagination) {
return Request<Paginate<Article[]>>("POST", `/article/paging`, {
data: { ...data?.query },
params: {
sort: data.sort,
...data.pagination
}
});
} else {
return Request<Article[]>("POST", `/article/list`, {
data: { ...data?.query },
params: {
sort: data?.sort
}
});
}
};

View File

@@ -15,21 +15,4 @@ export const editCateDataAPI = (data: Cate) => Request<Cate>("PATCH", "/cate", {
export const getCateDataAPI = (id?: number) => Request<Cate>("GET", `/cate/${id}`) export const getCateDataAPI = (id?: number) => Request<Cate>("GET", `/cate/${id}`)
// 获取分类列表 // 获取分类列表
export const getCateListAPI = (data?: QueryData) => { export const getCateListAPI = (data?: QueryData) => getListAPI<Cate>("/cate", data)
if (data?.pagination) {
return Request<Paginate<Cate[]>>("POST", `/cate/paging`, {
data: { ...data?.query },
params: {
sort: data.sort,
...data.pagination
}
});
} else {
return Request<Cate[]>("POST", `/cate/list`, {
data: { ...data?.query },
params: {
sort: data?.sort
}
});
}
};

View File

@@ -18,21 +18,4 @@ export const editCommentDataAPI = (data: Comment) => Request<Comment>("PATCH", "
export const getCommentDataAPI = (id?: number) => Request<Paginate<Comment>>("GET", `/comment/${id}`) export const getCommentDataAPI = (id?: number) => Request<Paginate<Comment>>("GET", `/comment/${id}`)
// 获取评论列表 // 获取评论列表
export const getCommentListAPI = (data?: QueryData) => { export const getCommentListAPI = (data?: QueryData) => getListAPI<Comment>("/comment", data)
if (data?.pagination) {
return Request<Paginate<Comment[]>>("POST", `/comment/paging`, {
data: { ...data?.query },
params: {
sort: data.sort,
...data.pagination
}
});
} else {
return Request<Comment[]>("POST", `/comment/list`, {
data: { ...data?.query },
params: {
sort: data?.sort
}
});
}
};

View File

@@ -1,5 +1,6 @@
import Request from '@/utils/request' import Request from '@/utils/request'
import { File } from '@/types/app/file' import { File } from '@/types/app/file'
import { getListAPI } from '@/utils'
// 删除文件 // 删除文件
export const delFileDataAPI = (filePath: string) => Request<File>("DELETE", `/file?filePath=${filePath}`) export const delFileDataAPI = (filePath: string) => Request<File>("DELETE", `/file?filePath=${filePath}`)
@@ -8,23 +9,7 @@ export const delFileDataAPI = (filePath: string) => Request<File>("DELETE", `/fi
export const getFileDataAPI = (filePath: string) => Request<File>("GET", `/file/info?filePath=${filePath}`) export const getFileDataAPI = (filePath: string) => Request<File>("GET", `/file/info?filePath=${filePath}`)
// 获取文件列表 // 获取文件列表
export const getFileListAPI = (data?: QueryData) => { export const getFileListAPI = (data?: QueryData) => getListAPI<File>("/file", data)
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<Paginate<File[]>>("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<File[]>("GET", `/file/all${sort}${dir}`);
}
};
// 获取目录列表 // 获取目录列表
export const getDirListAPI = () => Request<string[]>("GET", '/file/dir');; export const getDirListAPI = () => Request<string[]>("GET", '/file/dir');;

View File

@@ -2,4 +2,4 @@ import { Rss } from '@/types/app/rss';
import { getListAPI } from '@/utils'; import { getListAPI } from '@/utils';
// 获取订阅数据列表 // 获取订阅数据列表
export const getRssListAPI = getListAPI<Rss>("/rss") export const getRssListAPI = (data?: QueryData) => getListAPI<Rss>("/rss", data)

View File

@@ -15,4 +15,5 @@ export const editSwiperDataAPI = (data: Swiper) => Request<Swiper>("PATCH", "/sw
export const getSwiperDataAPI = (id?: number) => Request<Swiper>("GET", `/swiper/${id}`) export const getSwiperDataAPI = (id?: number) => Request<Swiper>("GET", `/swiper/${id}`)
// 获取轮播图列表 // 获取轮播图列表
export const getSwiperListAPI = getListAPI<Swiper>("/swiper") // export const getSwiperListAPI = getListAPI<Swiper>("/swiper")
export const getSwiperListAPI = (data?: QueryData) => getListAPI<Swiper>("/swiper", data)

View File

@@ -15,4 +15,4 @@ export const editTagDataAPI = (data: Tag) => Request<Tag>("PATCH", "/tag", { dat
export const getTagDataAPI = (id?: number) => Request<Tag>("GET", `/tag/${id}`) export const getTagDataAPI = (id?: number) => Request<Tag>("GET", `/tag/${id}`)
// 获取标签列表 // 获取标签列表
export const getTagListAPI = getListAPI<Tag>("/tag") export const getTagListAPI = (data?: QueryData) => getListAPI<Tag>("/tag", data)

View File

@@ -15,7 +15,7 @@ export const editLinkDataAPI = (data: Web) => Request<Web>("PATCH", "/link", { d
export const getLinkDataAPI = (id?: number) => Request<Web>("GET", `/link/${id}`) export const getLinkDataAPI = (id?: number) => Request<Web>("GET", `/link/${id}`)
// 获取网站列表 // 获取网站列表
export const getLinkListAPI = getListAPI<Web>("/link") export const getLinkListAPI = (data?: QueryData) => getListAPI<Web>("/link", data)
// 获取网站类型列表 // 获取网站类型列表
export const getLinkTypeListAPI = () => { export const getLinkTypeListAPI = () => {

View File

@@ -6,6 +6,7 @@ import { useUserStore } from '@/stores';
import { baseURL } from '@/utils/request'; import { baseURL } from '@/utils/request';
import Compressor from 'compressorjs'; import Compressor from 'compressorjs';
import { useState } from 'react'; import { useState } from 'react';
import { UploadFile } from 'antd/es/upload';
const { Dragger } = Upload; const { Dragger } = Upload;
@@ -20,7 +21,9 @@ export default ({ dir, open, onCancel, onSuccess }: UploadFileProps) => {
const store = useUserStore(); const store = useUserStore();
const [quality, setQuality] = useState(1000); const [quality, setQuality] = useState(1000);
const [isCompressionUpload, setIsCompressionUpload] = useState(false); const [isCompressionUpload, setIsCompressionUpload] = useState(false);
const [isLoading, setIsLoading] = useState(false); // 添加加载状态 const [isLoading, setIsLoading] = useState(false);
const [fileList, setFileList] = useState<UploadFile[]>([]);
const uploadProps: UploadProps = { const uploadProps: UploadProps = {
name: 'files', name: 'files',
@@ -30,63 +33,74 @@ export default ({ dir, open, onCancel, onSuccess }: UploadFileProps) => {
headers: { headers: {
"Authorization": `Bearer ${store.token}` "Authorization": `Bearer ${store.token}`
}, },
showUploadList: false, // 不显示文件列表 showUploadList: true,
async onChange(info) { async onChange(info) {
const { status } = info.file; const { status, response } = info.file;
let res; if (status !== 'uploading' && response?.code === 400) return message.error(response.message);
if (status !== 'uploading') {
res = info?.file?.response; setFileList(info.fileList);
if (res?.code === 400) return message.error(res.message);
}
if (status === 'done') { if (status === 'done') {
// // 复制文件链接到剪贴板 message.success(`${info.file.name} 文件上传成功`);
await navigator.clipboard.writeText(res.data.join("\n"));
console.log(5555, res.data.join("\n"));
message.success(`🎉 文件上传成功URL链接已复制到剪贴板`);
onSuccess(res.data.join("\n"));
onCloseModel()
} else if (status === 'error') { } 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) => { beforeUpload: async (file) => {
setIsLoading(true); // 开始加载状态 setIsLoading(true);
if (quality === 1000) return file if (quality === 1000) return file;
// 对图片进行压缩处理
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
new Compressor(file, { new Compressor(file, {
quality, quality,
success: (file) => { success: (compressedFile) => {
resolve(file); resolve(compressedFile);
}, },
error: (err) => { error: (err) => {
reject(err); reject(err);
}, },
}); });
}) });
}, },
className: "py-4" className: "py-4"
}; };
// 初始化操作
const onCloseModel = () => { const onCloseModel = () => {
setIsCompressionUpload(false); setIsCompressionUpload(false);
setQuality(1000); setQuality(1000);
setIsLoading(false); // 确保关闭时停止加载状态 setIsLoading(false);
setFileList([]);
onCancel(); onCancel();
} };
return ( return (
<> <>
<Modal title="文件上传" open={open} onCancel={onCloseModel} footer={null}> <Modal title="文件上传" open={open} onCancel={onCloseModel} footer={null}>
<Spin spinning={isLoading}> {/* 包裹内容的 Spin 组件 */} <Spin spinning={isLoading}>
<div className='my-4'> <div className='my-4'>
<Radio.Group defaultValue={0} value={isCompressionUpload ? 1 : 0} onChange={(e) => setIsCompressionUpload(e.target.value ? true : false)}> <Radio.Group defaultValue={0} value={isCompressionUpload ? 1 : 0} onChange={(e) => setIsCompressionUpload(e.target.value ? true : false)}>
<Radio value={0}></Radio> <Radio value={0}></Radio>
@@ -115,7 +129,7 @@ export default ({ dir, open, onCancel, onSuccess }: UploadFileProps) => {
} }
</div> </div>
<Dragger {...uploadProps}> <Dragger {...uploadProps} fileList={fileList}>
<p className="ant-upload-drag-icon"> <p className="ant-upload-drag-icon">
<InboxOutlined /> <InboxOutlined />
</p> </p>

View File

@@ -23,10 +23,11 @@ const CommentPage = () => {
setLoading(false) setLoading(false)
} }
const auditCommentData = async (id: number) => { const auditCommentData = async () => {
setLoading(true) setLoading(true)
await auditCommentDataAPI(id); await auditCommentDataAPI(comment?.id!);
getCommentList(); getCommentList();
setIsModalOpen(false)
message.success('🎉 审核评论成功'); message.success('🎉 审核评论成功');
}; };
@@ -155,7 +156,7 @@ const CommentPage = () => {
: <Tag bordered={false} color="error"></Tag>} : <Tag bordered={false} color="error"></Tag>}
</div> </div>
{!comment?.auditStatus ? <Button type="primary" className='w-full !mt-4' onClick={() => auditCommentData(1)}></Button> : null} {!comment?.auditStatus ? <Button type="primary" className='w-full !mt-4' onClick={auditCommentData}></Button> : null}
</div> </div>
</Modal> </Modal>
</> </>

View File

@@ -10,63 +10,21 @@ export const ObjectToUrlParam = (obj: Object): string => {
).toString(); ).toString();
} }
// 查询数据相关逻辑 export const getListAPI = <T>(api: string, data?: QueryData) => {
// export const getListAPI = <T>(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<Paginate<T[]>>("GET", `${api}${sort}${pagination}${query}`);
// } else {
// return Request<T[]>("GET", `${api}/all`, {
// sort: data?.sort,
// pattern: data?.pattern,
// ...data?.query
// });
// }
// }
// };
const buildQueryParams = (params: Record<string, any>): 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))}`;
}
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
})
.join('&');
};
export const getListAPI = <T>(api: string) => {
return (data?: QueryData) => {
let queryParams: Record<string, any> = {};
if (data?.sort) {
queryParams.sort = data.sort;
}
if (data?.query) {
queryParams = { ...queryParams, ...data.query };
}
if (data?.pagination) { if (data?.pagination) {
const { page, size } = data.pagination; return Request<Paginate<T[]>>("POST", `${api}/paging`, {
if (page) queryParams.page = page; data: { ...data?.query },
if (size) queryParams.size = size; params: {
sort: data.sort,
...data.pagination
} }
});
const queryString = buildQueryParams(queryParams);
if (data?.pagination) {
return Request<Paginate<T[]>>("GET", `${api}?${queryString}`);
} else { } else {
return Request<T[]>("GET", `${api}/all?${queryString}`); return Request<T[]>("POST", `${api}/list`, {
data: { ...data?.query },
params: {
sort: data?.sort
}
});
} }
};
}; };