web-base-h5/config/request.js

71 lines
1.8 KiB
JavaScript

/*
* @Descripttion:
* @version:
* @Author: kBank
* @Date: 2022-05-24 14:43:45
*/
import { getToken, removeToken } from '@/config/auth.js'
module.exports = (vm) => {
// 初始化请求配置
uni.$u.http.setConfig((config) => {
config.baseURL = 'https://p1.hzs413.com/inter-api';
// config.baseURL = 'http://192.168.31.137:8080';
// config.baseURL = 'https://cn.hzs413.com/inter-api/';
config.timeout = 80000;
return config
})
// 请求拦截
uni.$u.http.interceptors.request.use((config) => {
config.data = config.data || {}
config.header['Source'] = 3
let token = getToken();
let mToken = uni.getStorageSync('mToken');
let lang = uni.getStorageSync('lang');
if (lang) {
config.header['Accept-Language'] = lang
} else {
config.header['Accept-Language'] = 'zh-CN'
}
if (token) {
config.header['Authorization'] = token;
}
config.header['token'] = mToken || '';
return config
}, config => {
return Promise.reject(config)
})
// 响应拦截
uni.$u.http.interceptors.response.use((response) => {
const data = response.data
// uni.$u.toast(data.msg)
// if (data.code == 200) {
// return data;
// } else {
// token过期,清除token重新获取
if (data.code == 400 || data.code == 401 || data.code == 402) {
// uni.showToast({
// title: '登录超时',
// icon: 'none',
// duration: 1500,
// })
removeToken();
// 跳转到登录
setTimeout(() => {
uni.reLaunch({
url: '/pages/login/index'
})
}, 1500)
} else if (data.code == 500) {
uni.$u.toast(data.msg)
return data;
} else {
// uni.$u.toast(data.msg)
return data;
}
}, (response) => {
uni.$u.toast('服务器错误,请稍后重试')
})
}