From 6a2f1f80f5ab989c77315f04e9a7b057cc3bf987 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sat, 21 May 2022 19:22:29 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=AF=B9=E8=AF=9D=E5=88=97=E8=A1=A8=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/DialogController.php | 9 ++++-- .../assets/js/pages/manage/messenger.vue | 28 +++++++++++++++---- resources/assets/js/store/actions.js | 14 +++++++++- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/Api/DialogController.php b/app/Http/Controllers/Api/DialogController.php index 67f863bfb..8d6ef895a 100755 --- a/app/Http/Controllers/Api/DialogController.php +++ b/app/Http/Controllers/Api/DialogController.php @@ -31,6 +31,7 @@ class DialogController extends AbstractController * @apiGroup dialog * @apiName lists * + * @apiParam {String} [at_after] 只读取在这个时间之后更新的对话 * @apiParam {Number} [page] 当前页,默认:1 * @apiParam {Number} [pagesize] 每页显示数量,默认:100,最大:200 * @@ -42,9 +43,13 @@ class DialogController extends AbstractController { $user = User::auth(); // - $list = WebSocketDialog::select(['web_socket_dialogs.*', 'u.top_at', 'u.mark_unread']) + $builder = WebSocketDialog::select(['web_socket_dialogs.*', 'u.top_at', 'u.mark_unread']) ->join('web_socket_dialog_users as u', 'web_socket_dialogs.id', '=', 'u.dialog_id') - ->where('u.userid', $user->userid) + ->where('u.userid', $user->userid); + if (Request::exists('at_after')) { + $builder->where('web_socket_dialogs.last_at', '>', Carbon::parse(Request::input('at_after'))); + } + $list = $builder ->orderByDesc('u.top_at') ->orderByDesc('web_socket_dialogs.last_at') ->paginate(Base::getPaginate(200, 100)); diff --git a/resources/assets/js/pages/manage/messenger.vue b/resources/assets/js/pages/manage/messenger.vue index 0ffcf30e5..01ccbae17 100644 --- a/resources/assets/js/pages/manage/messenger.vue +++ b/resources/assets/js/pages/manage/messenger.vue @@ -159,6 +159,10 @@ export default { } }, + activated() { + this.updateDialogs(); + }, + computed: { ...mapState(['userId', 'cacheDialogs']), @@ -254,11 +258,6 @@ export default { }, watch: { - tabActive(val) { - if (val && this.contactsData === null) { - this.getContactsList(1); - } - }, contactsKey(val) { setTimeout(() => { if (this.contactsKey == val) { @@ -267,6 +266,16 @@ export default { } }, 600); }, + tabActive: { + handler(val) { + if (val == 'contacts') { + this.contactsData === null && this.getContactsList(1); + } else { + this.updateDialogs(); + } + }, + immediate: true + }, dialogId: { handler(id) { if (id > 0) { @@ -512,7 +521,14 @@ export default { }).catch(({msg}) => { $A.modalError(msg); }); - } + }, + + updateDialogs() { + this.__updateDialogs && clearTimeout(this.__updateDialogs) + this.__updateDialogs = setTimeout(_ => { + this.$store.dispatch("getDialogs", true).catch(() => {}); + }, 2000) + }, } } diff --git a/resources/assets/js/store/actions.js b/resources/assets/js/store/actions.js index 189421829..0f4510e48 100644 --- a/resources/assets/js/store/actions.js +++ b/resources/assets/js/store/actions.js @@ -1905,17 +1905,29 @@ export default { * 获取会话列表 * @param state * @param dispatch + * @param atAfter * @returns {Promise} */ - getDialogs({state, dispatch}) { + getDialogs({state, dispatch}, atAfter) { return new Promise(function (resolve, reject) { if (state.userId === 0) { state.cacheDialogs = []; reject({msg: 'Parameter error'}); return; } + let data = {}; + if (atAfter === true && state.cacheDialogs.length > 0) { + const tmpList = state.cacheDialogs.sort((a, b) => { + if (a.top_at || b.top_at) { + return $A.Date(b.top_at) - $A.Date(a.top_at); + } + return $A.Date(b.last_at) - $A.Date(a.last_at); + }) + data.at_after = tmpList[0].last_at; + } dispatch("call", { url: 'dialog/lists', + data, }).then(result => { dispatch("saveDialog", result.data.data); resolve(result)