menus.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import request from "@/utils/request";
  2. // 使用导入的 request 函数进行网络请求
  3. export const table = (params: { table: string }) => {
  4. return request({
  5. url: "/admin/table",
  6. method: "GET",
  7. data: params,
  8. });
  9. };
  10. const customRequest = (options: any) => {
  11. return request(options)
  12. .then(response => response.data)
  13. .catch(error => {
  14. if (error.response && error.response.status === 409) {
  15. console.error('数据重复:', error.response.data);
  16. throw new Error('数据重复,请重新添加');
  17. } else {
  18. console.error(`请求失败: ${error.message || '未知错误'}`);
  19. throw new Error(`请求失败: ${error.message || '未知错误'}`);
  20. }
  21. });
  22. };
  23. // 提交编辑数据
  24. export const updateItem = (data: { table: string, item: any }) => {
  25. return customRequest({
  26. url: "/admin/update_item",
  27. method: "PUT",
  28. data: data,
  29. });
  30. };
  31. // 提交新增数据
  32. export const addItem = (data: { table: string, item: any }) => {
  33. return customRequest({
  34. url: "/admin/add_item",
  35. method: "POST",
  36. data: data,
  37. }).catch(error => {
  38. if (error.message.includes("数据重复")) {
  39. alert(error.message);
  40. }
  41. throw error;
  42. });
  43. };
  44. // 删除
  45. export const deleteItemApi = (params: { table: string; condition: any }) => {
  46. const conditionString = `${Object.keys(params.condition)[0]}=${params.condition[Object.keys(params.condition)[0]]}`;
  47. return customRequest({
  48. url: "/admin/delete_item",
  49. method: "POST",
  50. data: {
  51. table: params.table,
  52. condition: conditionString,
  53. },
  54. });
  55. };
  56. // 下载模板
  57. export const downloadTemplate = (table: string, format: string = 'xlsx') => {
  58. return customRequest({
  59. url: "/admin/download_template",
  60. method: "GET",
  61. params: { table, format },
  62. responseType: 'blob',
  63. }).then(response => {
  64. const blob = new Blob([response], {
  65. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  66. });
  67. const url = window.URL.createObjectURL(blob);
  68. const link = document.createElement('a');
  69. link.href = url;
  70. link.download = `${table}_template.${format}`;
  71. link.click();
  72. window.URL.revokeObjectURL(url);
  73. });
  74. };
  75. export const exportData = (table: string, format: string = 'excel') => {
  76. const backendFormat = format.toLowerCase() === 'xlsx' ? 'excel' : format;
  77. //表格数据并导出
  78. return customRequest({
  79. url: "/admin/export_data",
  80. method: "GET",
  81. params: { table, format: backendFormat },
  82. responseType: 'blob',
  83. }).then(response => {
  84. const blob = new Blob([response], {
  85. type: backendFormat === 'excel'
  86. ? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  87. : 'text/csv',
  88. });
  89. const url = window.URL.createObjectURL(blob);
  90. const link = document.createElement('a');
  91. link.href = url;
  92. link.download = `${table}_data.${backendFormat === 'excel' ? 'xlsx' : 'csv'}`;
  93. link.click();
  94. window.URL.revokeObjectURL(url);
  95. }).catch(error => {
  96. console.error('导出数据时发生错误:', error);
  97. throw error;
  98. });
  99. };
  100. // 导入数据
  101. export const importData = (table: string, file: File) => {
  102. const formData = new FormData();
  103. formData.append('file', file);
  104. formData.append('table', table);
  105. return customRequest({
  106. url: "/admin/import_data",
  107. method: "POST",
  108. data: formData,
  109. headers: {
  110. 'Content-Type': 'multipart/form-data',
  111. },
  112. });
  113. };