perf: 1. 强化接龙接口本地时间戳问题 2. 接龙消息点展开按钮后做缓存处理

This commit is contained in:
weifs 2024-03-19 14:38:15 +08:00
parent 7bfc43c85f
commit d12c0c4207
3 changed files with 36 additions and 15 deletions

View File

@ -2175,12 +2175,12 @@ class DialogController extends AbstractController
$dialog_id = intval(Request::input('dialog_id')); $dialog_id = intval(Request::input('dialog_id'));
$uuid = trim(Request::input('uuid')); $uuid = trim(Request::input('uuid'));
$text = trim(Request::input('text')); $text = trim(Request::input('text'));
$list = Request::input('list'); $list = Request::input('list') ?? [];
// //
WebSocketDialog::checkDialog($dialog_id); WebSocketDialog::checkDialog($dialog_id);
$strlen = mb_strlen($text); $strlen = mb_strlen($text);
$noimglen = mb_strlen(preg_replace("/<img[^>]*?>/i", "", $text)); $noimglen = mb_strlen(preg_replace("/<img[^>]*?>/i", "", $text));
if ($strlen < 1) { if ($strlen < 1 || empty($list)) {
return Base::retError('内容不能为空'); return Base::retError('内容不能为空');
} }
if ($noimglen > 200000) { if ($noimglen > 200000) {
@ -2195,17 +2195,28 @@ class DialogController extends AbstractController
->orderByDesc('created_at') ->orderByDesc('created_at')
->where('msg', 'like', "%$uuid%") ->where('msg', 'like', "%$uuid%")
->value('msg'); ->value('msg');
$list = array_reverse(array_merge($dialogMsg['list'] ?? [], $list)); // 新增
$list = array_reduce($list, function ($result, $item) { $msgList = $dialogMsg['list'] ?? [];
$fieldValue = $item['id']; // 指定字段名 $addList = array_udiff($list, $msgList, function($a, $b) {
if (!isset($result[$fieldValue])) { return ($a['id'] ?? 0) - $b['id'];
$result[$fieldValue] = $item; });
foreach ($addList as $key => $item) {
$item['id'] = intval(round(microtime(true) * 1000)) + $key;
$msgList[] = $item;
}
// 编辑更新
$lists = array_column($list,null,'id');
foreach ($msgList as $key => $item) {
if (isset($lists[$item['id']]) && $item['userid'] == $user->userid) {
$msgList[$key] = $lists[$item['id']];
} }
return $result; }
}, []); $list = $msgList;
$list = array_reverse(array_values($list));
} else { } else {
$uuid = Base::generatePassword(36); $uuid = Base::generatePassword(36);
foreach ($list as $key => $item) {
$list[$key]['id'] = intval(round(microtime(true) * 1000)) + $key;
}
} }
// //
usort($list, function ($a, $b) { usort($list, function ($a, $b) {

View File

@ -76,12 +76,12 @@
<!--接龙--> <!--接龙-->
<div v-else-if="msgData.type === 'word-chain'" class="content-text content-word-chain no-dark-content"> <div v-else-if="msgData.type === 'word-chain'" class="content-text content-word-chain no-dark-content">
<pre v-html="$A.formatTextMsg(msgData.msg.text, userId)"></pre> <pre v-html="$A.formatTextMsg(msgData.msg.text, userId)"></pre>
<ul> <ul :class="{'expand': unfoldWordChainData.indexOf(msgData.id) !== -1 }">
<li v-for="(item) in (msgData.msg.list || []).filter(h=>h.type == 'case')"> <li v-for="(item) in (msgData.msg.list || []).filter(h=>h.type == 'case')">
{{ $L('例') }} {{ item.text }} {{ $L('例') }} {{ item.text }}
</li> </li>
<li v-for="(item,index) in (msgData.msg.list || []).filter(h=>h.type != 'case')"> <li v-for="(item,index) in (msgData.msg.list || []).filter(h=>h.type != 'case')">
<span class="expand" v-if="index == 2 && msgData.msg.list.length > 4" @click="unfoldWordChain"> <span class="expand" v-if="index == 2 && msgData.msg.list.length > 4" @click="unfoldWordChain(msgData)">
...{{$L('展开')}}... ...{{$L('展开')}}...
</span> </span>
<span :class="{'shrink': index >= 2 && msgData.msg.list.length > 4 } "> <span :class="{'shrink': index >= 2 && msgData.msg.list.length > 4 } ">
@ -325,7 +325,8 @@ export default {
emojiUsersNum: 5, emojiUsersNum: 5,
voteData: {} voteData: {},
unfoldWordChainData: []
} }
}, },
@ -334,6 +335,9 @@ export default {
if (Object.keys(this.voteData).length === 0) { if (Object.keys(this.voteData).length === 0) {
this.voteData = JSON.parse(window.localStorage.getItem(`__cache:vote__`)) || {}; this.voteData = JSON.parse(window.localStorage.getItem(`__cache:vote__`)) || {};
} }
if (this.unfoldWordChainData.length === 0) {
this.unfoldWordChainData = JSON.parse(window.localStorage.getItem(`__cache:unfoldWordChain__`)) || [];
}
}, },
beforeDestroy() { beforeDestroy() {
@ -643,8 +647,13 @@ export default {
} }
}, },
unfoldWordChain(e) { unfoldWordChain(msg) {
e.target.parentNode?.parentNode?.classList.add('expand') if (this.unfoldWordChainData.indexOf(msg.id) == -1) {
const data = JSON.parse(window.localStorage.getItem('__cache:unfoldWordChain__')) || [];
data.push(msg.id);
window.localStorage.setItem('__cache:unfoldWordChain__', JSON.stringify(data));
this.unfoldWordChainData.push(msg.id);
}
}, },
onVote(type, msgData) { onVote(type, msgData) {

View File

@ -1198,6 +1198,7 @@ export default {
this.$store.dispatch('closeDialog', old_id) this.$store.dispatch('closeDialog', old_id)
// //
window.localStorage.removeItem('__cache:vote__') window.localStorage.removeItem('__cache:vote__')
window.localStorage.removeItem('__cache:unfoldWordChain__')
}, },
immediate: true immediate: true
}, },