init project
This commit is contained in:
99
src/utils/ajax.js
Normal file
99
src/utils/ajax.js
Normal file
@@ -0,0 +1,99 @@
|
||||
//设置axios
|
||||
import qs from 'qs';
|
||||
import axios from 'axios'
|
||||
import { Message } from 'view-design'
|
||||
|
||||
|
||||
|
||||
export default {
|
||||
post(url, params) {
|
||||
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';//配置请求头
|
||||
axios.defaults.transformRequest = [
|
||||
function (data) {
|
||||
return qs.stringify(data)
|
||||
}
|
||||
];
|
||||
var res = axios.post(`${url}`, params).then((res) => {
|
||||
console.log("res:", res)
|
||||
if (res.status == 200) {
|
||||
if (res.data.exception != undefined && res.data.exception) {
|
||||
//统一处理异常
|
||||
Message.error(res.data.msg)
|
||||
return Promise.reject(res.data)
|
||||
} else {
|
||||
return res.data;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
Message.error("操作失败,服务端出现异常错误!")
|
||||
})
|
||||
return res;
|
||||
},
|
||||
postjson(url, params) {
|
||||
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8'//配置请求头
|
||||
axios.defaults.transformRequest = [
|
||||
function (data) {
|
||||
return JSON.stringify(data)
|
||||
}
|
||||
];
|
||||
var res = axios.post(`${url}`, params).then((res) => {
|
||||
console.log("res:", res)
|
||||
if (res.status == 200) {
|
||||
if (res.data.exception != undefined && res.data.exception) {
|
||||
//统一处理异常
|
||||
Message.error(res.data.msg)
|
||||
return Promise.reject(res.data)
|
||||
} else {
|
||||
return res.data;
|
||||
}
|
||||
}
|
||||
else
|
||||
Message.error("操作失败,服务端出现异常错误!")
|
||||
})
|
||||
return res;
|
||||
},
|
||||
getServerHost() {
|
||||
return serverHost;
|
||||
},
|
||||
postfile(url, params, formData) {
|
||||
console.log("postfile:", url, params)
|
||||
axios.defaults.headers.post['Content-Type'] = 'multipart/form-data;'//配置请求头
|
||||
axios.defaults.transformRequest = [
|
||||
function (data) {
|
||||
return data;
|
||||
}
|
||||
];
|
||||
var res = axios.post(`${url}`, params).then((res) => {
|
||||
console.log("res:", res)
|
||||
if (res.status == 200) {
|
||||
if (res.data.exception != undefined && res.data.exception) {
|
||||
//统一处理异常
|
||||
Message.error(res.data.msg)
|
||||
return Promise.reject(res.data)
|
||||
} else {
|
||||
return res.data;
|
||||
}
|
||||
}
|
||||
else
|
||||
Message.error("操作失败,服务端出现异常错误!")
|
||||
})
|
||||
return res;
|
||||
},
|
||||
download(url, fileName) {
|
||||
axios({
|
||||
method: 'get',
|
||||
url: `${url}`,
|
||||
responseType: 'blob'
|
||||
}).then(res => {
|
||||
const blob = new Blob([res.data]);
|
||||
console.log(blob.size)
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = fileName;
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(url);
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user