From 635cc04c50b0a5afada79259ecf411b165b28a41 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Thu, 13 Mar 2025 01:38:35 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/assets/js/functions/common.js | 27 +++++++++++++++++-- .../pages/manage/components/DialogWrapper.vue | 14 ++++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/resources/assets/js/functions/common.js b/resources/assets/js/functions/common.js index 0dfa3f2ec..beb99a7d9 100755 --- a/resources/assets/js/functions/common.js +++ b/resources/assets/js/functions/common.js @@ -1082,13 +1082,14 @@ const timezone = require("dayjs/plugin/timezone"); /** * 滚动到元素并抖动 * @param element + * @param viewIfNeeded */ - scrollIntoAndShake(element) { + scrollIntoAndShake(element, viewIfNeeded = true) { if (!element) return; const elements = Array.isArray(element) ? element : [element]; elements.forEach(el => { if (el) { - $A.scrollIntoViewIfNeeded(el); + viewIfNeeded && $A.scrollIntoViewIfNeeded(el); $A.addClassWithTimeout(el, "common-shake", 800); } }); @@ -1300,6 +1301,28 @@ const timezone = require("dayjs/plugin/timezone"); item === arr2[arr2.length - arr1.length + index] ).length)); }, + + /** + * 查找元素并在失败时重试 + * @param {Function} findElementFn - 查找元素的函数 + * @param {number} maxAttempts - 最大尝试次数 + * @param {number} delayMs - 每次尝试之间的延迟(毫秒) + * @returns {Promise<*>} + */ + async findElementWithRetry(findElementFn, maxAttempts = 3, delayMs = 500) { + for (let attempt = 1; attempt <= maxAttempts; attempt++) { + const element = findElementFn(); + + if (element) { + return element; + } + + if (attempt < maxAttempts) { + await new Promise(resolve => setTimeout(resolve, delayMs)); + } + } + throw new Error(`Element not found after ${maxAttempts} attempts`); + } }); /** diff --git a/resources/assets/js/pages/manage/components/DialogWrapper.vue b/resources/assets/js/pages/manage/components/DialogWrapper.vue index 75ccba9a3..b62cc8fd5 100644 --- a/resources/assets/js/pages/manage/components/DialogWrapper.vue +++ b/resources/assets/js/pages/manage/components/DialogWrapper.vue @@ -1487,8 +1487,7 @@ export default { msgActiveId(val) { if (val > 0) { this.msgActiveId = 0 - const element = this.$refs.scroller.$el.querySelector(`[data-id="${val}"]`)?.querySelector(".dialog-head") - $A.scrollIntoAndShake(element) + this.shakeToMsgId(val) } }, @@ -2650,6 +2649,8 @@ export default { msg_id: this.msgId, msg_type: this.msgType, clear_before: true + }).then(_ => { + this.onToBottom() }).catch(_ => {}) }, @@ -4382,6 +4383,15 @@ export default { $A.messageError(msg); }); }, + + async shakeToMsgId(id) { + try { + const element = await $A.findElementWithRetry(() => this.$refs.scroller.$el.querySelector(`[data-id="${id}"]`)?.querySelector(".dialog-head")); + $A.scrollIntoAndShake(element, false) + } catch (e) { + // console.log(e) + } + } } }