|
|
@ -1,8 +1,8 @@ |
|
|
|
import axios from "axios"; |
|
|
|
import axios from 'axios' |
|
|
|
import util from "@/libs/util"; |
|
|
|
import Util from '@/libs/util' |
|
|
|
import router from "@/router"; |
|
|
|
import router from '@/router' |
|
|
|
import Setting from "@/setting"; |
|
|
|
import Setting from '@/setting' |
|
|
|
import { Message } from "element-ui"; |
|
|
|
import { Message } from 'element-ui' |
|
|
|
|
|
|
|
|
|
|
|
const service = axios.create({ |
|
|
|
const service = axios.create({ |
|
|
|
baseURL: Setting.apiBaseURL, |
|
|
|
baseURL: Setting.apiBaseURL, |
|
|
@ -10,22 +10,22 @@ const service = axios.create({ |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// post请求头
|
|
|
|
// post请求头
|
|
|
|
service.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8"; |
|
|
|
service.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8" |
|
|
|
|
|
|
|
|
|
|
|
// 请求拦截器
|
|
|
|
// 请求拦截器
|
|
|
|
service.interceptors.request.use(config => { |
|
|
|
service.interceptors.request.use(config => { |
|
|
|
util.getToken(); |
|
|
|
Util.getToken(); |
|
|
|
let token = util.local.get(Setting.tokenKey); |
|
|
|
let token = Util.local.get(Setting.tokenKey) |
|
|
|
if (token) config.headers.token = token; |
|
|
|
if (token) config.headers.token = token |
|
|
|
return config; |
|
|
|
return config |
|
|
|
}, err => { |
|
|
|
}, err => { |
|
|
|
util.errorMsg({ |
|
|
|
Util.errorMsg({ |
|
|
|
message: "退出登陆", |
|
|
|
message: "退出登陆", |
|
|
|
onClose: function() { |
|
|
|
onClose: function () { |
|
|
|
router.push({ name: "/login" }); |
|
|
|
router.push({ name: "/login" }) |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}) |
|
|
|
return Promise.reject(err); |
|
|
|
return Promise.reject(err) |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
let logouted = 0; |
|
|
|
let logouted = 0; |
|
|
@ -34,9 +34,23 @@ service.interceptors.response.use( |
|
|
|
response => { |
|
|
|
response => { |
|
|
|
const res = response.data; |
|
|
|
const res = response.data; |
|
|
|
if (res.status == 200 || res.status == 10000 || res.status == 30001) { |
|
|
|
if (res.status == 200 || res.status == 10000 || res.status == 30001) { |
|
|
|
return Promise.resolve(res).catch(e => {}); |
|
|
|
return Promise.resolve(res).catch(e => { }); |
|
|
|
|
|
|
|
} else if (res.code === 401) { |
|
|
|
|
|
|
|
// 账号互踢
|
|
|
|
|
|
|
|
if (!logouted) { |
|
|
|
|
|
|
|
Util.local.remove(Setting.storeKey) |
|
|
|
|
|
|
|
Util.local.remove(Setting.tokenKey) |
|
|
|
|
|
|
|
Util.errorMsg(res.msg.includes('顶') ? '您的账号已在其他设备登录,您已被迫下线!' : '登录过期,请重新登录!') |
|
|
|
|
|
|
|
setTimeout(() => { |
|
|
|
|
|
|
|
router.replace({ |
|
|
|
|
|
|
|
path: '/login' |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
logouted = 0 |
|
|
|
|
|
|
|
}, 1500) |
|
|
|
|
|
|
|
logouted = 1 |
|
|
|
|
|
|
|
} |
|
|
|
} else if (!res.status) { |
|
|
|
} else if (!res.status) { |
|
|
|
return Promise.resolve(res).catch(e => {}); |
|
|
|
return Promise.resolve(res).catch(e => { }); |
|
|
|
} else if (res.status === 10008) { |
|
|
|
} else if (res.status === 10008) { |
|
|
|
Message.error({ |
|
|
|
Message.error({ |
|
|
|
message: res.message, |
|
|
|
message: res.message, |
|
|
@ -45,7 +59,7 @@ service.interceptors.response.use( |
|
|
|
}) |
|
|
|
}) |
|
|
|
return Promise.reject(res) |
|
|
|
return Promise.reject(res) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
util.errorMsg(res.message); |
|
|
|
Util.errorMsg(res.message); |
|
|
|
return Promise.reject(res) |
|
|
|
return Promise.reject(res) |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
@ -58,52 +72,35 @@ service.interceptors.response.use( |
|
|
|
// 在登录成功后返回当前页面,这一步需要在登录页操作。
|
|
|
|
// 在登录成功后返回当前页面,这一步需要在登录页操作。
|
|
|
|
case 401: |
|
|
|
case 401: |
|
|
|
if (!logouted) { |
|
|
|
if (!logouted) { |
|
|
|
util.local.remove(Setting.storeKey); |
|
|
|
Util.local.remove(Setting.storeKey) |
|
|
|
util.local.remove(Setting.tokenKey); |
|
|
|
Util.local.remove(Setting.tokenKey) |
|
|
|
util.errorMsg("登录过期,请重新登录"); |
|
|
|
Util.errorMsg("登录过期,请重新登录") |
|
|
|
setTimeout(() => { |
|
|
|
setTimeout(() => { |
|
|
|
router.replace({ |
|
|
|
router.replace({ |
|
|
|
path: "/login" |
|
|
|
path: "/login" |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, 1000); |
|
|
|
}, 1000) |
|
|
|
logouted = 1 |
|
|
|
logouted = 1 |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break |
|
|
|
case 500: |
|
|
|
case 500: |
|
|
|
util.errorMsg("网络错误"); |
|
|
|
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" |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}, 1000); |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
// 404请求不存在
|
|
|
|
// 404请求不存在
|
|
|
|
case 404: |
|
|
|
case 404: |
|
|
|
util.errorMsg("网络请求不存在!"); |
|
|
|
Util.errorMsg("网络请求不存在!") |
|
|
|
break; |
|
|
|
break; |
|
|
|
// 其他错误,直接抛出错误提示
|
|
|
|
// 其他错误,直接抛出错误提示
|
|
|
|
default: |
|
|
|
default: |
|
|
|
util.errorMsg(error.response.data.message); |
|
|
|
Util.errorMsg(error.response.data.message) |
|
|
|
Promise.reject(res); |
|
|
|
Promise.reject(res); |
|
|
|
} |
|
|
|
} |
|
|
|
return Promise.reject(error.response); |
|
|
|
return Promise.reject(error.response) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
function get(url, params) { |
|
|
|
function get (url, params) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
service.get(url, { params: params }).then(res => { |
|
|
|
service.get(url, { params: params }).then(res => { |
|
|
|
resolve(res); |
|
|
|
resolve(res); |
|
|
@ -113,7 +110,7 @@ function get(url, params) { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function post(url, params) { |
|
|
|
function post (url, params) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
service.post(url, params).then(res => { |
|
|
|
service.post(url, params).then(res => { |
|
|
|
resolve(res); |
|
|
|
resolve(res); |
|
|
@ -123,7 +120,7 @@ function post(url, params) { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function del(url, params) { |
|
|
|
function del (url, params) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
service.delete(url, { |
|
|
|
service.delete(url, { |
|
|
|
params |
|
|
|
params |
|
|
@ -135,7 +132,7 @@ function del(url, params) { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function put(url, params) { |
|
|
|
function put (url, params) { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
service.put(url, params).then(res => { |
|
|
|
service.put(url, params).then(res => { |
|
|
|
resolve(res); |
|
|
|
resolve(res); |
|
|
|