更新脚手架!
This commit is contained in:
33
src/api/Axios.js
Normal file
33
src/api/Axios.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import axios from 'axios'
|
||||
import QS from 'qs';
|
||||
|
||||
// 创建axios
|
||||
const service = axios.create({
|
||||
baseURL: import.meta.env.VITE_API,
|
||||
timeout: 50000,
|
||||
});
|
||||
|
||||
|
||||
|
||||
// 添加请求拦截器
|
||||
service.interceptors.request.use((config) => {
|
||||
// 在发送请求之前做些什么
|
||||
config
|
||||
return config;
|
||||
}, function (error) {
|
||||
// 对请求错误做些什么
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
// 添加响应拦截器
|
||||
service.interceptors.response.use(
|
||||
(response) => {
|
||||
|
||||
return response.data;
|
||||
}, (error) => {
|
||||
|
||||
|
||||
return Promise.reject(error);
|
||||
});
|
||||
|
||||
export default service;
|
||||
9
src/api/api.js
Normal file
9
src/api/api.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import http from './http'
|
||||
|
||||
export function addSurveyed(data, authorization) {
|
||||
return http.post("/api/user/addSurveyed",
|
||||
data,
|
||||
true,
|
||||
authorization
|
||||
);
|
||||
}
|
||||
73
src/api/http.js
Normal file
73
src/api/http.js
Normal file
@@ -0,0 +1,73 @@
|
||||
import service from './Axios'
|
||||
import qs from "qs";
|
||||
|
||||
|
||||
// json格式请求头
|
||||
const headerJSON = {
|
||||
"Content-Type": "application/json;charset=UTF-8",
|
||||
};
|
||||
// FormData格式请求头
|
||||
const headerFormData = {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
};
|
||||
|
||||
|
||||
const http = {
|
||||
/**
|
||||
* methods: 请求
|
||||
* @param url 请求地址
|
||||
* @param params 请求参数
|
||||
* @param json 判断数据发送是否是json格式: true-为是 false-为否
|
||||
*/
|
||||
get(url, params, json, authorization) {
|
||||
if (authorization) {
|
||||
headerJSON['authorization'] = authorization
|
||||
}
|
||||
const config = {
|
||||
method: "get",
|
||||
url: url,
|
||||
headers: json ? headerJSON : headerFormData
|
||||
};
|
||||
if (params) config.params = params;
|
||||
return service(config);
|
||||
},
|
||||
post(url, params, json, authorization) {
|
||||
|
||||
if (authorization) {
|
||||
headerJSON['authorization'] = authorization
|
||||
}
|
||||
|
||||
const config = {
|
||||
method: "post",
|
||||
url: url,
|
||||
headers: json ? headerJSON : headerFormData
|
||||
};
|
||||
|
||||
if (params) config.data = json ? params : qs.stringify(params);
|
||||
|
||||
return service(config);
|
||||
},
|
||||
put(url, params, json) {
|
||||
const config = {
|
||||
method: "put",
|
||||
url: url,
|
||||
headers: headerFormData
|
||||
};
|
||||
if (params) config.params = params;
|
||||
return service(config);
|
||||
},
|
||||
delete(url, params, json) {
|
||||
const config = {
|
||||
method: "delete",
|
||||
url: url,
|
||||
headers: headerFormData
|
||||
};
|
||||
if (params) config.params = params;
|
||||
return service(config);
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
|
||||
//导出
|
||||
export default http;
|
||||
Reference in New Issue
Block a user