From 4528d83a811727fd66e04d2548c1b1ae5bb037b5 Mon Sep 17 00:00:00 2001 From: Theo <971366405@qq.com> Date: Sat, 23 Nov 2024 00:28:42 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20:recycle:=20=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=88=B7=E6=96=B0=E9=98=B2=E6=8A=96=E5=92=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81patch=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 系统配置刷新防抖和支持patch请求 --- src/api/system/config.ts | 2 +- src/utils/index.ts | 29 +++++++++++++++++++++++++++++ src/views/system/config/index.vue | 10 ++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/api/system/config.ts b/src/api/system/config.ts index 95c1bb84..368cdd7d 100644 --- a/src/api/system/config.ts +++ b/src/api/system/config.ts @@ -62,7 +62,7 @@ const ConfigAPI = { refreshCache() { return request({ url: `${CONFIG_BASE_URL}/refresh`, - method: "PUT", + method: "PATCH", }); }, }; diff --git a/src/utils/index.ts b/src/utils/index.ts index 44bb588b..f8880d2b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -39,3 +39,32 @@ export function isExternal(path: string) { const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path); return isExternal; } + +/** + * 防抖函数 + * + * @param {Function} fn 需要防抖的函数 + * @param {number} delay 防抖时间 + * @returns {Function} + */ +export const debounce = any>( + fn: T, + delay: number, + immediate = false +) => { + let timer: NodeJS.Timeout | null = null; + return function (this: any, ...args: Parameters) { + if (timer) clearTimeout(timer); + + if (immediate && !timer) { + fn.apply(this, args); + } + + timer = setTimeout(() => { + if (!immediate) { + fn.apply(this, args); + } + timer = null; + }, delay); + }; +}; diff --git a/src/views/system/config/index.vue b/src/views/system/config/index.vue index 15bbb16e..8fb6e2cc 100644 --- a/src/views/system/config/index.vue +++ b/src/views/system/config/index.vue @@ -136,6 +136,7 @@ defineOptions({ }); import ConfigAPI, { ConfigPageVO, ConfigForm, ConfigPageQuery } from "@/api/system/config"; +import { debounce } from "@/utils"; const queryFormRef = ref(ElForm); const dataFormRef = ref(ElForm); @@ -211,11 +212,16 @@ function handleOpenDialog(id?: number) { } } -// 刷新缓存 -function handleRefreshCache() { +// 防抖刷新缓存 +const debouncedRefresh = debounce(() => { ConfigAPI.refreshCache().then(() => { ElMessage.success("刷新成功"); }); +}, 1000); + +// 刷新缓存 +function handleRefreshCache() { + debouncedRefresh(); } // 系统配置表单提交