perf: 优化阅读消息

This commit is contained in:
kuaifan 2023-03-04 15:58:31 +08:00
parent 883cd94a5e
commit 5c6fc6fe1a
5 changed files with 34 additions and 7 deletions

View File

@ -569,6 +569,7 @@ class DialogController extends AbstractController
'mention' => $dialogUser->webSocketDialog->mention, 'mention' => $dialogUser->webSocketDialog->mention,
'position_msgs' => $dialogUser->webSocketDialog->position_msgs, 'position_msgs' => $dialogUser->webSocketDialog->position_msgs,
'user_at' => Carbon::parse($dialogUser->updated_at)->toDateTimeString(), 'user_at' => Carbon::parse($dialogUser->updated_at)->toDateTimeString(),
'user_ms' => WebSocketDialogUser::userMs($dialogUser->updated_at)
]; ];
} }
return Base::retSuccess('success', $data); return Base::retSuccess('success', $data);
@ -594,6 +595,7 @@ class DialogController extends AbstractController
"mention": 11, "mention": 11,
"position_msgs": [], "position_msgs": [],
"user_at": "2020-12-12 00:00:00", "user_at": "2020-12-12 00:00:00",
"user_ms": 1677558147167,
} }
*/ */
public function msg__unread() public function msg__unread()
@ -612,6 +614,7 @@ class DialogController extends AbstractController
'mention' => $dialogUser->webSocketDialog->mention, 'mention' => $dialogUser->webSocketDialog->mention,
'position_msgs' => $dialogUser->webSocketDialog->position_msgs, 'position_msgs' => $dialogUser->webSocketDialog->position_msgs,
'user_at' => Carbon::parse($dialogUser->updated_at)->toDateTimeString(), 'user_at' => Carbon::parse($dialogUser->updated_at)->toDateTimeString(),
'user_ms' => WebSocketDialogUser::userMs($dialogUser->updated_at)
]); ]);
} }
@ -1099,6 +1102,7 @@ class DialogController extends AbstractController
'mention' => $dialogUser->webSocketDialog->mention, 'mention' => $dialogUser->webSocketDialog->mention,
'position_msgs' => $dialogUser->webSocketDialog->position_msgs, 'position_msgs' => $dialogUser->webSocketDialog->position_msgs,
'user_at' => Carbon::now()->toDateTimeString(), 'user_at' => Carbon::now()->toDateTimeString(),
'user_ms' => WebSocketDialogUser::userMs(),
'mark_unread' => 0, 'mark_unread' => 0,
]; ];
break; break;
@ -1107,6 +1111,7 @@ class DialogController extends AbstractController
$data = [ $data = [
'id' => $dialogUser->webSocketDialog->id, 'id' => $dialogUser->webSocketDialog->id,
'user_at' => Carbon::now()->toDateTimeString(), 'user_at' => Carbon::now()->toDateTimeString(),
'user_ms' => WebSocketDialogUser::userMs(),
'mark_unread' => 1, 'mark_unread' => 1,
]; ];
break; break;

View File

@ -71,6 +71,7 @@ class WebSocketDialog extends AbstractModel
$this->pinyin = Base::cn2pinyin($this->name); $this->pinyin = Base::cn2pinyin($this->name);
$this->top_at = $this->top_at ?? $dialogUserFun('top_at'); $this->top_at = $this->top_at ?? $dialogUserFun('top_at');
$this->user_at = $this->user_at ?? $dialogUserFun('updated_at'); $this->user_at = $this->user_at ?? $dialogUserFun('updated_at');
$this->user_ms = WebSocketDialogUser::userMs($this->user_at);
// //
if (isset($this->search_msg_id)) { if (isset($this->search_msg_id)) {
// 最后消息 (搜索预览消息) // 最后消息 (搜索预览消息)

View File

@ -2,6 +2,8 @@
namespace App\Models; namespace App\Models;
use Carbon\Carbon;
/** /**
* App\Models\WebSocketDialogUser * App\Models\WebSocketDialogUser
* *
@ -40,4 +42,17 @@ class WebSocketDialogUser extends AbstractModel
{ {
return $this->hasOne(WebSocketDialog::class, 'id', 'dialog_id'); return $this->hasOne(WebSocketDialog::class, 'id', 'dialog_id');
} }
/**
* 转时间戳加上现在的毫秒
* @param $time
* @return int
*/
public static function userMs($time = 0)
{
if (empty($time)) {
return Carbon::now()->getPreciseTimestamp(3);
}
return intval(Carbon::parse($time)->timestamp . substr(Carbon::now()->getPreciseTimestamp(3), -3));
}
} }

