From fa0537916280797a56cbff73db03c9ddd19f2865 Mon Sep 17 00:00:00 2001 From: ray <1490493387@qq.com> Date: Sat, 5 Oct 2024 23:45:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20:recycle:=20=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E5=92=8C=E7=BF=BB=E8=AF=91=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/api/dict-data.ts | 5 ++ src/components/Dictionary/DictLabel.vue | 52 ++++++++++++++++ src/types/components.d.ts | 1 + src/types/global.d.ts | 2 +- src/utils/cache.ts | 79 +++++++++++++++++++++++++ src/views/system/dict/data.vue | 20 ++++++- src/views/system/notice/index.vue | 22 ++----- src/views/system/notice/my-notice.vue | 16 ++--- 9 files changed, 167 insertions(+), 32 deletions(-) create mode 100644 src/components/Dictionary/DictLabel.vue create mode 100644 src/utils/cache.ts diff --git a/package.json b/package.json index 686fa115..95752fdb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue3-element-admin", - "version": "2.15.0", + "version": "2.16.0", "private": true, "type": "module", "scripts": { diff --git a/src/api/dict-data.ts b/src/api/dict-data.ts index 79a2e90b..0055b4c6 100644 --- a/src/api/dict-data.ts +++ b/src/api/dict-data.ts @@ -154,4 +154,9 @@ export interface DictDataForm { * 字典排序 */ sort?: number; + + /** + * 标签类型 + */ + tagType: "success" | "warning" | "info" | "primary" | "danger" | undefined; } diff --git a/src/components/Dictionary/DictLabel.vue b/src/components/Dictionary/DictLabel.vue new file mode 100644 index 00000000..000c2212 --- /dev/null +++ b/src/components/Dictionary/DictLabel.vue @@ -0,0 +1,52 @@ + + + diff --git a/src/types/components.d.ts b/src/types/components.d.ts index fe6a01e9..4313ff0e 100644 --- a/src/types/components.d.ts +++ b/src/types/components.d.ts @@ -14,6 +14,7 @@ declare module "vue" { CopyButton: (typeof import("./../components/CopyButton/index.vue"))["default"]; CURD: (typeof import("./../components/CURD/index.vue"))["default"]; Dictionary: (typeof import("./../components/Dictionary/index.vue"))["default"]; + DictLabel: (typeof import("./../components/Dictionary/DictLabel.vue"))["default"]; ElBacktop: (typeof import("element-plus/es"))["ElBacktop"]; ElBreadcrumb: (typeof import("element-plus/es"))["ElBreadcrumb"]; ElBreadcrumbItem: (typeof import("element-plus/es"))["ElBreadcrumbItem"]; diff --git a/src/types/global.d.ts b/src/types/global.d.ts index 146d5bfc..75a6cca2 100644 --- a/src/types/global.d.ts +++ b/src/types/global.d.ts @@ -81,7 +81,7 @@ declare global { } /** - * 组件数据源 + * 下拉选项数据类型 */ interface OptionType { /** 值 */ diff --git a/src/utils/cache.ts b/src/utils/cache.ts new file mode 100644 index 00000000..2a3b8c53 --- /dev/null +++ b/src/utils/cache.ts @@ -0,0 +1,79 @@ +const DEFAULT_CACHE_EXPIRY_TIME = 5 * 60 * 1000; // 默认缓存有效期为5分钟 + +/** + * 通用缓存工具类 + */ +class Cache { + private cachePrefix: string; + + constructor(prefix: string = "cache_") { + this.cachePrefix = prefix; + } + + /** + * 设置缓存 + * + * @param key 缓存的键 + * @param data 缓存的数据 + * @param expiryTime 缓存有效期(毫秒),默认5分钟 + */ + setCache( + key: string, + data: any, + expiryTime: number = DEFAULT_CACHE_EXPIRY_TIME + ) { + const expiryTimestamp = new Date().getTime() + expiryTime; + const cacheKey = this.cachePrefix + key; + const cacheData = { data, expiryTimestamp }; + + localStorage.setItem(cacheKey, JSON.stringify(cacheData)); + } + + /** + * 获取缓存 + * + * @param key 缓存的键 + * @returns 如果缓存有效则返回缓存的数据,否则返回 null + */ + getCache(key: string) { + const cacheKey = this.cachePrefix + key; + const cached = localStorage.getItem(cacheKey); + + if (cached) { + const { data, expiryTimestamp } = JSON.parse(cached); + const now = new Date().getTime(); + + // 如果缓存未过期,返回数据 + if (now < expiryTimestamp) { + return data; + } else { + // 如果缓存过期,移除缓存 + localStorage.removeItem(cacheKey); + } + } + return null; + } + + /** + * 移除缓存 + * + * @param key 缓存的键 + */ + removeCache(key: string) { + const cacheKey = this.cachePrefix + key; + localStorage.removeItem(cacheKey); + } + + /** + * 清空当前前缀下的所有缓存 + */ + clearCache() { + for (const key in localStorage) { + if (key.startsWith(this.cachePrefix)) { + localStorage.removeItem(key); + } + } + } +} + +export default Cache; diff --git a/src/views/system/dict/data.vue b/src/views/system/dict/data.vue index 50e0b3a5..3f6e31e3 100644 --- a/src/views/system/dict/data.vue +++ b/src/views/system/dict/data.vue @@ -102,7 +102,7 @@ 禁用 - + + + {{ formData.label }} + + + success + warning + info + primary + danger + 清空 + + diff --git a/src/views/system/notice/index.vue b/src/views/system/notice/index.vue index 0a369583..8e36e862 100644 --- a/src/views/system/notice/index.vue +++ b/src/views/system/notice/index.vue @@ -77,15 +77,9 @@ prop="title" min-width="150" /> - + - + - + + +