perf: 优化更新对话列表机制

This commit is contained in:
kuaifan 2022-05-21 19:22:29 +08:00
parent d0d6b8ce97
commit 6a2f1f80f5
3 changed files with 42 additions and 9 deletions

View File

@ -31,6 +31,7 @@ class DialogController extends AbstractController
* @apiGroup dialog * @apiGroup dialog
* @apiName lists * @apiName lists
* *
* @apiParam {String} [at_after] 只读取在这个时间之后更新的对话
* @apiParam {Number} [page] 当前页,默认:1 * @apiParam {Number} [page] 当前页,默认:1
* @apiParam {Number} [pagesize] 每页显示数量,默认:100,最大:200 * @apiParam {Number} [pagesize] 每页显示数量,默认:100,最大:200
* *
@ -42,9 +43,13 @@ class DialogController extends AbstractController
{ {
$user = User::auth(); $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') ->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('u.top_at')
->orderByDesc('web_socket_dialogs.last_at') ->orderByDesc('web_socket_dialogs.last_at')
->paginate(Base::getPaginate(200, 100)); ->paginate(Base::getPaginate(200, 100));

View File

@ -159,6 +159,10 @@ export default {
} }
}, },
activated() {
this.updateDialogs();
},
computed: { computed: {
...mapState(['userId', 'cacheDialogs']), ...mapState(['userId', 'cacheDialogs']),
@ -254,11 +258,6 @@ export default {
}, },
watch: { watch: {
tabActive(val) {
if (val && this.contactsData === null) {
this.getContactsList(1);
}
},
contactsKey(val) { contactsKey(val) {
setTimeout(() => { setTimeout(() => {
if (this.contactsKey == val) { if (this.contactsKey == val) {
@ -267,6 +266,16 @@ export default {
} }
}, 600); }, 600);
}, },
tabActive: {
handler(val) {
if (val == 'contacts') {
this.contactsData === null && this.getContactsList(1);
} else {
this.updateDialogs();
}
},
immediate: true
},
dialogId: { dialogId: {
handler(id) { handler(id) {
if (id > 0) { if (id > 0) {
@ -512,7 +521,14 @@ export default {
}).catch(({msg}) => { }).catch(({msg}) => {
$A.modalError(msg); $A.modalError(msg);
}); });
} },
updateDialogs() {
this.__updateDialogs && clearTimeout(this.__updateDialogs)
this.__updateDialogs = setTimeout(_ => {
this.$store.dispatch("getDialogs", true).catch(() => {});
}, 2000)
},
} }
} }
</script> </script>

View File

@ -1905,17 +1905,29 @@ export default {
* 获取会话列表 * 获取会话列表
* @param state * @param state
* @param dispatch * @param dispatch
* @param atAfter
* @returns {Promise<unknown>} * @returns {Promise<unknown>}
*/ */
getDialogs({state, dispatch}) { getDialogs({state, dispatch}, atAfter) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
if (state.userId === 0) { if (state.userId === 0) {
state.cacheDialogs = []; state.cacheDialogs = [];
reject({msg: 'Parameter error'}); reject({msg: 'Parameter error'});
return; 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", { dispatch("call", {
url: 'dialog/lists', url: 'dialog/lists',
data,
}).then(result => { }).then(result => {
dispatch("saveDialog", result.data.data); dispatch("saveDialog", result.data.data);
resolve(result) resolve(result)