View File

@ -8,7 +8,9 @@ use App\Models\User;
use App\Models\WebSocketDialog; use App\Models\WebSocketDialog;
use App\Models\WebSocketDialogMsg; use App\Models\WebSocketDialogMsg;
use App\Models\WebSocketDialogMsgRead; use App\Models\WebSocketDialogMsgRead;
use App\Models\WebSocketDialogUser;
use App\Module\Base; use App\Module\Base;
use Carbon\Carbon;
use Hhxsv5\LaravelS\Swoole\Task\Task; use Hhxsv5\LaravelS\Swoole\Task\Task;
use Request; use Request;
@ -154,6 +156,8 @@ class WebSocketDialogMsgTask extends AbstractTask
'silence' => $item['silence'] ? 1 : 0, 'silence' => $item['silence'] ? 1 : 0,
'data' => array_merge($msg->toArray(), [ 'data' => array_merge($msg->toArray(), [
'mention' => $item['mention'], 'mention' => $item['mention'],
'user_at' => Carbon::parse($msg->created_at)->toDateTimeString(),
'user_ms' => WebSocketDialogUser::userMs($msg->created_at),
]), ]),
] ]
]; ];
@ -197,7 +201,10 @@ class WebSocketDialogMsgTask extends AbstractTask
'type' => 'dialog', 'type' => 'dialog',
'mode' => 'chat', 'mode' => 'chat',
'silence' => $this->silence ? 1 : 0, 'silence' => $this->silence ? 1 : 0,
'data' => $msg->toArray(), 'data' => array_merge($msg->toArray(), [
'user_at' => Carbon::parse($msg->created_at)->toDateTimeString(),
'user_ms' => WebSocketDialogUser::userMs($msg->created_at),
]),
] ]
]; ];
} }

View File

@ -2075,8 +2075,8 @@ export default {
const index = state.cacheDialogs.findIndex(({id}) => id == data.id); const index = state.cacheDialogs.findIndex(({id}) => id == data.id);
if (index > -1) { if (index > -1) {
const original = state.cacheDialogs[index] const original = state.cacheDialogs[index]
const nowTime = data.user_ms || $A.TimeM(data.user_at); const nowTime = data.user_ms
const originalTime = $A.TimeM(original.user_at || original.updated_at); const originalTime = original.user_ms || 0
if (nowTime < originalTime) { if (nowTime < originalTime) {
typeof data.unread !== "undefined" && delete data.unread typeof data.unread !== "undefined" && delete data.unread
typeof data.mention !== "undefined" && delete data.mention typeof data.mention !== "undefined" && delete data.mention
@ -2574,8 +2574,7 @@ export default {
saveBefore() saveBefore()
const resData = result.data; const resData = result.data;
if ($A.isJson(resData.dialog)) { if ($A.isJson(resData.dialog)) {
const dialogData = Object.assign(resData.dialog, {user_ms: $A.TimeM(resData.dialog.user_at)}) setTimeout(_ => dispatch("saveDialog", resData.dialog), 300) // 延迟更新对话详情是因为等消息处理完
setTimeout(_ => dispatch("saveDialog", dialogData), 600) // 延迟更新对话详情是因为等消息处理完
// //
const ids = resData.list.map(({id}) => id) const ids = resData.list.map(({id}) => id)
state.dialogMsgs = state.dialogMsgs.filter(item => { state.dialogMsgs = state.dialogMsgs.filter(item => {
@ -2872,8 +2871,8 @@ export default {
id: dialog_id, id: dialog_id,
unread: dialog.unread + 1, unread: dialog.unread + 1,
mention: dialog.mention, mention: dialog.mention,
user_at: data.created_at, user_at: data.user_at,
user_ms: $A.TimeM(data.created_at), user_ms: data.user_ms,
} }
if (data.mention) { if (data.mention) {
newData.mention++; newData.mention++;