177 lines
4.4 KiB
JavaScript
177 lines
4.4 KiB
JavaScript
import Request from '@/common/luch-request/index.js'
|
||
import {ACCESS_TOKEN} from '@/common/util/constants.js'
|
||
import configService from './config.service.js'
|
||
import tip from '@/common/util/tip.js';
|
||
import store from '@/store/index.js';
|
||
import api from "@/api/api"
|
||
|
||
let apiUrl = configService.apiUrl;
|
||
|
||
// const getTokenStorage = () => {
|
||
// let token = ''
|
||
// try{
|
||
// token = uni.getStorageSync(ACCESS_TOKEN)
|
||
// }catch(e){
|
||
// //TODO handle the exception
|
||
// console.log("getTokenStorage",token)
|
||
// }
|
||
// return token
|
||
// }
|
||
|
||
|
||
|
||
const http = new Request()
|
||
http.setConfig((config) => { /* 设置全局配置 */
|
||
config.baseUrl = apiUrl /* 根域名不同 */
|
||
config.header = {
|
||
...config.header
|
||
}
|
||
return config
|
||
})
|
||
|
||
/**
|
||
* 自定义验证器,如果返回true 则进入响应拦截器的响应成功函数(resolve),否则进入响应拦截器的响应错误函数(reject)
|
||
* @param { Number } statusCode - 请求响应体statusCode(只读)
|
||
* @return { Boolean } 如果为true,则 resolve, 否则 reject
|
||
*/
|
||
// 有默认,非必写
|
||
http.validateStatus = (statusCode) => {
|
||
return statusCode === 200
|
||
}
|
||
|
||
http.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
|
||
config.header = {
|
||
...config.header,
|
||
// 'X-Access-Token':getTokenStorage()
|
||
'app-id':uni.getAccountInfoSync().miniProgram.appId,
|
||
"client-type":"MA",
|
||
'third-session':uni.getStorageSync('third_session')
|
||
}
|
||
/*
|
||
if (!token) { // 如果token不存在,调用cancel 会取消本次请求,但是该函数的catch() 仍会执行
|
||
cancel('token 不存在') // 接收一个参数,会传给catch((err) => {}) err.errMsg === 'token 不存在'
|
||
}
|
||
*/
|
||
return config
|
||
})
|
||
|
||
// 必须使用异步函数,注意
|
||
http.interceptor.response(async (response) => { /* 请求之后拦截器 */
|
||
// if (response.data.code !== 200) { // 服务端返回的状态码不等于200,则reject()
|
||
// return Promise.reject(response)
|
||
// }
|
||
console.log(response.data.code,'状态码')
|
||
if(response.data.code == 60001){
|
||
uni.showToast({
|
||
title: response.data.msg,
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
setTimeout(function(){
|
||
uni.removeStorageSync('third_session')
|
||
uni.switchTab({
|
||
url:'/pages/index/index'
|
||
})
|
||
// uni.reLaunch({
|
||
// url:'/packDetail/pages/login/login'
|
||
// })
|
||
},200)
|
||
}
|
||
return response
|
||
}, (response) => {
|
||
// 请求错误做点什么
|
||
console.log("请求错误做点什么",response);
|
||
if (response) {
|
||
let data = response.data
|
||
console.log(data,'状态码')
|
||
const token = uni.getStorageSync(ACCESS_TOKEN)
|
||
console.log("------异常响应------",token)
|
||
console.log("------异常响应------",data.status)
|
||
switch (data.status) {
|
||
case 403:
|
||
uni.showToast({
|
||
title: response.data.message,
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
setTimeout(function(){
|
||
uni.removeStorageSync('third_session')
|
||
uni.removeStorageSync('constant')
|
||
uni.reLaunch({
|
||
url:'/packDetail/pages/login/login'
|
||
})
|
||
},2000)
|
||
tip.error('拒绝访问');
|
||
break
|
||
case 500:
|
||
uni.showToast({
|
||
title: response.data.message,
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
setTimeout(function(){
|
||
uni.removeStorageSync('third_session')
|
||
uni.removeStorageSync('constant')
|
||
uni.reLaunch({
|
||
url:'/packDetail/pages/login/login'
|
||
})
|
||
},2000)
|
||
break
|
||
case 404:
|
||
uni.showToast({
|
||
title: response.data.message,
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
setTimeout(function(){
|
||
uni.removeStorageSync('third_session')
|
||
uni.removeStorageSync('constant')
|
||
uni.reLaunch({
|
||
url:'/packDetail/pages/login/login'
|
||
})
|
||
},2000)
|
||
break
|
||
case 504:
|
||
uni.showToast({
|
||
title: response.data.message,
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
setTimeout(function(){
|
||
uni.removeStorageSync('third_session')
|
||
uni.removeStorageSync('constant')
|
||
uni.reLaunch({
|
||
url:'/packDetail/pages/login/login'
|
||
})
|
||
},2000)
|
||
break
|
||
case 401:
|
||
uni.showToast({
|
||
title: response.data.message,
|
||
icon: 'none',
|
||
duration: 2000
|
||
})
|
||
setTimeout(function(){
|
||
uni.removeStorageSync('third_session')
|
||
uni.removeStorageSync('constant')
|
||
uni.reLaunch({
|
||
url:'/packDetail/pages/login/login'
|
||
})
|
||
},2000)
|
||
break
|
||
default:
|
||
tip.error({
|
||
duration: 0,
|
||
forbidClick: true,
|
||
message: data.message
|
||
});
|
||
break
|
||
}
|
||
}
|
||
return response
|
||
})
|
||
|
||
export {
|
||
http
|
||
}
|