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.
120 lines
3.2 KiB
120 lines
3.2 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';//配置请求头
|
|
|
|
// 添加一个请求拦截器
|
|
// axios.interceptors.request.use(function (config) {
|
|
// console.log(config);
|
|
|
|
// return config;
|
|
// }, function (error) {
|
|
// // Do something with request error
|
|
// return Promise.reject(error);
|
|
// });
|
|
|
|
// 添加一个响应拦截器
|
|
axios.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 axios(newConfig).then(successCallback, errorCallback);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 请求成功回调
|
|
const successCallback = (result) => {
|
|
// -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
|
|
}
|