From 1fbd4e3d61476c590121424b5ce4ad38aaf278e9 Mon Sep 17 00:00:00 2001 From: "chen.home" <1147347984@qq.com> Date: Tue, 17 Jan 2023 17:17:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor(project):=20=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E9=83=A8=E5=88=86=E9=85=8D=E7=BD=AE=E8=87=B3config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 3 --- .eslintrc.js | 2 ++ .npmrc | 4 ++++ src/components/custom/EIcon.vue | 29 ++++++++++++++++------------- src/config/index.ts | 1 + src/config/system.ts | 20 ++++++++++++++++++++ src/enum/common.ts | 12 ------------ src/plugins/assets.ts | 4 +--- src/typings/env.d.ts | 4 ---- src/utils/auth.ts | 20 ++++++++++---------- src/utils/crypto.ts | 7 ++++--- src/utils/storage.ts | 19 +++++++++---------- vite.config.ts | 1 + 13 files changed, 68 insertions(+), 58 deletions(-) create mode 100644 .npmrc create mode 100644 src/config/system.ts diff --git a/.env b/.env index e1f55a89..980849da 100644 --- a/.env +++ b/.env @@ -9,6 +9,3 @@ VITE_APP_DESC=EnchAdmin是一个中后台管理系统模版 VITE_HASH_ROUTE = Y # 权限路由模式: static | dynamic VITE_AUTH_ROUTE_MODE=dynamic -# 存储前缀 -VITE_STORAGE_PREFIX = "" -VITE_STORAGE_ENCRYPT_SECRET = '__CryptoJS_Secret__' \ No newline at end of file diff --git a/.eslintrc.js b/.eslintrc.js index aa6d58c8..a088add3 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -34,6 +34,7 @@ module.exports = { }, rules: { 'no-undef': 'off' + } }, { @@ -52,6 +53,7 @@ module.exports = { 'vue/multi-word-component-names': 0, // 关闭文件名多单词 // 'import/no-unresolved': ['error', { ignore: ['~icons/*'] }], "@typescript-eslint/no-explicit-any": ["off"], // 允许使用any + "@typescript-eslint/no-empty-function": 'off', // 允许空函数 '@typescript-eslint/no-empty-interface': [ 'error', { diff --git a/.npmrc b/.npmrc new file mode 100644 index 00000000..f3ce588a --- /dev/null +++ b/.npmrc @@ -0,0 +1,4 @@ +registry=https://registry.npmmirror.com/ +shamefully-hoist=true +strict-peer-dependencies=false +auto-install-peers=true diff --git a/src/components/custom/EIcon.vue b/src/components/custom/EIcon.vue index 119bf002..ff7bbb93 100644 --- a/src/components/custom/EIcon.vue +++ b/src/components/custom/EIcon.vue @@ -1,23 +1,26 @@ diff --git a/src/config/index.ts b/src/config/index.ts index 7e08f9b8..780e1470 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,3 +1,4 @@ export * from './sdk'; export * from './service'; export * from './env'; +export * from './system'; diff --git a/src/config/system.ts b/src/config/system.ts new file mode 100644 index 00000000..3666b4d5 --- /dev/null +++ b/src/config/system.ts @@ -0,0 +1,20 @@ +/** 本地存储信息字段 */ +export const storageKey = { + /* 用户信息 */ + userInfo: '__USER_INFO__', + /* token */ + token: '__TOKEN__', + /* refreshToken */ + refreshToken: '__REFRESH_TOKEN__', + /* 标签栏信息 */ + tabsRoutes: '__TABS_ROUTES__', +}; + +/** 本地存储前缀 */ +export const STORAGE_PREFIX = ''; + +/** 本地存储加密密钥 */ +export const STORAGE_ENCRYPT_SECRET = '__CryptoJS_Secret__'; + +/** 本地存储缓存时长 */ +export const STORAGE_DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7; diff --git a/src/enum/common.ts b/src/enum/common.ts index f11b548a..9afa0641 100644 --- a/src/enum/common.ts +++ b/src/enum/common.ts @@ -1,15 +1,3 @@ -/* 缓存的Key值 */ -export enum EnumStorageKey { - /* 用户信息 */ - userInfo = '__USER_INFO__', - /* token */ - token = '__TOKEN__', - /* refreshToken */ - refreshToken = '__REFRESH_TOKEN__', - /* 标签栏信息 */ - tabsRoutes = '__TABS_ROUTES__', -} - export enum EnumContentType { json = 'application/json', formUrlencoded = 'application/x-www-form-urlencoded', diff --git a/src/plugins/assets.ts b/src/plugins/assets.ts index 0058504c..2922bb4e 100644 --- a/src/plugins/assets.ts +++ b/src/plugins/assets.ts @@ -3,6 +3,4 @@ import 'uno.css'; import '@/styles/css/index.css'; import 'virtual:svg-icons-register'; -export default function setupAssets() { - // -} +export default function setupAssets() {} diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index a2f371ef..68daad73 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -36,12 +36,8 @@ interface ImportMetaEnv { readonly VITE_HASH_ROUTE?: 'Y' | 'N'; /** 路由加载模式 */ readonly VITE_AUTH_ROUTE_MODE?: 'static' | 'dynamic'; - /** 本地存储前缀 */ - readonly VITE_STORAGE_PREFIX?: string; /** 本地存储内容开启加密 */ readonly VITE_STORAGE_ENCRYPT?: 'Y' | 'N'; - /** 本地存储加密密钥 */ - readonly VITE_STORAGE_ENCRYPT_SECRET: string; /** 后端服务的环境类型 */ readonly MODE?: ServiceEnvType; diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 35e1b641..226851be 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -1,44 +1,44 @@ import { local } from './storage'; -import { EnumStorageKey } from '@/enum'; +import { storageKey } from '@/config'; const DURATION = 6 * 60 * 60; /* 获取当前token */ export function getToken() { - return local.get(EnumStorageKey.token); + return local.get(storageKey.token); } /* 设置token */ export function setToken(data: string) { - local.set(EnumStorageKey.token, data, DURATION); + local.set(storageKey.token, data, DURATION); } /* 移除token */ export function removeToken() { - local.remove(EnumStorageKey.token); + local.remove(storageKey.token); } /* 获取当前refreshToken */ export function getRefreshToken() { - return local.get(EnumStorageKey.refreshToken); + return local.get(storageKey.refreshToken); } /* 设置refreshToken */ export function setRefreshToken(data: string) { - local.set(EnumStorageKey.refreshToken, data, DURATION); + local.set(storageKey.refreshToken, data, DURATION); } /* 移除refreshToken */ export function removeRefreshToken() { - local.remove(EnumStorageKey.refreshToken); + local.remove(storageKey.refreshToken); } /* 获取用户详情 */ export function getUserInfo() { - return local.get(EnumStorageKey.userInfo); + return local.get(storageKey.userInfo); } /* 设置用户详情 */ export function setUserInfo(data: any) { - local.set(EnumStorageKey.userInfo, data); + local.set(storageKey.userInfo, data); } /* 移除用户详情 */ export function removeUserInfo() { - local.remove(EnumStorageKey.userInfo); + local.remove(storageKey.userInfo); } /** 去除用户相关缓存 */ diff --git a/src/utils/crypto.ts b/src/utils/crypto.ts index 826daddd..972e9765 100644 --- a/src/utils/crypto.ts +++ b/src/utils/crypto.ts @@ -1,7 +1,8 @@ import CryptoJS from 'crypto-js'; import { isObject } from './is'; -const { VITE_STORAGE_ENCRYPT, VITE_STORAGE_ENCRYPT_SECRET } = import.meta.env; +const { VITE_STORAGE_ENCRYPT } = import.meta.env; +import { STORAGE_ENCRYPT_SECRET } from '@/config'; /** * 加密数据 @@ -18,7 +19,7 @@ export function encrypto(data: any) { return newData; } - return CryptoJS.AES.encrypt(newData, VITE_STORAGE_ENCRYPT_SECRET).toString(); + return CryptoJS.AES.encrypt(newData, STORAGE_ENCRYPT_SECRET).toString(); } /** @@ -30,7 +31,7 @@ export function decrypto(cipherText: string) { return JSON.parse(cipherText); } - const bytes = CryptoJS.AES.decrypt(cipherText, VITE_STORAGE_ENCRYPT_SECRET); + const bytes = CryptoJS.AES.decrypt(cipherText, STORAGE_ENCRYPT_SECRET); const originalText = bytes.toString(CryptoJS.enc.Utf8); if (originalText) { diff --git a/src/utils/storage.ts b/src/utils/storage.ts index b91e9534..4ddd3027 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -1,6 +1,6 @@ import { encrypto, decrypto } from './crypto'; // 读取缓存前缀 -const prefix = import.meta.env.VITE_STORAGE_PREFIX as string; +import { STORAGE_PREFIX, STORAGE_DEFAULT_CACHE_TIME } from '@/config'; interface StorageData { value: any; @@ -11,19 +11,18 @@ interface StorageData { */ function createLocalStorage() { // 默认缓存期限为7天 - const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7; - function set(key: string, value: any, expire: number = DEFAULT_CACHE_TIME) { + function set(key: string, value: any, expire: number = STORAGE_DEFAULT_CACHE_TIME) { const storageData: StorageData = { value, expire: new Date().getTime() + expire * 1000, }; const json = encrypto(storageData); - window.localStorage.setItem(prefix + key, json); + window.localStorage.setItem(STORAGE_PREFIX + key, json); } function get(key: string) { - const json = window.localStorage.getItem(prefix + key); + const json = window.localStorage.getItem(STORAGE_PREFIX + key); if (!json) return null; let storageData: StorageData | null = null; @@ -44,7 +43,7 @@ function createLocalStorage() { } function remove(key: string) { - window.localStorage.removeItem(prefix + key); + window.localStorage.removeItem(STORAGE_PREFIX + key); } function clear() { @@ -64,10 +63,10 @@ function createLocalStorage() { function createSessionStorage() { function set(key: string, value: any) { const json = encrypto(value); - window.sessionStorage.setItem(prefix + key, json); + window.sessionStorage.setItem(STORAGE_PREFIX + key, json); } - function get(key: string) { - const json = sessionStorage.getItem(prefix + key); + function get(key: string) { + const json = sessionStorage.getItem(STORAGE_PREFIX + key); if (!json) return null; let storageData; @@ -83,7 +82,7 @@ function createSessionStorage() { return null; } function remove(key: string) { - window.sessionStorage.removeItem(prefix + key); + window.sessionStorage.removeItem(STORAGE_PREFIX + key); } function clear() { window.sessionStorage.clear(); diff --git a/vite.config.ts b/vite.config.ts index 0072f0e7..a7e7e132 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -39,6 +39,7 @@ export default defineConfig(({ command, mode }: ConfigEnv) => { build: { reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告 sourcemap: false, // 构建后是否生成 source map 文件 + assetsInlineLimit: 4096, // 4kb内资源使用base64 }, optimizeDeps: { include: ['echarts', 'md-editor-v3', '@wangeditor/editor', '@wangeditor/editor-for-vue'],