From 560d4d05fee2de21aa4e35cf161309a7b943476b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=89=AC?= <1076535690@qq.com> Date: Fri, 6 Sep 2024 09:39:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AF=B7=E6=B1=82=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8=E5=9F=BA=E5=87=86URL=E9=80=BB=E8=BE=91=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E5=8F=8A=E5=B0=8F=E7=A8=8B=E5=BA=8F=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调整请求拦截器中的baseUrl定义,以适配不同的运行环境,特别是小程序环境。 --- src/interceptors/request.ts | 3 ++- src/utils/index.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index 4cd0204..79c0a49 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -2,6 +2,7 @@ import qs from 'qs' import { useUserStore } from '@/store' import { platform } from '@/utils/platform' +import { getEvnBaseUrl } from '@/utils' export type CustomRequestOptions = UniApp.RequestOptions & { query?: Record @@ -10,7 +11,7 @@ export type CustomRequestOptions = UniApp.RequestOptions & { } & IUniUploadFileOptions // 添加uni.uploadFile参数类型 // 请求基准地址 -const baseUrl = import.meta.env.VITE_SERVER_BASEURL +const baseUrl = getEvnBaseUrl() // 拦截器配置 const httpInterceptor = { diff --git a/src/utils/index.ts b/src/utils/index.ts index 5c5ee2e..c01f0a7 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1,6 @@ import { pages, subPackages, tabBar } from '@/pages.json' +import { isMp } from './platform' + const getLastPage = () => { // getCurrentPages() 至少有1个元素,所以不再额外判断 // const lastPage = getCurrentPages().at(-1) @@ -116,3 +118,32 @@ export const getNeedLoginPages = (): string[] => getAllPages('needLogin').map((p * 只得到 path 数组 */ export const needLoginPages: string[] = getAllPages('needLogin').map((page) => page.path) + +/** + * 根据微信小程序当前环境,判断应该获取的BaseUrl + */ +export const getEvnBaseUrl = () => { + // 请求基准地址 + let baseUrl = import.meta.env.VITE_SERVER_BASEURL + + // 小程序端环境区分 + if (isMp) { + const { + miniProgram: { envVersion }, + } = uni.getAccountInfoSync() + + switch (envVersion) { + case 'develop': + baseUrl = 'https://dev.test.net' + break + case 'trial': + baseUrl = 'https://trial.test.net' + break + case 'release': + baseUrl = 'https://prod.test.net' + break + } + } + + return baseUrl +}