refactor(project): 迁移部分配置至config

This commit is contained in:
chen.home 2023-01-17 17:17:55 +08:00
parent 22c85f2848
commit 1fbd4e3d61
13 changed files with 68 additions and 58 deletions

3
.env
View File

@ -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__'

View File

@ -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',
{

4
.npmrc Normal file
View File

@ -0,0 +1,4 @@
registry=https://registry.npmmirror.com/
shamefully-hoist=true
strict-peer-dependencies=false
auto-install-peers=true

View File

@ -1,23 +1,26 @@
<template>
<n-icon :size="props.size" :depth="props.depth" :color="props.color">
<n-icon
:size="props.size"
:depth="props.depth"
:color="props.color"
>
<Icon :icon="props.icon" />
</n-icon>
</template>
<script setup lang="ts">
import { Icon } from '@iconify/vue';
const props = withDefaults(
defineProps<{
icon?: string;
color?: string;
size?: number;
depth?: 1 | 2 | 3 | 4 | 5;
}>(),
{
size: 18,
icon: 'icon-park-outline:baby-feet',
},
);
interface iconPorps {
icon?: string;
color?: string;
size?: number;
depth?: 1 | 2 | 3 | 4 | 5;
}
const props = withDefaults(defineProps<iconPorps>(), {
size: 18,
icon: 'icon-park-outline:baby-feet',
});
</script>
<style scoped></style>

View File

@ -1,3 +1,4 @@
export * from './sdk';
export * from './service';
export * from './env';
export * from './system';

20
src/config/system.ts Normal file
View File

@ -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;

View File

@ -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',

View File

@ -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() {}

View File

@ -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;

View File

@ -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);
}
/** 去除用户相关缓存 */

View File

@ -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) {

View File

@ -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<T>(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();

View File

@ -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'],