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.
132 lines
3.3 KiB
132 lines
3.3 KiB
|
10 months ago
|
import axios from "axios";
|
||
|
|
import { commonStore, loadingStore } from "@/stores/commonStore";
|
||
|
|
import { storage } from "@/utils/storage";
|
||
|
|
import router from "@/router";
|
||
|
|
|
||
|
|
const loading = loadingStore();
|
||
|
|
|
||
|
|
const API_URL = import.meta.env.VITE_APP_API_SERVER_URL;
|
||
|
|
console.log("API URL:", API_URL);
|
||
|
|
export const request = {
|
||
|
|
post: (uri: string, param: any): any => {
|
||
|
|
return axios.post(`${API_URL}${uri}`, param);
|
||
|
|
},
|
||
|
|
get: (uri: string, param: any): any => {
|
||
|
|
return axios.get(`${API_URL}${uri}`, { params: param });
|
||
|
|
},
|
||
|
|
delete: (uri: string, param: any): any => {
|
||
|
|
return axios.delete(`${API_URL}${uri}`, param);
|
||
|
|
},
|
||
|
|
put: (uri: string, param: any): any => {
|
||
|
|
return axios.put(`${API_URL}${uri}`, param);
|
||
|
|
},
|
||
|
|
postFile: (uri: string, param: any, attachment: any, progress: any): any => {
|
||
|
|
const formData = new FormData();
|
||
|
|
|
||
|
|
for (const key in attachment) {
|
||
|
|
if (Object.prototype.hasOwnProperty.call(attachment, key)) {
|
||
|
|
const value = attachment[key];
|
||
|
|
|
||
|
|
if (Array.isArray(value)) {
|
||
|
|
for (const file of value) {
|
||
|
|
formData.append(key, file);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
formData.append(key, value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
formData.append("param", JSON.stringify(param));
|
||
|
|
|
||
|
|
return axios.post(`${API_URL}${uri}`, formData, {
|
||
|
|
headers: {
|
||
|
|
"Content-Type": "multipart/form-data",
|
||
|
|
},
|
||
|
|
onUploadProgress: progress,
|
||
|
|
});
|
||
|
|
},
|
||
|
|
postResponseFile: (
|
||
|
|
uri: string,
|
||
|
|
param: any,
|
||
|
|
responseParam: any,
|
||
|
|
attachment: any,
|
||
|
|
progress: any,
|
||
|
|
): any => {
|
||
|
|
const formData = new FormData();
|
||
|
|
for (const key in attachment) {
|
||
|
|
if (Object.prototype.hasOwnProperty.call(attachment, key)) {
|
||
|
|
const value = attachment[key];
|
||
|
|
if (Array.isArray(value)) {
|
||
|
|
for (const file of value) {
|
||
|
|
formData.append(key, file);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
formData.append("param", JSON.stringify(param));
|
||
|
|
formData.append("responseParam", JSON.stringify(responseParam));
|
||
|
|
|
||
|
|
return axios.post(`${API_URL}${uri}`, formData, {
|
||
|
|
headers: {
|
||
|
|
"Content-Type": "multipart/form-data",
|
||
|
|
},
|
||
|
|
onUploadProgress: progress,
|
||
|
|
});
|
||
|
|
},
|
||
|
|
// postOptimization: (uri: string, param: any): any => {
|
||
|
|
// return axios.post(`${PYTHON_API_URL}${uri}`, param);
|
||
|
|
// },
|
||
|
|
|
||
|
|
// postPython: (uri: string, param: any): Promise<any> => {
|
||
|
|
// return axios.post(`${PYTHON_API_URL}${uri}`, param);
|
||
|
|
// },
|
||
|
|
};
|
||
|
|
|
||
|
|
// axios.defaults.withCredentials = true;
|
||
|
|
axios.defaults.headers.common["Access-Control-Allow-Origin"] = "*";
|
||
|
|
axios.interceptors.request.use(
|
||
|
|
(config) => {
|
||
|
|
loading.setLoading(true);
|
||
|
|
|
||
|
|
const token = storage.getToken();
|
||
|
|
if (token) {
|
||
|
|
config.headers.Authorization = `Bearer ${token}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
return config;
|
||
|
|
},
|
||
|
|
(error) => {
|
||
|
|
loading.setLoading(false);
|
||
|
|
|
||
|
|
console.log("request error", error);
|
||
|
|
|
||
|
|
const store = commonStore();
|
||
|
|
store.setSnackbarMsg({
|
||
|
|
text: "에러가 발생하였습니다.",
|
||
|
|
color: "red",
|
||
|
|
result: 500,
|
||
|
|
});
|
||
|
|
|
||
|
|
return Promise.reject(error);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
axios.interceptors.response.use(
|
||
|
|
(response) => {
|
||
|
|
loading.setLoading(false);
|
||
|
|
return response;
|
||
|
|
},
|
||
|
|
(error) => {
|
||
|
|
loading.setLoading(false);
|
||
|
|
|
||
|
|
const store = commonStore();
|
||
|
|
store.setSnackbarMsg({
|
||
|
|
text: "에러가 발생하였습니다.",
|
||
|
|
color: "red",
|
||
|
|
result: 500,
|
||
|
|
});
|
||
|
|
|
||
|
|
return Promise.reject(error);
|
||
|
|
},
|
||
|
|
);
|