perf: 优化已读标记

This commit is contained in:
kuaifan 2023-02-21 16:16:12 +08:00
parent efd25bc787
commit 71a9b8f6ce
3 changed files with 61 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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':