From f4e4252227ed57eb401b988a83e2375cea440d12 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Thu, 3 Mar 2022 11:41:54 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=B7=B2=E8=AF=BB?= =?UTF-8?q?=E5=9B=9E=E6=89=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/WebSocketDialogMsg.php | 32 ++++++++++++++----- app/Services/WebSocketService.php | 8 +++-- .../js/pages/manage/components/DialogView.vue | 6 ++-- resources/assets/js/store/actions.js | 8 ++++- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/app/Models/WebSocketDialogMsg.php b/app/Models/WebSocketDialogMsg.php index 8ed689fa..0353dc7f 100644 --- a/app/Models/WebSocketDialogMsg.php +++ b/app/Models/WebSocketDialogMsg.php @@ -70,11 +70,7 @@ class WebSocketDialogMsg extends AbstractModel public function getPercentageAttribute() { if (!isset($this->appendattrs['percentage'])) { - if ($this->read > $this->send || empty($this->send)) { - $this->appendattrs['percentage'] = 100; - } else { - $this->appendattrs['percentage'] = intval($this->read / $this->send * 100); - } + $this->generatePercentage(); } return $this->appendattrs['percentage']; } @@ -98,6 +94,22 @@ class WebSocketDialogMsg extends AbstractModel return $value; } + /** + * 获取占比 + * @param bool $increment 是否新增阅读数 + * @return int + */ + public function generatePercentage($increment = false) { + if ($increment) { + $this->increment('read'); + } + if ($this->read > $this->send || empty($this->send)) { + return $this->appendattrs['percentage'] = 100; + } else { + return $this->appendattrs['percentage'] = intval($this->read / $this->send * 100); + } + } + /** * 标记已送达 同时 告诉发送人已送达 * @param $userid @@ -127,13 +139,17 @@ class WebSocketDialogMsg extends AbstractModel if (!$msgRead->read_at) { $msgRead->read_at = Carbon::now(); $msgRead->save(); - $this->increment('read'); + $this->generatePercentage(true); PushTask::push([ 'userid' => $this->userid, 'msg' => [ 'type' => 'dialog', - 'mode' => 'update', - 'data' => $this->toArray(), + 'mode' => 'readed', + 'data' => [ + 'id' => $this->id, + 'read' => $this->read, + 'percentage' => $this->percentage, + ], ] ]); } diff --git a/app/Services/WebSocketService.php b/app/Services/WebSocketService.php index 685b54be..d97bb517 100644 --- a/app/Services/WebSocketService.php +++ b/app/Services/WebSocketService.php @@ -127,9 +127,11 @@ class WebSocketService implements WebSocketHandlerInterface case 'readMsg': $ids = is_array($data['id']) ? $data['id'] : [$data['id']]; $userid = $this->getUserid($frame->fd); - $list = WebSocketDialogMsg::whereIn('id', $ids)->get(); - $list->transform(function(WebSocketDialogMsg $item) use ($userid) { - $item->readSuccess($userid); + WebSocketDialogMsg::whereIn('id', $ids)->chunkById(20, function($list) use ($userid) { + /** @var WebSocketDialogMsg $item */ + foreach ($list as $item) { + $item->readSuccess($userid); + } }); return; diff --git a/resources/assets/js/pages/manage/components/DialogView.vue b/resources/assets/js/pages/manage/components/DialogView.vue index 51e3bbbf..12ea8933 100644 --- a/resources/assets/js/pages/manage/components/DialogView.vue +++ b/resources/assets/js/pages/manage/components/DialogView.vue @@ -46,6 +46,7 @@
{{$A.formatTime(msgData.created_at)}}
{ + setTimeout(() => { if (!this.$el.offsetParent) { this.msgData._r = false; return } this.$store.dispatch("dialogMsgRead", this.msgData); - }) + }, 50) }, popperShow() { @@ -151,6 +152,7 @@ export default { }, }).then(({data}) => { this.read_list = data; + this.$refs.percent.updatePopper(); }).catch(() => { this.read_list = []; }); diff --git a/resources/assets/js/store/actions.js b/resources/assets/js/store/actions.js index 0e0d625b..0071ea8f 100644 --- a/resources/assets/js/store/actions.js +++ b/resources/assets/js/store/actions.js @@ -2046,7 +2046,7 @@ export default { } }); state.wsReadWaitList = []; - }, 20); + }, 50); }, /** @@ -2174,6 +2174,12 @@ export default { // 更新最后消息 dispatch("updateDialogLastMsg", data); break; + case 'readed': + // 已读回执 + if (state.dialogMsgs.find(({id}) => id == data.id)) { + dispatch("saveDialogMsg", data) + } + break; } })(msgDetail); break;