2
0

file.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import axios from 'axios';
  2. import type { MinioUploadPolicy, OSSUploadPolicy } from '@d8d-appcontainer/types';
  3. import type {
  4. FileLibrary, FileCategory
  5. } from '../../share/types.ts';
  6. const API_BASE_URL = '/api';
  7. interface FileUploadPolicyResponse {
  8. message: string;
  9. data: MinioUploadPolicy | OSSUploadPolicy;
  10. }
  11. interface FileListResponse {
  12. message: string;
  13. data: {
  14. list: FileLibrary[];
  15. pagination: {
  16. current: number;
  17. pageSize: number;
  18. total: number;
  19. };
  20. };
  21. }
  22. interface FileSaveResponse {
  23. message: string;
  24. data: FileLibrary;
  25. }
  26. interface FileInfoResponse {
  27. message: string;
  28. data: FileLibrary;
  29. }
  30. interface FileDeleteResponse {
  31. message: string;
  32. }
  33. interface FileCategoryListResponse {
  34. data: FileCategory[];
  35. total: number;
  36. page: number;
  37. pageSize: number;
  38. }
  39. interface FileCategoryCreateResponse {
  40. message: string;
  41. data: FileCategory;
  42. }
  43. interface FileCategoryUpdateResponse {
  44. message: string;
  45. data: FileCategory;
  46. }
  47. interface FileCategoryDeleteResponse {
  48. message: string;
  49. }
  50. // 文件管理API
  51. export const FileAPI = {
  52. // 获取文件上传策略
  53. getUploadPolicy: async (filename: string, prefix: string = 'uploads/', maxSize: number = 10 * 1024 * 1024): Promise<FileUploadPolicyResponse> => {
  54. try {
  55. const response = await axios.get(`${API_BASE_URL}/upload/policy`, {
  56. params: { filename, prefix, maxSize }
  57. });
  58. return response.data;
  59. } catch (error) {
  60. throw error;
  61. }
  62. },
  63. // 保存文件信息
  64. saveFileInfo: async (fileData: Partial<FileLibrary>): Promise<FileSaveResponse> => {
  65. try {
  66. const response = await axios.post(`${API_BASE_URL}/upload/save`, fileData);
  67. return response.data;
  68. } catch (error) {
  69. throw error;
  70. }
  71. },
  72. // 获取文件列表
  73. getFileList: async (params?: {
  74. page?: number,
  75. pageSize?: number,
  76. category_id?: number,
  77. fileType?: string,
  78. keyword?: string
  79. }): Promise<FileListResponse> => {
  80. try {
  81. const response = await axios.get(`${API_BASE_URL}/upload/list`, { params });
  82. return response.data;
  83. } catch (error) {
  84. throw error;
  85. }
  86. },
  87. // 获取单个文件信息
  88. getFileInfo: async (id: number): Promise<FileInfoResponse> => {
  89. try {
  90. const response = await axios.get(`${API_BASE_URL}/upload/${id}`);
  91. return response.data;
  92. } catch (error) {
  93. throw error;
  94. }
  95. },
  96. // 更新文件下载计数
  97. updateDownloadCount: async (id: number): Promise<FileDeleteResponse> => {
  98. try {
  99. const response = await axios.post(`${API_BASE_URL}/upload/${id}/download`);
  100. return response.data;
  101. } catch (error) {
  102. throw error;
  103. }
  104. },
  105. // 删除文件
  106. deleteFile: async (id: number): Promise<FileDeleteResponse> => {
  107. try {
  108. const response = await axios.delete(`${API_BASE_URL}/upload/${id}`);
  109. return response.data;
  110. } catch (error) {
  111. throw error;
  112. }
  113. },
  114. // 获取文件分类列表
  115. getCategories: async (params?: {
  116. page?: number,
  117. pageSize?: number,
  118. search?: string
  119. }): Promise<FileCategoryListResponse> => {
  120. try {
  121. const response = await axios.get(`${API_BASE_URL}/file-categories`, { params });
  122. return response.data;
  123. } catch (error) {
  124. throw error;
  125. }
  126. },
  127. // 创建文件分类
  128. createCategory: async (data: Partial<FileCategory>): Promise<FileCategoryCreateResponse> => {
  129. try {
  130. const response = await axios.post(`${API_BASE_URL}/file-categories`, data);
  131. return response.data;
  132. } catch (error) {
  133. throw error;
  134. }
  135. },
  136. // 更新文件分类
  137. updateCategory: async (id: number, data: Partial<FileCategory>): Promise<FileCategoryUpdateResponse> => {
  138. try {
  139. const response = await axios.put(`${API_BASE_URL}/file-categories/${id}`, data);
  140. return response.data;
  141. } catch (error) {
  142. throw error;
  143. }
  144. },
  145. // 删除文件分类
  146. deleteCategory: async (id: number): Promise<FileCategoryDeleteResponse> => {
  147. try {
  148. const response = await axios.delete(`${API_BASE_URL}/file-categories/${id}`);
  149. return response.data;
  150. } catch (error) {
  151. throw error;
  152. }
  153. }
  154. };