官网:https://www.quanzhan.co/luch-request/
/**
* @version 3.0.4
* @Author lu-ch
* @Email webwork.s@qq.com
* 文档: https://www.quanzhan.co/luch-request/
* github: https://github.com/lei-mu/luch-request
* DCloud: http://ext.dcloud.net.cn/plugin?id=392
* HBuilderX: beat-2.7.14 alpha-2.8.0
*/
import Request from '@/utils/luch-request/index.js'
import $mConfig from '@/config/index.config.js';
const getTokenStorage = () => {
let Authorization = ''
try {
Authorization = uni.getStorageSync('Authorization')
} catch (e) {}
return Authorization
}
const http = new Request()
http.setConfig((config) => {
/* 设置全局配置 */
// #ifdef H5
config.baseURL = "/api/graphql";
// #endif
// #ifdef MP-WEIXIN
config.baseURL = $mConfig.baseUrl /* 根域名不同 */
// #endif
config.header = {
...config.header
}
return config
})
http.interceptors.request.use((config) => {
/* 请求之前拦截器。可以使用async await 做异步操作 */
config.header = {
...config.header,
Authorization: getTokenStorage()
}
return config
}, (config) => {
return Promise.reject(config)
})
http.interceptors.response.use(async (response) => {
/* 请求之后拦截器。可以使用async await 做异步操作 */
let isShowApp = uni.getStorageSync("isShowApp") || null;
if (isShowApp && !isShowApp.show) {
return Promise.reject(null)
}
return response.data
}, (response) => { // 请求错误做点什么
// 小程序未登录弹窗
//#ifdef MP-WEIXIN
if (response.statusCode == 401) {
// 立马获取 获取不到 故延迟
setTimeout(() => {
const pages = getCurrentPages();
const curPage = pages[pages.length - 1].$page.fullPath;
uni.setStorageSync('redirectURL', curPage);
}, 1000)
uni.showModal({
title: "温馨提示",
content: "你还未登录,是否前往登录?",
success(res) {
if (res.confirm) {
uni.navigateTo({
url: "/pages/login/login"
})
}
}
})
return Promise.reject(null)
}
//#endif
return Promise.reject(response)
})
export {
http
}