diff --git a/.env b/.env index 326d02b0..e79db2f2 100644 --- a/.env +++ b/.env @@ -5,3 +5,5 @@ VITE_APP_NAME=SoybeanAdmin VITE_APP_TITLE=Soybean管理系统 VITE_APP_DESC=SoybeanAdmin是一个中后台管理系统模版 + +VITE_HTTP_PROXY=true diff --git a/.env-config.ts b/.env-config.ts index d61fb158..b7af6e35 100644 --- a/.env-config.ts +++ b/.env-config.ts @@ -1,22 +1,38 @@ /** 请求环境配置 */ type ServiceEnv = Record< - Service.HttpEnv, + EnvType, { - /** 请求环境 */ - env: Service.HttpEnv; /** 请求地址 */ url: string; + /** 代理地址 */ + proxy: string; } >; /** 请求的环境 */ -export const serviceEnv: ServiceEnv = { +const serviceEnvConfig: ServiceEnv = { + dev: { + url: 'http://localhost:8080', + proxy: '/api', + }, test: { - env: 'test', - url: 'http://www.baidu.com', + url: 'http://localhost:8080', + proxy: '/api', }, prod: { - env: 'prod', - url: 'http://www.baidu.com', + url: 'http://localhost:8080', + proxy: '/api', }, }; + +/** + * 获取环境配置 + * @param env 环境描述 + */ +export function getEnvConfig(env: ImportMetaEnv) { + const { VITE_ENV_TYPE = 'dev' } = env; + const envConfig = { + http: serviceEnvConfig[VITE_ENV_TYPE], + }; + return envConfig; +} diff --git a/package.json b/package.json index 713daeda..14155b85 100644 --- a/package.json +++ b/package.json @@ -2,10 +2,12 @@ "name": "soybean-admin", "version": "0.9.2", "scripts": { - "dev": "cross-env VITE_HTTP_ENV=test vite", - "dev:prod": "cross-env VITE_HTTP_ENV=prod vite", - "build": "npm run typecheck && cross-env VITE_HTTP_ENV=prod vite build", - "build:test": "npm run typecheck && cross-env VITE_HTTP_ENV=test vite build", + "dev": "cross-env VITE_ENV_TYPE=dev vite", + "dev:test": "cross-env VITE_ENV_TYPE=test vite", + "dev:prod": "cross-env VITE_ENV_TYPE=prod vite", + "build": "npm run typecheck && cross-env VITE_ENV_TYPE=prod vite build", + "build:dev": "npm run typecheck && cross-env VITE_ENV_TYPE=dev vite build", + "build:test": "npm run typecheck && cross-env VITE_ENV_TYPE=test vite build", "build:vercel": "cross-env VITE_HASH_ROUTE=true vite build", "preview": "vite preview --port 5050", "typecheck": "vue-tsc --noEmit", diff --git a/src/composables/common/index.ts b/src/composables/common/index.ts deleted file mode 100644 index 2d2a11c3..00000000 --- a/src/composables/common/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './system'; -export * from './router'; -export * from './layout'; diff --git a/src/composables/events/index.ts b/src/composables/events/index.ts deleted file mode 100644 index cb0ff5c3..00000000 --- a/src/composables/events/index.ts +++ /dev/null @@ -1 +0,0 @@ -export {}; diff --git a/src/composables/index.ts b/src/composables/index.ts index d0b93236..2d2a11c3 100644 --- a/src/composables/index.ts +++ b/src/composables/index.ts @@ -1 +1,3 @@ -export * from './common'; +export * from './system'; +export * from './router'; +export * from './layout'; diff --git a/src/composables/common/layout.ts b/src/composables/layout.ts similarity index 100% rename from src/composables/common/layout.ts rename to src/composables/layout.ts diff --git a/src/composables/common/router.ts b/src/composables/router.ts similarity index 100% rename from src/composables/common/router.ts rename to src/composables/router.ts diff --git a/src/composables/common/system.ts b/src/composables/system.ts similarity index 100% rename from src/composables/common/system.ts rename to src/composables/system.ts diff --git a/src/config/business/index.ts b/src/config/business/index.ts deleted file mode 100644 index cb0ff5c3..00000000 --- a/src/config/business/index.ts +++ /dev/null @@ -1 +0,0 @@ -export {}; diff --git a/src/config/common/index.ts b/src/config/common/index.ts deleted file mode 100644 index 847c47e2..00000000 --- a/src/config/common/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from './service'; -export * from './regexp'; -export * from './map-sdk'; diff --git a/src/config/index.ts b/src/config/index.ts index d0b93236..847c47e2 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1 +1,3 @@ -export * from './common'; +export * from './service'; +export * from './regexp'; +export * from './map-sdk'; diff --git a/src/config/common/map-sdk.ts b/src/config/map-sdk.ts similarity index 100% rename from src/config/common/map-sdk.ts rename to src/config/map-sdk.ts diff --git a/src/config/common/regexp.ts b/src/config/regexp.ts similarity index 100% rename from src/config/common/regexp.ts rename to src/config/regexp.ts diff --git a/src/config/common/service.ts b/src/config/service.ts similarity index 100% rename from src/config/common/service.ts rename to src/config/service.ts diff --git a/src/service/api/demo.ts b/src/service/api/demo.ts new file mode 100644 index 00000000..e6c216b5 --- /dev/null +++ b/src/service/api/demo.ts @@ -0,0 +1,6 @@ +import { request } from '../request'; + +/** 测试请求代理 */ +export function fetchTestProxy() { + return request.get('/test'); +} diff --git a/src/service/api/index.ts b/src/service/api/index.ts index 269586ee..a2c76b1c 100644 --- a/src/service/api/index.ts +++ b/src/service/api/index.ts @@ -1 +1,2 @@ export * from './auth'; +export * from './demo'; diff --git a/src/service/request/helpers.ts b/src/service/request/helpers.ts index f6499b65..d430c222 100644 --- a/src/service/request/helpers.ts +++ b/src/service/request/helpers.ts @@ -7,10 +7,10 @@ import { fetchUpdateToken } from '../api'; * 刷新token * @param axiosConfig - token失效时的请求配置 */ -export async function refreshToken(axiosConfig: AxiosRequestConfig) { +export async function handleRefreshToken(axiosConfig: AxiosRequestConfig) { const { resetAuthStore } = useAuthStore(); - const rToken = getRefreshToken(); - const { data } = await fetchUpdateToken(rToken); + const refreshToken = getRefreshToken(); + const { data } = await fetchUpdateToken(refreshToken); if (data) { setToken(data.token); setRefreshToken(data.refreshToken); diff --git a/src/service/request/index.ts b/src/service/request/index.ts index fb352c21..f1711186 100644 --- a/src/service/request/index.ts +++ b/src/service/request/index.ts @@ -1,10 +1,9 @@ import { createRequest } from './request'; -import { serviceEnv } from '~/.env-config'; +import { getEnvConfig } from '~/.env-config'; -const { VITE_HTTP_ENV = 'test' } = import.meta.env; +const { http } = getEnvConfig(import.meta.env); +const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'true'; -const { url } = serviceEnv[VITE_HTTP_ENV]; - -export const request = createRequest({ baseURL: url }); +export const request = createRequest({ baseURL: isHttpProxy ? http.proxy : http.url }); export const mockRequest = createRequest({ baseURL: '/mock' }); diff --git a/src/service/request/instance.ts b/src/service/request/instance.ts index 21913a22..d75d8856 100644 --- a/src/service/request/instance.ts +++ b/src/service/request/instance.ts @@ -9,7 +9,7 @@ import { handleBackendError, handleServiceResult, } from '@/utils'; -import { refreshToken } from './helpers'; +import { handleRefreshToken } from './helpers'; /** * 封装axios请求类 @@ -71,7 +71,7 @@ export default class CustomAxiosInstance { // token失效, 刷新token if (REFRESH_TOKEN_CODE.includes(backend[codeKey])) { - const config = await refreshToken(response.config); + const config = await handleRefreshToken(response.config); if (config) { return this.instance.request(config); } diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index f736c8e2..bde3033f 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -7,6 +7,9 @@ declare module '*.vue' { export default component; } +/** env环境类型 */ +type EnvType = 'dev' | 'test' | 'prod'; + interface ImportMetaEnv { /** 项目基本地址 */ readonly VITE_BASE_URL: string; @@ -16,10 +19,12 @@ interface ImportMetaEnv { readonly VITE_APP_TITLE: string; /** 项目描述 */ readonly VITE_APP_DESC: string; + /** vite环境类型 */ + readonly VITE_ENV_TYPE?: EnvType; + /** 开启请求代理 */ + readonly VITE_HTTP_PROXY?: 'true' | 'false'; /** 是否开启打包文件大小结果分析 */ - readonly VITE_VISUALIZER: 'true' | 'false'; - /** 网路请求环境类型 */ - readonly VITE_HTTP_ENV?: Service.HttpEnv; + readonly VITE_VISUALIZER?: 'true' | 'false'; /** hash路由模式 */ readonly VITE_HASH_ROUTE?: 'true' | 'false'; } diff --git a/tsconfig.json b/tsconfig.json index 2c7a6131..2f6a47c8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "src/typings/**/*.d.ts", "src/**/*", "src/**/*.vue", + "vite.config.*", "mock/**/*.ts", "build/**/*.ts", ".env-config.ts", @@ -18,11 +19,6 @@ "@/*": ["./src/*"], "~/*": ["./*"] }, - "types": ["naive-ui/volar"] - }, - "references": [ - { - "path": "./tsconfig.vite-config.json" - } - ] + "types": ["node","naive-ui/volar"] + } } diff --git a/tsconfig.vite-config.json b/tsconfig.vite-config.json deleted file mode 100644 index 93fe5846..00000000 --- a/tsconfig.vite-config.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "extends": "@vue/tsconfig/tsconfig.node.json", - "include": ["vite.config.*"], - "compilerOptions": { - "composite": true, - "types": ["node"] - } -} diff --git a/vite.config.ts b/vite.config.ts index c9dda609..4e547df4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,6 +1,7 @@ import { fileURLToPath } from 'url'; import { defineConfig, loadEnv } from 'vite'; import { setupVitePlugins, define } from './build'; +import { getEnvConfig } from './.env-config'; export default defineConfig((configEnv) => { const viteEnv = loadEnv(configEnv.mode, `.env.${configEnv.mode}`) as ImportMetaEnv; @@ -8,6 +9,8 @@ export default defineConfig((configEnv) => { const srcPath = fileURLToPath(new URL('./src', import.meta.url)); const rootPath = fileURLToPath(new URL('./', import.meta.url)); + const { http } = getEnvConfig(viteEnv); + return { base: viteEnv.VITE_BASE_URL, resolve: { @@ -32,6 +35,13 @@ export default defineConfig((configEnv) => { host: '0.0.0.0', port: 3200, open: true, + proxy: { + [http.proxy]: { + target: http.url, + changeOrigin: true, + rewrite: (path) => path.replace(new RegExp(`^${http.proxy}`), ''), + }, + }, }, build: { brotliSize: false,