在这里插入图片描述

全局请求封装

export function http(path, params = {}, loading = true, method = "POST") {
	console.log('%c请求拦截:', ' background:orange', params);
	if (loading) {
		uni.showLoading({
			title: "加载中",
			mask: true
		});
	};

	return new Promise((resolve, reject) => {
		uni.request({
			header: {
				Authorization: uni.getStorageSync("Authorization") || ""
			},
			url: import.meta.env.VITE_BASE_URL + path,
			method,
			data: params,
			async success(res) {
				uni.hideLoading();
				resolve(res.data);
				console.log('响应拦截:', path, params, res.data);
				if (res.data?.code !== 0) {
					uni.showToast({
						icon: "error",
						duration: 2000,
						title: res.data.msg
					});
				}

			},
			fail(err) {
				uni.hideLoading();
				uni.reLaunch({
					url: "/pages/status/service/error"
				})
				reject(err);
			},
			complete() {
				// uni.hideLoading();    // 在showToast之前执行会受影响
			}
		});
	});
};

局部业务接口封装

在这里插入图片描述

使用接口

在这里插入图片描述

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