material.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { asyncTimeout, jsonToForm } from "@/utils";
  2. import { PagingRequest, PagingResult } from ".";
  3. import {
  4. ADD_MATERIAL,
  5. DEL_MATERIAL,
  6. MATERIAL_GROUP_LIST,
  7. MATERIAL_PAG,
  8. UPLOAD_HEADS,
  9. } from "./constant";
  10. import axios from "./instance";
  11. type ServiceMaterialGroup = {
  12. dictKey: string;
  13. dictName: string;
  14. id: number;
  15. };
  16. type ServiceMaterial = {
  17. createTime: string;
  18. dictId: number;
  19. dictName: string;
  20. fileFormat: string;
  21. fileName: string;
  22. fileSize: string;
  23. fileType: string;
  24. fileUrl: string;
  25. id: number;
  26. name: string;
  27. newFileName: string;
  28. typeKey: string;
  29. updateTime: string;
  30. uploadId: number;
  31. };
  32. export type MaterialGroup = {
  33. id: number;
  34. name: string;
  35. };
  36. export type Material = {
  37. id: number;
  38. name: string;
  39. format: string;
  40. url: string;
  41. size: number;
  42. groupId: number;
  43. group: string;
  44. uploadId?: number;
  45. modelId?: number;
  46. };
  47. export type MaterialPageProps = PagingRequest<
  48. Partial<Material> & { groupIds: number[], formats: string[] }
  49. >;
  50. export const fetchMaterialPage = async (params: MaterialPageProps) => {
  51. //
  52. const material = await axios.post<PagingResult<ServiceMaterial[]>>(MATERIAL_PAG, {
  53. pageNum: params.pageNum,
  54. pageSize: params.pageSize,
  55. name: params.name,
  56. dictIds: params.groupIds,
  57. fileFormats: params.formats
  58. });
  59. const nm = {
  60. ...material,
  61. list: material.list.map((item): Material => ({
  62. id: item.id,
  63. name: item.fileName,
  64. format: item.fileFormat,
  65. url: item.fileUrl,
  66. size: Number(item.fileSize),
  67. groupId: item.dictId,
  68. group: item.dictName,
  69. uploadId: item.uploadId
  70. }))
  71. }
  72. return nm;
  73. };
  74. export const fetchMaterialGroups = async () => {
  75. return (await axios.get<ServiceMaterialGroup[]>(MATERIAL_GROUP_LIST)).map(
  76. (item) => ({
  77. name: item.dictName,
  78. id: item.id,
  79. })
  80. ) as MaterialGroup[];
  81. };
  82. export const addMaterial = (file: File) => {
  83. return axios<string>({
  84. method: "POST",
  85. url: ADD_MATERIAL,
  86. data: jsonToForm({ file }),
  87. headers: { ...UPLOAD_HEADS },
  88. });
  89. };
  90. export const delMaterial = (id: Material["id"]) => {
  91. return axios.post(DEL_MATERIAL, { id });
  92. };