mediaLibrary.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. axios,
  3. getMediaList,
  4. getLibraryGroup,
  5. addGroupUrl,
  6. deleteGroupUrl,
  7. getAllGroup,
  8. uploadMedia,
  9. editMedia,
  10. deleteMedia,
  11. downFile,
  12. downhash,
  13. PaggingReq,
  14. PaggingRes,
  15. } from "@/request";
  16. // 媒体库数据类型
  17. export type Media = {
  18. id?: string;
  19. dictName: string;
  20. fileTypeStr: string;
  21. fileFormat: string;
  22. fileSize: string;
  23. dictId: Number | string;
  24. status: string | number;
  25. fileType: string;
  26. dictNameStr: string;
  27. createTime: string;
  28. updateTime: string;
  29. };
  30. // 媒体库分页查询参数类型
  31. type MediaPaggingParams = PaggingReq<{
  32. name: string;
  33. fileType: string; // 确保这里有 fileType 字段
  34. dictId: Number | string; // 分组ID
  35. }>;
  36. // 获取媒体库列表数据
  37. export const getMediaPagging = async (params: MediaPaggingParams) =>
  38. (await axios.post(getMediaList, params)).data as PaggingRes<Media>;
  39. // 获取分组列表
  40. export const getGroupList = async ({type, pageNum, pageSize}) =>
  41. (await axios.get(getLibraryGroup, { params: { type, pageNum, pageSize } })).data;
  42. // 获取所有分组列表
  43. export const getAllGroupList = async (params) =>
  44. (await axios.post(getAllGroup, params)).data;
  45. // 新增分组
  46. export const addGroupItem = async (dictName: string) =>
  47. (await axios.post(addGroupUrl, { dictName })).data;
  48. // 删除分组
  49. export const deleteGroupItem = async (dictId: Number | string) =>
  50. (await axios.post(deleteGroupUrl, { id: dictId })).data;
  51. // 上传媒体
  52. export const uploadNewMedia = async (formData) => {
  53. return (await axios.post(uploadMedia, formData)).data;
  54. }
  55. // 编辑媒体
  56. export const editMediaItem = async (formData) => {
  57. return (await axios.post(editMedia, formData)).data;
  58. }
  59. // 删除媒体
  60. export const deleteMediaItem = async (id) =>
  61. (await axios.post(deleteMedia, { id })).data;
  62. // 下载媒体
  63. export const downloadMedia = async (id) => {
  64. const response = await axios.get(downFile, { params: {dictFileId: id} });
  65. if (response.data) {
  66. // 创建一个隐藏的a标签
  67. const link = document.createElement('a');
  68. link.href = response.data;
  69. // 从URL中提取文件名
  70. const fileName = response.data.split('/').pop() || `file_${id}`;
  71. link.setAttribute('download', fileName);
  72. document.body.appendChild(link);
  73. link.click();
  74. document.body.removeChild(link);
  75. }
  76. return response.data;
  77. }
  78. // 下载hash
  79. export const downloadHash = async (id) => {
  80. const response = await axios.get(downhash, { params: {dictFileId: id} });
  81. if (response.data) {
  82. // 创建一个隐藏的a标签
  83. const link = document.createElement('a');
  84. link.href = response.data;
  85. // 从URL中提取文件名
  86. const fileName = response.data.split('/').pop() || `hash_${id}.txt`;
  87. link.setAttribute('download', fileName);
  88. document.body.appendChild(link);
  89. link.click();
  90. document.body.removeChild(link);
  91. }
  92. return response.data;
  93. }