|
|
|
import axios from 'axios'
|
|
|
|
import util from '@/libs/util'
|
|
|
|
import router from '@/router/index'
|
|
|
|
import Setting from '@/setting'
|
|
|
|
|
|
|
|
const service = axios.create({
|
|
|
|
baseURL: Setting.apiBaseURL,
|
|
|
|
timeout: 10000000
|
|
|
|
})
|
|
|
|
// post请求头
|
|
|
|
service.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
|
|
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
service.interceptors.request.use(config => {
|
|
|
|
util.getToken()
|
|
|
|
let token = util.local.get(Setting.tokenKey)
|
|
|
|
if(token) config.headers.token = token
|
|
|
|
return config
|
|
|
|
},err => {
|
|
|
|
util.errorMsg({
|
|
|
|
message: '退出登陆',
|
|
|
|
onClose: function () {
|
|
|
|
router.push({name: '/login'})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return Promise.reject(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// 响应拦截器
|
|
|
|
service.interceptors.response.use(
|
|
|
|
response => {
|
|
|
|
const res = response.data
|
|
|
|
if(res.status == 200 || res.status == 10000) {
|
|
|
|
return Promise.resolve(res).catch(e => {})
|
|
|
|
}else if(!res.status){
|
|
|
|
return Promise.resolve(res).catch(e => {})
|
|
|
|
}else {
|
|
|
|
util.errorMsg(res.errmessage || res.message)
|
|
|
|
return Promise.reject(res)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// 服务器状态码不是200的情况
|
|
|
|
error => {
|
|
|
|
if (error.response.status) {
|
|
|
|
switch (error.response.status) {
|
|
|
|
// 401: 未登录
|
|
|
|
// 未登录则跳转登录页面,并携带当前页面的路径
|
|
|
|
// 在登录成功后返回当前页面,这一步需要在登录页操作。
|
|
|
|
case 401:
|
|
|
|
util.local.remove(Setting.storeKey)
|
|
|
|
util.local.remove(Setting.tokenKey)
|
|
|
|
util.errorMsg('登录过期,请重新登录')
|
|
|
|
setTimeout(() => {
|
|
|
|
router.replace({
|
|
|
|
path: '/login',
|
|
|
|
query: {
|
|
|
|
redirect: router.currentRoute.fullPath
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, 1000)
|
|
|
|
break
|
|
|
|
case 500:
|
|
|
|
util.errorMsg('网络错误')
|
|
|
|
break
|
|
|
|
// 403 token过期
|
|
|
|
// 登录过期对用户进行提示
|
|
|
|
// 清除本地token和清空vuex中token对象
|
|
|
|
// 跳转登录页面
|
|
|
|
case 403:
|
|
|
|
util.local.remove(Setting.storeKey)
|
|
|
|
util.local.remove(Setting.tokenKey)
|
|
|
|
util.errorMsg('登录过期,请重新登录')
|
|
|
|
// 清除token
|
|
|
|
// store.commit('loginSuccess', null);
|
|
|
|
// 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面
|
|
|
|
setTimeout(() => {
|
|
|
|
router.replace({
|
|
|
|
path: '/login',
|
|
|
|
query: {
|
|
|
|
redirect: router.currentRoute.fullPath
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, 1000)
|
|
|
|
break
|
|
|
|
// 404请求不存在
|
|
|
|
case 404:
|
|
|
|
util.errorMsg('网络请求不存在!')
|
|
|
|
break
|
|
|
|
// 其他错误,直接抛出错误提示
|
|
|
|
default:
|
|
|
|
util.errorMsg(error.response.data.message)
|
|
|
|
Promise.reject(res)
|
|
|
|
}
|
|
|
|
return Promise.reject(error.response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
function get(url, params){
|
|
|
|
return new Promise((resolve, reject) =>{
|
|
|
|
service.get(url, {params: params}).then(res => {
|
|
|
|
resolve(res)
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function post(url, params){
|
|
|
|
return new Promise((resolve, reject) =>{
|
|
|
|
service.post(url,params).then(res => {
|
|
|
|
resolve(res)
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err.data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function del(url, params){
|
|
|
|
return new Promise((resolve, reject) =>{
|
|
|
|
service.delete(url, {
|
|
|
|
params
|
|
|
|
}).then(res => {
|
|
|
|
resolve(res)
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err.data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function put(url, params){
|
|
|
|
return new Promise((resolve, reject) =>{
|
|
|
|
service.put(url, params).then(res => {
|
|
|
|
resolve(res)
|
|
|
|
}).catch(err => {
|
|
|
|
reject(err.data)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
export { get,post,del,put }
|