Commit 967b0d25 authored by 何远江's avatar 何远江

axios 环境变量修改和添加

parent bdd1b15b
# spa-title # spa-title
VITE_GLOB_APP_TITLE = 宝绅 VITE_GLOB_APP_TITLE = 宝绅EP系统
# Whether to open mock
VITE_USE_MOCK = true
# public path # public path
VITE_PUBLIC_PATH = / VITE_PUBLIC_PATH = /
# Basic interface address SPA # Basic interface address SPA
VITE_GLOB_API_URL=/api VITE_GLOB_API_URL=
# File upload address, optional
VITE_GLOB_UPLOAD_URL=/upload
# Interface prefix # Interface prefix
VITE_GLOB_API_URL_PREFIX= VITE_GLOB_API_URL_PREFIX=/api
# public path
VITE_PUBLIC_PATH = /
# Basic interface address SPA
VITE_GLOB_API_URL=
# Interface prefix
VITE_GLOB_API_URL_PREFIX=/api
\ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<link rel="icon" href="/favicon.ico"> <link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title> <title>%VITE_GLOB_APP_TITLE%</title>
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
......
...@@ -3,16 +3,14 @@ import type { GlobConfig } from '#/config'; ...@@ -3,16 +3,14 @@ import type { GlobConfig } from '#/config';
import { getAppEnvConfig } from '@/utils/env'; import { getAppEnvConfig } from '@/utils/env';
export const useGlobSetting = (): Readonly<GlobConfig> => { export const useGlobSetting = (): Readonly<GlobConfig> => {
const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } = const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX } =
getAppEnvConfig(); getAppEnvConfig();
// Take global configuration // Take global configuration
const glob: Readonly<GlobConfig> = { const glob: Readonly<GlobConfig> = {
title: VITE_GLOB_APP_TITLE, title: VITE_GLOB_APP_TITLE,
apiUrl: VITE_GLOB_API_URL, apiUrl: VITE_GLOB_API_URL,
shortName: VITE_GLOB_APP_TITLE.replace(/\s/g, '_').replace(/-/g, '_'),
urlPrefix: VITE_GLOB_API_URL_PREFIX, urlPrefix: VITE_GLOB_API_URL_PREFIX,
uploadUrl: VITE_GLOB_UPLOAD_URL,
}; };
return glob as Readonly<GlobConfig>; return glob as Readonly<GlobConfig>;
}; };
import type { GlobEnvConfig } from '#/config'; import type { GlobEnvConfig } from '#/config'
const getVariableName = (title: string) => {
function strToHex(str: string) {
const result: string[] = [];
for (let i = 0; i < str.length; ++i) {
const hex = str.charCodeAt(i).toString(16);
result.push(('000' + hex).slice(-4));
}
return result.join('').toUpperCase();
}
return `__PRODUCTION__${strToHex(title) || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, '');
};
export function getAppEnvConfig() { export function getAppEnvConfig() {
const ENV_NAME = getVariableName(import.meta.env.VITE_GLOB_APP_TITLE); const ENV = import.meta.env as unknown as GlobEnvConfig
const ENV = import.meta.env.DEV const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX } = ENV
? // Get the global configuration (the configuration will be extracted independently when packaging)
(import.meta.env as unknown as GlobEnvConfig)
: (window[ENV_NAME] as unknown as GlobEnvConfig);
const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } =
ENV;
return {
VITE_GLOB_APP_TITLE,
VITE_GLOB_API_URL,
VITE_GLOB_API_URL_PREFIX,
VITE_GLOB_UPLOAD_URL,
};
}
return {
VITE_GLOB_APP_TITLE,
VITE_GLOB_API_URL,
VITE_GLOB_API_URL_PREFIX
}
}
/** /**
* @description: Development mode * @description: Development mode
*/ */
export const devMode = 'development'; export const devMode = 'development'
/** /**
* @description: Production mode * @description: Production mode
*/ */
export const prodMode = 'production'; export const prodMode = 'production'
/** /**
* @description: Get environment variables * @description: Get environment variables
...@@ -47,7 +27,7 @@ export const prodMode = 'production'; ...@@ -47,7 +27,7 @@ export const prodMode = 'production';
* @example: * @example:
*/ */
export function getEnv(): string { export function getEnv(): string {
return import.meta.env.MODE; return import.meta.env.MODE
} }
/** /**
...@@ -56,7 +36,7 @@ export function getEnv(): string { ...@@ -56,7 +36,7 @@ export function getEnv(): string {
* @example: * @example:
*/ */
export function isDevMode(): boolean { export function isDevMode(): boolean {
return import.meta.env.DEV; return import.meta.env.DEV
} }
/** /**
...@@ -65,5 +45,5 @@ export function isDevMode(): boolean { ...@@ -65,5 +45,5 @@ export function isDevMode(): boolean {
* @example: * @example:
*/ */
export function isProdMode(): boolean { export function isProdMode(): boolean {
return import.meta.env.PROD; return import.meta.env.PROD
} }
\ No newline at end of file
...@@ -136,7 +136,7 @@ export interface GlobConfig { ...@@ -136,7 +136,7 @@ export interface GlobConfig {
// Service interface url prefix // Service interface url prefix
urlPrefix?: string; urlPrefix?: string;
// Project abbreviation // Project abbreviation
shortName: string; shortName?: string;
} }
export interface GlobEnvConfig { export interface GlobEnvConfig {
// Site title // Site title
......
import vue from '@vitejs/plugin-vue' import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx' import vueJsx from '@vitejs/plugin-vue-jsx'
import { defineConfig } from 'vite' import { defineConfig } from 'vite'
import Components from 'unplugin-vue-components/vite'; import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'; import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import { createStyleImportPlugin, VxeTableResolve } from 'vite-plugin-style-import' import { createStyleImportPlugin, VxeTableResolve } from 'vite-plugin-style-import'
import path from 'node:path' import path from 'node:path'
...@@ -15,29 +15,29 @@ export default defineConfig({ ...@@ -15,29 +15,29 @@ export default defineConfig({
Components({ Components({
resolvers: [ resolvers: [
AntDesignVueResolver({ AntDesignVueResolver({
importStyle: false, // css in js importStyle: false // css in js
}), })
], ]
}), }),
createStyleImportPlugin({ createStyleImportPlugin({
resolves: [VxeTableResolve()], resolves: [VxeTableResolve()]
}) })
], ],
server: { server: {
port: 3001, port: 8089,
host: '0.0.0.0', host: '0.0.0.0',
proxy: { proxy: {
'/api': { '/api': {
target: 'http://192.168.66.125:8070', target: 'http://192.168.66.135:8070',
changeOrigin: true, changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '') rewrite: (path) => path.replace(/^\/api/, '')
} }
} }
}, },
resolve: { resolve: {
alias: { alias: {
'@': path.join(__dirname, 'src'), '@': path.join(__dirname, 'src'),
'#': path.join(__dirname, 'types'), '#': path.join(__dirname, 'types')
} }
} }
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment