Sanakey
3 天以前 b5c1614fe473330ceca8b7cff0f1802e19bd5039
提交 | 用户 | age
4d2fb0 1 import type { GlobEnvConfig } from '#/config';
8a9ca4 2 import pkg from '../../package.json';
4d2fb0 3 import { API_ADDRESS } from '@/enums/cacheEnum';
8a9ca4 4
f6cef1 5 export function getCommonStoragePrefix() {
aedb8e 6   const { VITE_GLOB_APP_TITLE } = getAppEnvConfig();
V 7   return `${VITE_GLOB_APP_TITLE.replace(/\s/g, '_')}__${getEnv()}`.toUpperCase();
144ab5 8 }
bb3b8f 9
8a9ca4 10 // Generate cache key according to version
V 11 export function getStorageShortName() {
f6cef1 12   return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
V 13 }
14
c99ef6 15 const getVariableName = (title: string) => {
J 16   function strToHex(str: string) {
17     const result: string[] = [];
18     for (let i = 0; i < str.length; ++i) {
19       const hex = str.charCodeAt(i).toString(16);
20       result.push(('000' + hex).slice(-4));
21     }
22     return result.join('').toUpperCase();
23   }
24   return `__PRODUCTION__${strToHex(title) || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, '');
25 };
26
f6cef1 27 export function getAppEnvConfig() {
aedb8e 28   const ENV_NAME = getVariableName(import.meta.env.VITE_GLOB_APP_TITLE);
833b31 29   const ENV = import.meta.env.DEV
f6cef1 30     ? // Get the global configuration (the configuration will be extracted independently when packaging)
00fca0 31       (import.meta.env as unknown as GlobEnvConfig)
6fbc55 32     : (window[ENV_NAME] as unknown as GlobEnvConfig);
00bf5d 33   const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } = ENV;
L 34   let { VITE_GLOB_API_URL } = ENV;
35   if (localStorage.getItem(API_ADDRESS)) {
36     const address = JSON.parse(localStorage.getItem(API_ADDRESS) || '{}');
37     if (address?.key) VITE_GLOB_API_URL = address?.val;
38   }
b7ce74 39   return {
V 40     VITE_GLOB_APP_TITLE,
41     VITE_GLOB_API_URL,
42     VITE_GLOB_API_URL_PREFIX,
43     VITE_GLOB_UPLOAD_URL,
44   };
8a9ca4 45 }
V 46
2f6253 47 /**
e8eefd 48  * @description: Development mode
2f6253 49  */
50 export const devMode = 'development';
51
52 /**
236575 53  * @description: Production mode
2f6253 54  */
55 export const prodMode = 'production';
56
57 /**
236575 58  * @description: Get environment variables
2f6253 59  * @returns:
60  * @example:
61  */
144ab5 62 export function getEnv(): string {
V 63   return import.meta.env.MODE;
64 }
2f6253 65
66 /**
236575 67  * @description: Is it a development mode
2f6253 68  * @returns:
69  * @example:
70  */
144ab5 71 export function isDevMode(): boolean {
V 72   return import.meta.env.DEV;
73 }
2f6253 74
75 /**
236575 76  * @description: Is it a production mode
2f6253 77  * @returns:
78  * @example:
79  */
144ab5 80 export function isProdMode(): boolean {
V 81   return import.meta.env.PROD;
82 }