Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

164 рядки
4.4 KiB

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