feat: 请求拦截器基准URL逻辑调整及小程序环境适配
调整请求拦截器中的baseUrl定义,以适配不同的运行环境,特别是小程序环境。
This commit is contained in:
parent
35ee452a3e
commit
560d4d05fe
@ -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<string, any>
|
||||
@ -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 = {
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user