Skip to content

Commit

Permalink
refactor(project): 迁移部分配置至config
Browse files Browse the repository at this point in the history
  • Loading branch information
chansee97 committed Jan 17, 2023
1 parent 22c85f2 commit 1fbd4e3
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 58 deletions.
3 changes: 0 additions & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -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__'
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = {
},
rules: {
'no-undef': 'off'

}
},
{
Expand All @@ -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',
{
Expand Down
4 changes: 4 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
registry=https://registry.npmmirror.com/
shamefully-hoist=true
strict-peer-dependencies=false
auto-install-peers=true
29 changes: 16 additions & 13 deletions src/components/custom/EIcon.vue
Original file line number Diff line number Diff line change
@@ -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>
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './sdk';
export * from './service';
export * from './env';
export * from './system';
20 changes: 20 additions & 0 deletions src/config/system.ts
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 0 additions & 12 deletions src/enum/common.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
4 changes: 1 addition & 3 deletions src/plugins/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
4 changes: 0 additions & 4 deletions src/typings/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 10 additions & 10 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -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);
}

/** 去除用户相关缓存 */
Expand Down
7 changes: 4 additions & 3 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
* 加密数据
Expand All @@ -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();
}

/**
Expand All @@ -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) {
Expand Down
19 changes: 9 additions & 10 deletions src/utils/storage.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -44,7 +43,7 @@ function createLocalStorage() {
}

function remove(key: string) {
window.localStorage.removeItem(prefix + key);
window.localStorage.removeItem(STORAGE_PREFIX + key);
}

function clear() {
Expand All @@ -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;
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit 1fbd4e3

Please sign in to comment.