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.
103 lines
2.7 KiB
103 lines
2.7 KiB
/*---------------------------------------------------------------
|
|
| 请求接口封装 |
|
|
---------------------------------------------------------------*/
|
|
import axios from "axios";
|
|
import curry from "dai-js/tools/curry";
|
|
import { Message } from "element-ui";
|
|
import { clearLoginInfo } from "@/utils";
|
|
|
|
axios.defaults.withCredentials=true
|
|
axios.defaults.crossDomain=true
|
|
|
|
const request = curry(
|
|
(method, url, data = {}, headers = {}, progress = () => {}) => {
|
|
return new Promise((reslove) => {
|
|
let returnIniData = {
|
|
httpCode: "",
|
|
data: {},
|
|
msg: "",
|
|
code: "",
|
|
};
|
|
|
|
// 添加服务器端URL
|
|
function processUrl(url) {
|
|
if (url.indexOf("http://") > -1 || url.indexOf("https://") > -1) {
|
|
return url;
|
|
}
|
|
return process.env.VUE_APP_API_SERVER + url;
|
|
}
|
|
|
|
url = processUrl(url);
|
|
|
|
const succFn = (res) => {
|
|
// log(`[request成功] ${url}`, data, res);
|
|
let retData = {
|
|
...returnIniData,
|
|
...res.data,
|
|
httpCode: res.statusCode,
|
|
};
|
|
// if(typeof Vue.$afterRequestHook == 'function'){
|
|
// retData = Vue.$afterRequestHook(retData);
|
|
// }
|
|
if (res.data.code > 8000 && res.data.code < 10000) {
|
|
// Message.error(res.data.msg);
|
|
}
|
|
if (res.data.code == 10005 || res.data.code == 10006 || res.data.code == 10007) {
|
|
// 10005 token为空 10006登陆超时 10007别处登录
|
|
console.log("登录失效");
|
|
Message.error(res.data.msg);
|
|
clearLoginInfo();
|
|
return next({
|
|
name: "login",
|
|
});
|
|
}
|
|
|
|
reslove(retData);
|
|
};
|
|
|
|
const failFn = (err) => {
|
|
// log(`[request失败] ${url}`, data, err);
|
|
|
|
reslove(
|
|
Object.assign({}, returnIniData, {
|
|
httpCode: "9999", //访问出现意外
|
|
msg: "网络错误",
|
|
})
|
|
);
|
|
};
|
|
|
|
if (method.toUpperCase() == "POST") {
|
|
axios
|
|
.post(url, data, {
|
|
headers,
|
|
responseType: "json",
|
|
// progress,
|
|
// credentials: false,
|
|
})
|
|
.then(succFn)
|
|
.catch(failFn);
|
|
} else {
|
|
axios
|
|
.get(url, {
|
|
params: data,
|
|
headers,
|
|
responseType: "json",
|
|
// credentials: true,
|
|
})
|
|
.then(succFn)
|
|
.catch(failFn);
|
|
}
|
|
});
|
|
}
|
|
);
|
|
|
|
export const requestGet = request("get");
|
|
|
|
export const requestPost = request("post");
|
|
|
|
export default {
|
|
install(Vue) {
|
|
Vue.prototype.$requestGet = requestGet;
|
|
Vue.prototype.$requestPost = requestPost;
|
|
},
|
|
};
|
|
|