You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

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