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.
93 lines
1.9 KiB
93 lines
1.9 KiB
// const domain = 'http://read.2dan88.com'
|
|
// const config = {
|
|
// baseUrl:`${domain}/api`,
|
|
// siteUrl:domain
|
|
// }
|
|
|
|
import config from '../config/config.js'
|
|
import vuex from 'vuex'
|
|
|
|
//不需要登录即可访问的地址
|
|
const unNeedLogin = [
|
|
'/home/sendSmsCode',
|
|
'/user/register',
|
|
'/user/login',
|
|
'/user/retrievePassword'
|
|
];
|
|
|
|
|
|
const install = (Vue, vm) => {
|
|
Vue.prototype.$u.http.setConfig({
|
|
baseUrl: config.baseUrl
|
|
});
|
|
// 请求拦截,配置Token等参数
|
|
Vue.prototype.$u.http.interceptor.request = (config) => {
|
|
config.header['Content-Type'] = 'application/x-www-form-urlencoded;';
|
|
// formData
|
|
if (config.url.includes('/home/uploadFile')) {
|
|
config.header['Content-Type'] =
|
|
"multipart/form-data; boundary=----WebKitFormBoundaryW7uQCK3EFloZNjAB"
|
|
}
|
|
|
|
//不需要登录
|
|
if (!unNeedLogin.includes(config.url)) {
|
|
let lifeData = uni.getStorageSync('lifeData');
|
|
let ticket = (lifeData && lifeData.user) ? lifeData.user.ticket : undefined;
|
|
if (!ticket) {
|
|
uni.showToast({
|
|
icon: 'none',
|
|
title: '您还未登录,请先登录!',
|
|
duration: 1000,
|
|
success() {
|
|
setTimeout(() => {
|
|
uni.navigateTo({
|
|
url: '/pages/index/login'
|
|
});
|
|
}, 2000)
|
|
}
|
|
})
|
|
return false;
|
|
}
|
|
config.header.ticket = ticket;
|
|
|
|
}
|
|
|
|
return config;
|
|
}
|
|
// 响应拦截,判断状态码是否通过
|
|
Vue.prototype.$u.http.interceptor.response = (res) => {
|
|
uni.hideLoading();
|
|
uni.hideToast();
|
|
if (res.code == 601) {
|
|
uni.showToast({
|
|
icon: 'none',
|
|
title: '您的身份信息已过期!',
|
|
duration: 1000,
|
|
success() {
|
|
setTimeout(() => {
|
|
vm.user.ticket = ''
|
|
uni.navigateTo({
|
|
url: '/pages/index/login'
|
|
});
|
|
}, 2000)
|
|
}
|
|
})
|
|
} else if (res.code == 200) {
|
|
return res;
|
|
} else {
|
|
uni.showToast({
|
|
icon: 'none',
|
|
title: res.msg || '请求错误',
|
|
mask: true,
|
|
duration: 1000
|
|
})
|
|
|
|
|
|
return false;
|
|
};
|
|
}
|
|
}
|
|
|
|
export default {
|
|
install
|
|
}
|
|
|