選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

167 行
4.5 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, onResponse, 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. if(onResponse){
  70. onResponse();
  71. }
  72. }
  73. ).catch(error => {
  74. return handleError(error,onError);
  75. });
  76. };
  77. export const reportDownload = ({url, filename, onError}) =>{
  78. axios.get( url,
  79. {
  80. responseType: 'blob',
  81. }
  82. ).then(
  83. (response)=>{
  84. const url = URL.createObjectURL(response.data);
  85. const a = document.createElement('a');
  86. a.href = url;
  87. a.setAttribute("download", filename);
  88. document.body.appendChild(a);
  89. a.click();
  90. document.body.removeChild(a);
  91. URL.revokeObjectURL(url);
  92. }
  93. ).catch(error => {
  94. return handleError(error,onError);
  95. });
  96. };
  97. export const fileUpload = ({ refType, refId, files, refCode, onSuccess, onFail, onError}) =>{
  98. // console.log(files);
  99. // var formData = new FormData();
  100. // for (let i = 0; i < files.length; i++){
  101. // const file = files[i]
  102. // formData.append("multipartFileList", file);
  103. // }
  104. // // formData.append("multipartFile", file);
  105. // formData.append("refType", refType);
  106. // formData.append("refId", refId);
  107. // if(refCode){
  108. // formData.append("refCode", refCode);
  109. // }
  110. // console.log(formData)
  111. // axios.post(FILE_UP_POST,formData,{
  112. // headers: {
  113. // "Content-Type":"multipart/form-data"
  114. // }
  115. // }).then(
  116. // (response)=>{
  117. // onResponse(response,onSuccess,onFail);
  118. // }
  119. // ).catch(error => { return handleError(error, onError); });
  120. postWithFiles({
  121. url: FILE_UP_POST,
  122. params:{
  123. refType: refType,
  124. refId: refId,
  125. refCode: refCode
  126. },
  127. files: files,
  128. onSuccess: onSuccess,
  129. onFail:onFail,
  130. onError:onError
  131. });
  132. };
  133. const onResponse= (response, onSuccess, onFail) =>{
  134. if (response.status >= 300 ||response.status < 200) {
  135. console.log("onFail");
  136. if(onFail){
  137. onFail(response);
  138. }else{
  139. console.log(response);
  140. }
  141. return;
  142. }
  143. if(onSuccess){
  144. onSuccess(response.data);
  145. }
  146. }
  147. const handleError= (error, onError) =>{
  148. if(onError){
  149. return onError(error);
  150. }else{
  151. console.log(error);
  152. return false;
  153. }
  154. }