module.exports = (vm) => {
uni.$u.http.setConfig((config) => {
config.baseURL = 'https://api.shop.eduwork.cn'
config.timeout = 10000
return config
})
uni.$u.http.interceptors.request.use((config) => {
if (vm.$store.state.login.token_type && vm.$store.state.login.token)
config.header['Authorization'] = vm.$store.state.login.token_type + vm.$store.state.login.token
else {
vm.$u.route({
url: 'pages/login/login',
})}
return config
}, config => {
return Promise.reject(config)
})
uni.$u.http.interceptors.response.use((response) => {
const {
statusCode,
data,
} = response;
if(response.data){
if (200 <= statusCode < 300) return data
else if (statusCode === 400) {
vm.$u.toast(data.message)
return false
} else if (statusCode === 401) {
vm.$u.toast('验证失败,请重新登录')
setTimeout(() => {
vm.$u.route('@/pages/login/login')
}, 1000)
} else if (statusCode === 400) {
const {
errors
} = data
vm.$u.toast(Object.values(errors)[0][0])
return false
} else return data.message
}
else{
return response
}
}, (response) => {
return Promise.reject(response)
})
vm.$u.patch = (url, params = {}) => {
const _params = {
...params,
_method: 'PATCH'
}
console.log(uni.$u.http.post);
return uni.$u.http.post(url, _params)
}
}
|