From 71a9b8f6cefa09d12226c626d8ef2ef5f4e58918 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Tue, 21 Feb 2023 16:16:12 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=B7=B2=E8=AF=BB?= =?UTF-8?q?=E6=A0=87=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Module/Base.php | 21 +++++++++++++++++++++ resources/assets/js/functions/common.js | 21 +++++++++++++++++++++ resources/assets/js/store/actions.js | 21 +++++++++++++++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/app/Module/Base.php b/app/Module/Base.php index 8868f9c11..6be07b9a0 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -1311,6 +1311,27 @@ class Base return $setting; } + /** + * 时间转毫秒时间戳 + * @param $time + * @return float|int + */ + public static function strtotimeM($time) + { + if (str_contains($time, '.')) { + list($t, $m) = explode(".", $time); + if (is_string($t)) { + $t = strtotime($t); + } + $time = $t . str_pad($m, 3, "0", STR_PAD_LEFT); + } + if (is_numeric($time)) { + return (int) str_pad($time, 13, "0"); + } else { + return strtotime($time) * 1000; + } + } + /** * 获取设置值 * @param $setname diff --git a/resources/assets/js/functions/common.js b/resources/assets/js/functions/common.js index 1327acb9c..4f100f063 100755 --- a/resources/assets/js/functions/common.js +++ b/resources/assets/js/functions/common.js @@ -253,6 +253,27 @@ const localforage = require("localforage"); return Math.round(time / 1000) }, + /** + * 返回毫秒时间戳 + * @param v + * @param cm 使用当前的毫秒 + * @returns {number} + * @constructor + */ + TimeM(v = undefined, cm = true) { + let time + if (typeof v === "string" && this.strExists(v, "-")) { + v = v.replace(/-/g, '/'); + time = new Date(v).getTime(); + if (cm && v.indexOf('.') === -1) { + time = parseInt(`${Math.round(time / 1000)}${new Date().getMilliseconds()}`) + } + } else { + time = new Date().getTime(); + } + return time + }, + /** * 返回 时间对象|时间戳 * @param v diff --git a/resources/assets/js/store/actions.js b/resources/assets/js/store/actions.js index cdfeba922..ba6b03f62 100644 --- a/resources/assets/js/store/actions.js +++ b/resources/assets/js/store/actions.js @@ -2067,7 +2067,9 @@ export default { const index = state.cacheDialogs.findIndex(({id}) => id == data.id); if (index > -1) { const original = state.cacheDialogs[index] - if ($A.Time(data.user_at) < $A.Time(original.user_at || original.updated_at)) { + const nowTime = data.user_ms || $A.TimeM(data.user_at); + const originalTime = $A.TimeM(original.user_at || original.updated_at); + if (nowTime < originalTime) { typeof data.unread !== "undefined" && delete data.unread typeof data.mention !== "undefined" && delete data.mention typeof data.position_msgs !== "undefined" && delete data.position_msgs @@ -2844,11 +2846,13 @@ export default { unread: dialog.unread + 1, mention: dialog.mention, user_at: data.created_at, + user_ms: $A.TimeM(data.created_at), } if (data.mention) { newData.mention++; } - dispatch("saveDialog", newData) + const timeout = state.dialogIns.findIndex(item => item.dialog_id === dialog_id) > -1 ? 3000 : 300 + setTimeout(_ => dispatch("saveDialog", newData), timeout) } } if (!silence) { @@ -2869,6 +2873,19 @@ export default { if (typeof data.todo !== "undefined") { dispatch("getDialogTodo", dialog_id) } + } else if (mode === 'readed') { + // 消息不存在,重试已读标记 + let readedNum = 0 + const readedTimer = setInterval(_ => { + if (readedNum > 6) { + clearInterval(readedTimer) + } + if (state.dialogMsgs.find(({id}) => id == data.id)) { + clearInterval(readedTimer) + dispatch("saveDialogMsg", data) + } + readedNum++ + }, 500) } break; case 'groupAdd':