From b254fd5ce28ad0c2f5c2349ceb1f3bd07fc7171e Mon Sep 17 00:00:00 2001 From: wfs <605403358> Date: Tue, 12 Mar 2024 23:49:05 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=8E=A5=E9=BE=99?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=8A=A0=E4=B8=8A=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/DialogController.php | 60 ++++++++++--------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/app/Http/Controllers/Api/DialogController.php b/app/Http/Controllers/Api/DialogController.php index 27f4227d8..26c45ea6a 100755 --- a/app/Http/Controllers/Api/DialogController.php +++ b/app/Http/Controllers/Api/DialogController.php @@ -2180,35 +2180,39 @@ class DialogController extends AbstractController return Base::retError('内容最大不能超过200000字'); } // - $userid = $user->userid; - if ($uuid) { - $dialogMsg = WebSocketDialogMsg::whereDialogId($dialog_id) - ->whereType('word-chain') - ->orderByDesc('created_at') - ->where('msg', 'like', "%$uuid%") - ->value('msg'); - $list = array_reverse(array_merge($dialogMsg['list'] ?? [], $list)); - $list = array_reduce($list, function ($result, $item) { - $fieldValue = $item['id']; // 指定字段名 - if (!isset($result[$fieldValue])) { - $result[$fieldValue] = $item; - } - return $result; - }, []); - $list = array_reverse(array_values($list)); - } - // - usort($list, function($a, $b) { - return $a['id'] - $b['id']; + return AbstractModel::transaction(function () use ($user, $uuid, $dialog_id, $list, $text) { + if ($uuid) { + $dialogMsg = WebSocketDialogMsg::whereDialogId($dialog_id) + ->lockForUpdate() + ->whereType('word-chain') + ->orderByDesc('created_at') + ->where('msg', 'like', "%$uuid%") + ->value('msg'); + $list = array_reverse(array_merge($dialogMsg['list'] ?? [], $list)); + $list = array_reduce($list, function ($result, $item) { + $fieldValue = $item['id']; // 指定字段名 + if (!isset($result[$fieldValue])) { + $result[$fieldValue] = $item; + } + return $result; + }, []); + $list = array_reverse(array_values($list)); + } else { + $uuid = Base::generatePassword(36); + } + // + usort($list, function ($a, $b) { + return $a['id'] - $b['id']; + }); + // + $msgData = [ + 'text' => $text, + 'list' => $list, + 'userid' => $user->userid, + 'uuid' => $uuid, + ]; + return WebSocketDialogMsg::sendMsg(null, $dialog_id, 'word-chain', $msgData, $user->userid); }); - // - $msgData = [ - 'text' => $text, - 'list' => $list, - 'userid' => $userid, - 'uuid' => $uuid ?: Base::generatePassword(36), - ]; - return WebSocketDialogMsg::sendMsg(null, $dialog_id, 'word-chain', $msgData, $user->userid); } /**