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.
133 lines
3.8 KiB
133 lines
3.8 KiB
import configs from "../configs";
|
|
import axios from "axios";
|
|
import { bus } from "../bus.js";
|
|
|
|
// axios.defaults.withCredentials = true;
|
|
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
|
|
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';//配置请求头
|
|
|
|
let instance = axios.create();
|
|
// 添加一个请求拦截器
|
|
instance.interceptors.request.use(
|
|
config => {
|
|
let token = sessionStorage.getItem("token") || "";
|
|
if (token) {
|
|
config.headers.encodeToken = token;
|
|
}
|
|
// config.headers.encodeToken =
|
|
// "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NzEyMjQ3Mjc1OTcsInBheWxvYWQiOiJcIjE4MDYwMTE0MDkyODM4NDEwMXwxNTM5Njg4NzI3NTc5XCIifQ.sJBefUfJwybtWTNauW3j-w8VA6BlV56ASKmgQvDJc_Y";
|
|
console.log(config);
|
|
return config;
|
|
},
|
|
err => {
|
|
return Promise.reject(err);
|
|
}
|
|
);
|
|
// 添加一个响应拦截器
|
|
instance.interceptors.response.use(
|
|
function(response) {
|
|
if (response.data && response.data.errcode) {
|
|
if (parseInt(response.data.errcode) === 40001) {
|
|
//未登录
|
|
bus.$emit("goto", "/login");
|
|
}
|
|
}
|
|
return response;
|
|
},
|
|
function(error) {
|
|
// Do something with response error
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
//基地址
|
|
let basePath = configs.baseURL;
|
|
|
|
/**
|
|
* @param path {string} path
|
|
* @param opts {Object} options
|
|
* @return {?Object}
|
|
*
|
|
* @description
|
|
* Normalizes the given path, returning a regular expression
|
|
* and the original path.
|
|
*
|
|
* Inspired by pathRexp in visionmedia/express/lib/utils.js.
|
|
*/
|
|
const pathReplace = function(path, opts) {
|
|
const ret = {
|
|
originalPath: path,
|
|
patternPath: path
|
|
};
|
|
|
|
path = path
|
|
.replace(/([().])/g, "\\$1")
|
|
.replace(/(\/)?:(\w+)(\*\?|[?*])?/g, function(_, slash, key, option) {
|
|
const optional = option === "?" || option === "*?" ? "?" : null;
|
|
const star = option === "*" || option === "*?" ? "*" : null;
|
|
slash = slash || "";
|
|
opts[key] =
|
|
typeof opts[key] === "number"
|
|
? opts[key].toString()
|
|
: opts[key];
|
|
return (
|
|
"" +
|
|
(optional ? "" : slash) +
|
|
(opts[key] != "" ? opts[key] : "${" + key + "}") +
|
|
(optional || "")
|
|
);
|
|
});
|
|
|
|
ret.patternPath = path;
|
|
return ret;
|
|
};
|
|
|
|
// 处理http请求
|
|
const config = function(config, params) {
|
|
//提取接口url路径
|
|
const url = basePath + pathReplace(config.url, params || {}).patternPath;
|
|
|
|
return {
|
|
// 发送请求
|
|
request: function(obj) {
|
|
// 全部接口带token
|
|
obj = obj || {};
|
|
obj.params = obj.params || {};
|
|
// obj.params.token = localStorage.getItem('user_key')
|
|
|
|
//整合请求配置
|
|
let newConfig = Object.assign({}, obj || {}, config, {
|
|
url: url
|
|
});
|
|
// console.log(newConfig)
|
|
// newConfig.headers.Authorization=params.token
|
|
//执行http接口请求
|
|
return instance(newConfig).then(successCallback, errorCallback);
|
|
}
|
|
};
|
|
};
|
|
|
|
// 请求成功回调
|
|
const successCallback = result => {
|
|
// if (result.data.code === 666) {
|
|
// sessionStorage.removeItem("token");
|
|
// }
|
|
// -402用户认证失败,请重新登录 -101没有操作权限
|
|
if (result.data.error_code === -402) {
|
|
sessionStorage.clear();
|
|
localStorage.removeItem("user_key");
|
|
//未登录
|
|
bus.$emit("goto", "/login");
|
|
}
|
|
|
|
return result.data;
|
|
};
|
|
|
|
// 请求失败回调
|
|
const errorCallback = result => {
|
|
return result.data;
|
|
};
|
|
|
|
export default {
|
|
config: config
|
|
};
|