87 lines
2.1 KiB
JavaScript
87 lines
2.1 KiB
JavaScript
import Vue from 'vue'
|
|
import Request from 'luch-request'
|
|
import config from '@/config'
|
|
import { getToken } from '@/utils/auth.js'
|
|
// import store from '@/store';
|
|
|
|
// const baseUrl = config.loginBaseUrl
|
|
const baseUrl = config.lpLoginUrl
|
|
console.log('baseUrl-请求', baseUrl)
|
|
|
|
const http = new Request({
|
|
// baseURL: "http://172.16.19.33:8081/prod-api", //设置请求的base url
|
|
// baseURL: "/dev-api", //设置请求的base url
|
|
baseURL: baseUrl, //设置请求的base url
|
|
timeout: 300000, //超时时长5分钟,
|
|
header: {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
Authorization: getToken()
|
|
}
|
|
})
|
|
|
|
//请求拦截器
|
|
http.interceptors.request.use(
|
|
config => {
|
|
// 可使用async await 做异步操作
|
|
// console.log('请求拦截器');
|
|
|
|
// // console.log(config);
|
|
// if (config.method === 'POST' && config.data.h) {
|
|
// config.header = {
|
|
// ...config.header,
|
|
// 'Content-Type': 'application/json;charset=UTF-8;'
|
|
// }
|
|
// // config.data = JSON.stringify(config.data);
|
|
// }else{
|
|
// config.header = {
|
|
// ...config.header,
|
|
// // 'uToken':Vue.prototype.$store.state.token,
|
|
// 'content-type':'application/x-www-form-urlencoded',
|
|
// }
|
|
// }
|
|
|
|
// // if(getToken()){
|
|
// // console.log(">>>>>>",getToken());
|
|
// // config.header.Authorization='Bearer ' +getToken();
|
|
// // }
|
|
if (config.data && !config.data.noShowLoading) {
|
|
uni.showLoading({
|
|
title: '加载中'
|
|
})
|
|
}
|
|
console.log(config.data)
|
|
|
|
return config
|
|
},
|
|
error => {
|
|
return Promise.resolve(error)
|
|
}
|
|
)
|
|
|
|
// 响应拦截器
|
|
http.interceptors.response.use(
|
|
response => {
|
|
// console.log('响应拦截器');
|
|
uni.hideLoading()
|
|
return response
|
|
},
|
|
error => {
|
|
if (error.statusCode == 302) {
|
|
uni.clearStorageSync()
|
|
uni.switchTab({
|
|
url: '/pages/gzt/index'
|
|
})
|
|
}
|
|
//未登录时清空缓存跳转
|
|
if (error.statusCode == 401) {
|
|
uni.clearStorageSync()
|
|
uni.switchTab({
|
|
// url: "/pages/changguan/subscribe-fill"
|
|
url: '/pages/gzt/index'
|
|
})
|
|
}
|
|
return Promise.resolve(error)
|
|
}
|
|
)
|
|
export default http
|