perf: 优化投票接口,加上事务锁

This commit is contained in:
wfs 2024-03-13 00:17:23 +08:00
parent b254fd5ce2
commit 5efe659cf5

View File

@ -2253,8 +2253,6 @@ class DialogController extends AbstractController
WebSocketDialog::checkDialog($dialog_id); WebSocketDialog::checkDialog($dialog_id);
// //
$action = null; $action = null;
$userid = $user->userid;
$result = [];
if ($type != 'create') { if ($type != 'create') {
if ($type == 'vote' && empty($votes)) { if ($type == 'vote' && empty($votes)) {
return Base::retError('参数错误'); return Base::retError('参数错误');
@ -2262,42 +2260,50 @@ class DialogController extends AbstractController
if (empty($uuid)) { if (empty($uuid)) {
return Base::retError('参数错误'); return Base::retError('参数错误');
} }
$dialogMsgs = WebSocketDialogMsg::whereDialogId($dialog_id) return AbstractModel::transaction(function () use ($user, $uuid, $dialog_id, $type, $votes) {
->whereType('vote') //
->orderByDesc('created_at') $dialogMsgs = WebSocketDialogMsg::whereDialogId($dialog_id)
->where('msg', 'like', "%$uuid%") ->lockForUpdate()
->get(); ->whereType('vote')
// ->orderByDesc('created_at')
if ($type == 'again') { ->where('msg', 'like', "%$uuid%")
$res = WebSocketDialogMsg::sendMsg(null, $dialog_id, 'vote', $dialogMsgs[0]->msg, $user->userid); ->get();
if (Base::isError($res)) { //
return $res; $result = [];
} if ($type == 'again') {
$result[] = $res['data']; $res = WebSocketDialogMsg::sendMsg(null, $dialog_id, 'vote', $dialogMsgs[0]->msg, $user->userid);
} else {
foreach ($dialogMsgs as $dialogMsg) {
$action = "change-{$dialogMsg->id}";
$msgData = $dialogMsg->msg;
if ($type == 'finish') {
$msgData['state'] = 0;
} else {
$msgDataVotes = $msgData['votes'] ?? [];
if (in_array($userid, array_column($msgDataVotes, 'userid'))) {
return Base::retError('不能重复投票');
}
$msgDataVotes[] = [
'userid' => $userid,
'votes' => $votes,
];
$msgData['votes'] = $msgDataVotes;
}
$res = WebSocketDialogMsg::sendMsg($action, $dialog_id, 'vote', $msgData, $user->userid);
if (Base::isError($res)) { if (Base::isError($res)) {
return $res; return $res;
} }
$result[] = $res['data']; $result[] = $res['data'];
} else {
foreach ($dialogMsgs as $dialogMsg) {
$action = "change-{$dialogMsg->id}";
$msgData = $dialogMsg->msg;
if ($type == 'finish') {
$msgData['state'] = 0;
} else {
$msgDataVotes = $msgData['votes'] ?? [];
if (in_array($user->userid, array_column($msgDataVotes, 'userid'))) {
return Base::retError('不能重复投票');
}
$msgDataVotes[] = [
'userid' => $user->userid,
'votes' => $votes,
];
$msgData['votes'] = $msgDataVotes;
}
//
$res = WebSocketDialogMsg::sendMsg($action, $dialog_id, 'vote', $msgData, $user->userid);
if (Base::isError($res)) {
return $res;
}
$result[] = $res['data'];
}
} }
} //
return Base::retSuccess('发送成功', $result);
});
} else { } else {
$strlen = mb_strlen($text); $strlen = mb_strlen($text);
$noimglen = mb_strlen(preg_replace("/<img[^>]*?>/i", "", $text)); $noimglen = mb_strlen(preg_replace("/<img[^>]*?>/i", "", $text));
@ -2310,7 +2316,7 @@ class DialogController extends AbstractController
$msgData = [ $msgData = [
'text' => $text, 'text' => $text,
'list' => $list, 'list' => $list,
'userid' => $userid, 'userid' => $user->userid,
'uuid' => $uuid ?: Base::generatePassword(36), 'uuid' => $uuid ?: Base::generatePassword(36),
'multiple' => $multiple, 'multiple' => $multiple,
'anonymous' => $anonymous, 'anonymous' => $anonymous,
@ -2321,9 +2327,8 @@ class DialogController extends AbstractController
if (Base::isError($res)) { if (Base::isError($res)) {
return $res; return $res;
} }
$result[] = $res['data']; return Base::retSuccess('发送成功', [$res['data']]);
} }
return Base::retSuccess('发送成功', $result);
} }
/** /**