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.
42 lines
1.1 KiB
42 lines
1.1 KiB
5 years ago
|
import Request from '@/js_sdk/luch-request/request'
|
||
|
import store from '@/store'
|
||
|
import { getToken } from '@/common/auth'
|
||
|
|
||
|
const http = new Request();
|
||
|
|
||
|
http.setConfig((config) => { /* 设置全局配置 */
|
||
|
config.baseUrl = 'http://39.98.253.192/api'; /* 根域名不同 */
|
||
|
return config;
|
||
|
})
|
||
|
|
||
|
http.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
|
||
|
if (store.getters.token) {
|
||
|
config.header['token'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
|
||
|
}
|
||
|
return config;
|
||
|
})
|
||
|
|
||
|
http.interceptor.response((response) => { /* 请求之后拦截器 */
|
||
|
const res = response.data
|
||
|
if (res.code !== 0) {
|
||
|
uni.showToast({title: res.msg, duration:2000});
|
||
|
|
||
|
// 10001 无效的token或者token已过期
|
||
|
if (res.code === 10001) {
|
||
|
uni.showModal({
|
||
|
title: '重新登录',
|
||
|
content: 'token已经过期,需重新登录',
|
||
|
success: function (res) {
|
||
|
store.dispatch('FedLogOut').then(() => {
|
||
|
location.reload() // 为了重新实例化vue-router对象 避免bug
|
||
|
})
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
return Promise.reject('error')
|
||
|
} else {
|
||
|
return response.data
|
||
|
}
|
||
|
})
|
||
|
|
||
|
export default http;
|