From 61b15833b74bd214fbf7df86b6481df996becd22 Mon Sep 17 00:00:00 2001 From: dL-hx <897057246@qq.com> Date: Mon, 15 Jan 2024 00:56:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=204-13=20=E8=BF=90=E8=A1=8C=E6=97=B6?= =?UTF-8?q?=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 2 +- src/config/index.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++ src/utils/request.ts | 22 +++++++++++++++----- 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 src/config/index.ts diff --git a/index.html b/index.html index 2cb953a..ed0eca3 100644 --- a/index.html +++ b/index.html @@ -1,5 +1,5 @@ - + diff --git a/src/config/index.ts b/src/config/index.ts new file mode 100644 index 0000000..317f151 --- /dev/null +++ b/src/config/index.ts @@ -0,0 +1,48 @@ +/** + * 运行时:判断环境变量 + * 环境配置封装 + */ + +type ENV = 'dev' | 'stg' | 'prd' + +// let env: ENV = 'dev' +// if (location.host.indexOf('localhost') > -1) { +// env = 'dev' +// } else if (location.host === 'driver-stg.marsview.cc') { +// env = 'stg' +// } else { +// env = 'prd' +// } + +// 运行时候环境获取, 在html元素根元素上注入该元素, 通过下面方式获取当前环境 +// +const env = (document.documentElement.dataset.env as ENV) || 'stg' + +const config = { + dev: { + baseApi: '/api', + uploadApi: 'http://api-driver-dev.marsview.cc', + cdn: 'http://xxx.aliyun.com', + mock: false, + mockApi: 'https://www.fastmock.site/mock/5841b82d5672783b6fd62bb2a06aeb1f/api' + }, + stg: { + baseApi: '/api', + uploadApi: 'http://api-driver-stg.marsview.cc', + cdn: 'http://xxx.aliyun.com', + mock: false, + mockApi: 'https://www.fastmock.site/mock/5841b82d5672783b6fd62bb2a06aeb1f/api' + }, + prd: { + baseApi: '/api', + uploadApi: 'http://api-driver.marsview.cc', + cdn: 'http://xxx.aliyun.com', + mock: false, + mockApi: 'https://www.fastmock.site/mock/5841b82d5672783b6fd62bb2a06aeb1f/api' + } +} + +export default { + env, + ...config['prd'] +} diff --git a/src/utils/request.ts b/src/utils/request.ts index c6440b3..d9f64cf 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -2,11 +2,13 @@ import { message } from 'antd' import axios, { AxiosError } from 'axios' import { hideLoading, showLoading } from './loading/index.' import storage from './storage' +import env from '@/config' // 创建实例对象 const instance = axios.create({ // baseURL: '/api', - baseURL: import.meta.env.VITE_BASE_API, + // baseURL: import.meta.env.VITE_BASE_API, + baseURL: env.baseApi, timeout: 8000, timeoutErrorMessage: '请求超时, 请稍后再试', withCredentials: true, @@ -23,11 +25,21 @@ instance.interceptors.request.use( // if (token) { // config.headers.Authorization = 'Bearer ' + token // } + + // 配置环境变量的两种方式 // 当MOCK 开关打开时, 使用MOCK地址。 否则使用默认地址 - if (import.meta.env.VITE_MOCK === 'true') { - config.baseURL = import.meta.env.VITE_MOCK_API - }else{ - config.baseURL = import.meta.env.VITE_BASE_API + // 编译时环境 + // if (import.meta.env.VITE_MOCK === 'true') { + // config.baseURL = import.meta.env.VITE_MOCK_API + // }else{ + // config.baseURL = import.meta.env.VITE_BASE_API + // } + + // 运行时环境 + if (env.mock) { + config.baseURL = env.mockApi + } else { + config.baseURL = env.baseApi } return { ...config