feat: 兼容多端统一配置BASE_URL环境变量

This commit is contained in:
Huang 2022-09-19 16:14:03 +08:00
parent f7a5f350f7
commit 6ca18b4bc2
7 changed files with 48 additions and 22 deletions

View File

@ -3,15 +3,8 @@ VITE_PROD = false
VITE_DEV = true VITE_DEV = true
# BASE_URL # BASE_URL
VITE_BASE_URL = /api VITE_BASE_URL = https://api-catch.ranesuangyu.top
# PROXY_BASE_URL
# 仅H5跨域需使用proxy,其他端请直接设置 VITE_BASE_URL 即可
VITE_PROXY_BASE_URL = https://api-catch.ranesuangyu.top
# 上传域名 # 上传域名
VITE_UPLOAD_URL = /upload VITE_UPLOAD_URL = /upload
# 代理上传域名
VITE_PROXY_UPLOAD_URL = 'YOUR UPLOAD URL'

1
src/env.d.ts vendored
View File

@ -11,7 +11,6 @@ interface ImportMetaEnv {
readonly VITE_ENV: string; readonly VITE_ENV: string;
readonly VITE_APP_TITLE: string; readonly VITE_APP_TITLE: string;
readonly VITE_BASE_URL: string; readonly VITE_BASE_URL: string;
readonly VITE_PROXY_BASE_URL: string;
readonly VITE_UPLOAD_URL: string; readonly VITE_UPLOAD_URL: string;
readonly VITE_PROD: boolean; readonly VITE_PROD: boolean;
readonly VITE_DEV: boolean; readonly VITE_DEV: boolean;

View File

@ -90,6 +90,17 @@
"devServer" : { "devServer" : {
"https" : false "https" : false
}, },
"title" : "uniapp_vue3_vite_ts" "title" : "uniapp_vue3_vite_ts",
"unipush" : {
"enable" : false
},
"sdkConfigs" : {
"maps" : {}
},
"optimization" : {
"treeShaking" : {
"enable" : true
}
}
} }
} }

View File

@ -45,7 +45,6 @@ export const useAuthStore = defineStore({
* @description * @description
*/ */
async loginOut(): Promise<any> { async loginOut(): Promise<any> {
console.log('~~loginOut请求');
try { try {
const res = await logout(); const res = await logout();
removeCache(TOKEN_KEY); removeCache(TOKEN_KEY);

View File

@ -1,6 +1,10 @@
import pkg from '../../package.json'; import pkg from '../../package.json';
import { judgePlatform } from '@/utils/platform';
import { PLATFORMS } from '@/enums/platformEnum';
// Generate cache key according to version /**
* @description: Generate cache key according to version
*/
export function getPkgVersion() { export function getPkgVersion() {
return `${`__${pkg.version}`}__`.toUpperCase(); return `${`__${pkg.version}`}__`.toUpperCase();
} }
@ -51,3 +55,23 @@ export function isDevMode(): boolean {
export function isProdMode(): boolean { export function isProdMode(): boolean {
return getEnvValue<boolean>('VITE_PROD'); return getEnvValue<boolean>('VITE_PROD');
} }
/**
* @description: Get environment VITE_BASE_URL value
* @returns:
* @example:
*/
export function getBaseUrl(): string {
if (judgePlatform(PLATFORMS.H5) && isDevMode()) return '/api';
return getEnvValue<string>('VITE_BASE_URL');
}
/**
* @description: Get environment VITE_UPLOAD_URL value
* @returns:
* @example:
*/
export function getUploadUrl(): string {
if (judgePlatform(PLATFORMS.H5) && isDevMode()) return '/upload';
return getEnvValue<string>('VITE_UPLOAD_URL');
}

View File

@ -1,11 +1,11 @@
import Request from 'luch-request'; import Request from 'luch-request';
import { assign } from 'lodash-es'; import { assign } from 'lodash-es';
import { Toast } from '@/utils/uniapi/prompt'; import { Toast } from '@/utils/uniapi/prompt';
import { getEnvValue } from '@/utils/env'; import { getBaseUrl } from '@/utils/env';
import { useAuthStore } from '@/state/modules/auth'; import { useAuthStore } from '@/state/modules/auth';
import { ResultEnum } from '@/enums/httpEnum'; import { ResultEnum } from '@/enums/httpEnum';
const BASE_URL = getEnvValue<string>('VITE_BASE_URL'); const BASE_URL = getBaseUrl();
const HEADER = { const HEADER = {
'Content-Type': 'application/json;charset=UTF-8;', 'Content-Type': 'application/json;charset=UTF-8;',
Accept: 'application/json, text/plain, */*', Accept: 'application/json, text/plain, */*',

View File

@ -31,12 +31,12 @@ export default ({ mode }: ConfigEnv): UserConfig => {
host: true, host: true,
proxy: { proxy: {
'/api': { '/api': {
target: env.VITE_PROXY_BASE_URL, target: env.VITE_BASE_URL,
changeOrigin: true, changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''), rewrite: (path) => path.replace(/^\/api/, ''),
}, },
'/upload': { '/upload': {
target: env.VITE_PROXY_UPLOAD_URL, target: env.VITE_BASE_URL,
changeOrigin: true, changeOrigin: true,
rewrite: (path) => path.replace(/^\/upload/, ''), rewrite: (path) => path.replace(/^\/upload/, ''),
}, },