Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

189 righe
4.4 KiB

  1. import axios from 'axios';
  2. import { FILE_UP_POST, FILE_DOWN_GET } from '../utils/ApiPathConst';
  3. import qs from 'qs';
  4. export const get = ({ url, params, onSuccess, onFail, onError, onFinally }) => {
  5. axios
  6. .get(url, {
  7. params,
  8. paramsSerializer: (p) => qs.stringify(p, { arrayFormat: 'repeat' }) // <-- FIX
  9. })
  10. .then((response) => {
  11. onResponse(response, onSuccess, onFail);
  12. })
  13. .catch((error) => {
  14. return handleError(error, onError);
  15. })
  16. .finally(() => {
  17. if (typeof onFinally === 'function') {
  18. onFinally();
  19. }
  20. });
  21. };
  22. //TODO
  23. export const put = ({ url, params, onSuccess, onFail, onError }) => {
  24. axios
  25. .put(url, params)
  26. .then((response) => {
  27. onResponse(response, onSuccess, onFail);
  28. })
  29. .catch((error) => {
  30. return handleError(error, onError);
  31. });
  32. };
  33. export const patch = ({ url, params, onSuccess, onFail, onError }) => {
  34. axios
  35. .patch(url, params)
  36. .then((response) => {
  37. onResponse(response, onSuccess, onFail);
  38. })
  39. .catch((error) => {
  40. return handleError(error, onError);
  41. });
  42. };
  43. export const del = ({ url, params, onSuccess, onFail, onError }) => {
  44. axios
  45. .delete(url, { params })
  46. .then((response) => {
  47. onResponse(response, onSuccess, onFail);
  48. })
  49. .catch((error) => {
  50. return handleError(error, onError);
  51. });
  52. };
  53. export const post = ({ url, params, onSuccess, onFail, onError, headers }) => {
  54. headers = headers
  55. ? headers
  56. : {
  57. 'Content-Type': 'application/json'
  58. };
  59. axios
  60. .post(url, params, {
  61. headers: headers
  62. })
  63. .then((response) => {
  64. onResponse(response, onSuccess, onFail);
  65. })
  66. .catch((error) => {
  67. return handleError(error, onError);
  68. });
  69. };
  70. export const postWithFiles = ({ url, params, files, onSuccess, onFail, onError }) => {
  71. var formData = new FormData();
  72. for (let i = 0; i < files.length; i++) {
  73. const file = files[i];
  74. formData.append('multipartFileList', file);
  75. }
  76. if (params)
  77. for (var key in params) {
  78. if (typeof params[key] === 'object') {
  79. formData.append(key, JSON.stringify(params[key]));
  80. } else {
  81. formData.append(key, params[key]);
  82. }
  83. }
  84. axios
  85. .post(url, formData, { headers: { 'Content-Type': 'multipart/form-data' } })
  86. .then((response) => {
  87. onResponse(response, onSuccess, onFail);
  88. })
  89. .catch((error) => {
  90. return handleError(error, onError);
  91. });
  92. };
  93. export const fileDownload = ({ url, fileId, skey, params, method, onResponse, onError }) => {
  94. if (!url) {
  95. url = FILE_DOWN_GET + '/' + fileId + '/' + skey;
  96. }
  97. if (method == 'post') {
  98. axios
  99. .post(url, params, {
  100. responseType: 'blob',
  101. headers: {
  102. 'Content-Type': 'application/json'
  103. }
  104. })
  105. .then((response) => {
  106. fileDownloadResponse(response, onResponse);
  107. })
  108. .catch((error) => {
  109. return handleError(error, onError);
  110. });
  111. } else {
  112. axios
  113. .get(url, {
  114. responseType: 'blob',
  115. params: params
  116. })
  117. .then((response) => {
  118. fileDownloadResponse(response, onResponse);
  119. })
  120. .catch((error) => {
  121. return handleError(error, onError);
  122. });
  123. }
  124. };
  125. const fileDownloadResponse = (response, onResponse) => {
  126. const cd = response.headers?.['content-disposition'];
  127. const fn = cd?.split('filename=')[1]?.replaceAll('"', '')?.trim() || 'export.xlsx';
  128. const url = URL.createObjectURL(response.data);
  129. const a = document.createElement('a');
  130. a.href = url;
  131. a.download = fn;
  132. document.body.appendChild(a);
  133. a.click();
  134. document.body.removeChild(a);
  135. URL.revokeObjectURL(url);
  136. onResponse?.();
  137. };
  138. export const fileUpload = ({ refType, refId, files, refCode, onSuccess, onFail, onError }) => {
  139. postWithFiles({
  140. url: FILE_UP_POST,
  141. params: {
  142. refType: refType,
  143. refId: refId,
  144. refCode: refCode
  145. },
  146. files: files,
  147. onSuccess: onSuccess,
  148. onFail: onFail,
  149. onError: onError
  150. });
  151. };
  152. const onResponse = (response, onSuccess, onFail) => {
  153. if (response.status >= 300 || response.status < 200) {
  154. console.log('onFail');
  155. if (onFail) {
  156. onFail(response);
  157. } else {
  158. console.log(response);
  159. }
  160. return;
  161. }
  162. if (onSuccess) {
  163. onSuccess(response?.data);
  164. }
  165. };
  166. const handleError = (error, onError) => {
  167. if (onError) {
  168. return onError(error);
  169. } else {
  170. // console.log(error);
  171. return false;
  172. }
  173. };