|
- import axios from "axios";
- import {FILE_UP_POST, FILE_DOWN_GET} from "../utils/ApiPathConst";
-
- export const get = ({url, params, onSuccess, onFail, onError}) =>{
- axios.get(url,{
- params: params
- }).then(
- (response)=>{onResponse(response, onSuccess, onFail);}
- ).catch(error => {
- return handleError(error,onError);
- });
- };
-
- //TODO
- export const put = ({url,params, onSuccess, onFail, onError}) =>{
- axios.put(url,params).then(
- (response)=>{onResponse(response, onSuccess, onFail);}
- ).catch(error => {
- return handleError(error,onError);
- });
- };
-
- export const post = ({url, params, onSuccess, onFail, onError, headers}) =>{
- headers = headers?headers:{
- "Content-Type":"application/json"
- };
-
- axios.post(url,params,
- {
- headers:headers
- }).then(
- (response)=>{onResponse(response, onSuccess, onFail);}
- ).catch(error => {
- return handleError(error,onError);
- });
- };
-
- export const postWithFiles = ({url, params, files, onSuccess, onFail, onError}) =>{
- var formData = new FormData();
- for (let i = 0; i < files.length; i++){
- const file = files[i]
- formData.append("multipartFileList", file);
- }
- for (var key in params) {
- if(typeof(params[key]) ==='object'){
- formData.append(key, JSON.stringify(params[key]));
- }else{
- formData.append(key, params[key]);
- }
- }
-
- axios.post(url,formData,
- {headers: {"Content-Type":"multipart/form-data"}})
- .then(
- (response)=>{onResponse(response, onSuccess, onFail);}
- ).catch(error => {
- return handleError(error,onError);
- });
- };
-
- export const fileDownload = ({fileId, skey, filename, onResponse, onError}) =>{
- axios.get( FILE_DOWN_GET+"/"+fileId+"/"+skey+"/"+filename,
- {
- responseType: 'blob',
- }
- ).then(
- (response)=>{
- const url = URL.createObjectURL(response.data);
- const a = document.createElement('a');
- a.href = url;
- a.setAttribute("download", filename);
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- URL.revokeObjectURL(url);
- if(onResponse){
- onResponse();
- }
- }
- ).catch(error => {
- return handleError(error,onError);
- });
- };
-
- export const reportDownload = ({url, filename, onError}) =>{
- axios.get( url,
- {
- responseType: 'blob',
- }
- ).then(
- (response)=>{
- const url = URL.createObjectURL(response.data);
- const a = document.createElement('a');
- a.href = url;
- a.setAttribute("download", filename);
- document.body.appendChild(a);
- a.click();
- document.body.removeChild(a);
- URL.revokeObjectURL(url);
- }
- ).catch(error => {
- return handleError(error,onError);
- });
- };
-
- export const fileUpload = ({ refType, refId, files, refCode, onSuccess, onFail, onError}) =>{
- // console.log(files);
- // var formData = new FormData();
- // for (let i = 0; i < files.length; i++){
- // const file = files[i]
- // formData.append("multipartFileList", file);
- // }
- // // formData.append("multipartFile", file);
- // formData.append("refType", refType);
- // formData.append("refId", refId);
- // if(refCode){
- // formData.append("refCode", refCode);
- // }
- // console.log(formData)
- // axios.post(FILE_UP_POST,formData,{
- // headers: {
- // "Content-Type":"multipart/form-data"
- // }
- // }).then(
- // (response)=>{
- // onResponse(response,onSuccess,onFail);
- // }
- // ).catch(error => { return handleError(error, onError); });
- postWithFiles({
- url: FILE_UP_POST,
- params:{
- refType: refType,
- refId: refId,
- refCode: refCode
- },
- files: files,
- onSuccess: onSuccess,
- onFail:onFail,
- onError:onError
- });
- };
-
-
- const onResponse= (response, onSuccess, onFail) =>{
- if (response.status >= 300 ||response.status < 200) {
- console.log("onFail");
- if(onFail){
- onFail(response);
- }else{
- console.log(response);
- }
- return;
- }
-
- if(onSuccess){
- onSuccess(response.data);
- }
- }
-
- const handleError= (error, onError) =>{
- if(onError){
- return onError(error);
- }else{
- console.log(error);
- return false;
- }
- }
|