uni-app 跨平台、项目级别的请求库 luch-request封装

官网: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
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值