From 222291400557af22fe04dcb7d6ed287e5ea71ea3 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sun, 6 Jun 2021 20:41:10 +0800 Subject: [PATCH] no message --- resources/assets/js/pages/manage.vue | 18 +-- .../assets/js/pages/manage/messenger.vue | 78 +------------ resources/assets/js/store/mutations.js | 110 +++++++++++++++--- resources/assets/js/store/state.js | 1 + 4 files changed, 106 insertions(+), 101 deletions(-) diff --git a/resources/assets/js/pages/manage.vue b/resources/assets/js/pages/manage.vue index f6b4dc80..eb44ea77 100644 --- a/resources/assets/js/pages/manage.vue +++ b/resources/assets/js/pages/manage.vue @@ -267,27 +267,13 @@ export default { }, computed: { - ...mapState(['wsMsg', 'dialogShow', 'dialogMsgUnread', 'projectList']), + ...mapState(['dialogMsgUnread', 'projectList']), }, watch: { '$route' (route) { this.curPath = route.path; }, - - /** - * 收到新消息 - * @param msg - */ - wsMsg(msg) { - const {type, mode} = msg; - if (type === "dialog" && mode === "add") { - if (this.dialogShow) { - return; - } - this.$store.state.dialogMsgUnread++; - } - }, }, methods: { @@ -297,7 +283,7 @@ export default { value: [], }, { label: this.$L('软件开发'), - value: [this.$L('产品规划'),this.$L('前端开发'),this.$L('后端开发'),this.$L('测试'),this.$L('发布'),this.$L('其它')], + value: [this.$L('产品规划'), this.$L('前端开发'), this.$L('后端开发'), this.$L('测试'), this.$L('发布'), this.$L('其它')], }, { label: this.$L('产品开发'), value: [this.$L('产品计划'), this.$L('正在设计'), this.$L('正在研发'), this.$L('测试'), this.$L('准备发布'), this.$L('发布成功')], diff --git a/resources/assets/js/pages/manage/messenger.vue b/resources/assets/js/pages/manage/messenger.vue index 9d06aa19..b342ad9d 100644 --- a/resources/assets/js/pages/manage/messenger.vue +++ b/resources/assets/js/pages/manage/messenger.vue @@ -63,18 +63,20 @@ export default { components: {DialogWrapper}, data() { return { - dialogLoad: 0, dialogKey: '', - dialogList: [], + dialogLoad: 0, } }, mounted() { - this.getDialogLists(); + this.dialogLoad++; + this.$store.commit("getDialogList", () => { + this.dialogLoad--; + }) }, computed: { - ...mapState(['dialogId', 'dialogShow', 'wsMsg']), + ...mapState(['dialogId', 'dialogList']), dialogLists() { const {dialogKey} = this; @@ -93,79 +95,11 @@ export default { }, }, - watch: { - /** - * 收到新消息 - * @param msg - */ - wsMsg(msg) { - const {type, mode, data} = msg; - if (type === "dialog" && mode === "add") { - if (this.dialogShow && this.dialogId == data.dialog_id) { - return; - } - let dialog = this.dialogList.find(({id}) => id == data.dialog_id); - if (dialog) { - this.$set(dialog, 'unread', dialog.unread + 1); - this.$set(dialog, 'last_msg', data); - } else { - this.getDialogOne(data.dialog_id) - } - } - }, - - /** - * 打开对话,标记已读 - * @param dialog_id - */ - dialogId(dialog_id) { - let dialog = this.dialogList.find(({id}) => id == dialog_id); - if (dialog && dialog.unread > 0) { - this.$store.state.dialogMsgUnread-= dialog.unread; - this.$set(dialog, 'unread', 0); - } - } - }, - methods: { openDialog(dialog) { this.$store.commit('getDialogMsgList', dialog.id); }, - getDialogLists() { - this.dialogLoad++; - $A.apiAjax({ - url: 'dialog/lists', - complete: () => { - this.dialogLoad--; - }, - success: ({ret, data, msg}) => { - if (ret === 1) { - this.dialogList = data.data; - } - } - }); - }, - - getDialogOne(dialog_id) { - $A.apiAjax({ - url: 'dialog/one', - data: { - dialog_id, - }, - success: ({ret, data, msg}) => { - if (ret === 1) { - let index = this.dialogList.findIndex(({id}) => id == data.id); - if (index > -1) { - this.dialogList.splice(index, 1, data); - } else { - this.dialogList.unshift(data) - } - } - } - }); - }, - formatTime(date) { let time = Math.round(new Date(date).getTime() / 1000), string = ''; diff --git a/resources/assets/js/store/mutations.js b/resources/assets/js/store/mutations.js index a7833118..e18a0c8a 100644 --- a/resources/assets/js/store/mutations.js +++ b/resources/assets/js/store/mutations.js @@ -1,5 +1,3 @@ -import state from "./state"; - export default { /** * 切换Boolean变量 @@ -211,6 +209,61 @@ export default { }); }, + /** + * 获取对话列表 + * @param state + * @param completeCallback + */ + getDialogList(state, completeCallback) { + $A.apiAjax({ + url: 'dialog/lists', + complete: () => { + typeof completeCallback === "function" && completeCallback(); + }, + success: ({ret, data, msg}) => { + if (ret === 1) { + state.dialogList = data.data; + } + } + }); + }, + + /** + * 获取单个对话 + * @param state + * @param dialog_id + */ + getDialogOne(state, dialog_id) { + $A.apiAjax({ + url: 'dialog/one', + data: { + dialog_id, + }, + success: ({ret, data, msg}) => { + if (ret === 1) { + let index = state.dialogList.findIndex(({id}) => id == data.id); + if (index > -1) { + state.dialogList.splice(index, 1, data); + } else { + state.dialogList.unshift(data) + } + } + } + }); + + $A.apiAjax({ + url: 'dialog/lists', + complete: () => { + typeof completeCallback === "function" && completeCallback(); + }, + success: ({ret, data, msg}) => { + if (ret === 1) { + state.dialogList = data.data; + } + } + }); + }, + /** * 获取对话消息 * @param state @@ -237,6 +290,12 @@ export default { } state.dialogId = dialog_id; // + let dialog = state.dialogList.find(({id}) => id == dialog_id); + if (dialog && dialog.unread > 0) { + state.dialogMsgUnread-= dialog.unread; + dialog.unread = 0; + } + // if (state.cacheDialog[dialog_id + "::load"]) { return; } @@ -395,19 +454,44 @@ export default { } }); if (type === "dialog") { - const msgData = msgDetail.data; - const dialog_id = msgData.dialog_id; - if (dialog_id === state.dialogId) { - let index = state.dialogMsgList.findIndex(({id}) => id === msgData.id); - if (index === -1) { - if (state.dialogMsgList.length >= 200) { - state.dialogMsgList.splice(0, 1); + // 更新消息 + (function (msg) { + const {data} = msg; + const {dialog_id} = data; + if (dialog_id === state.dialogId) { + let index = state.dialogMsgList.findIndex(({id}) => id === data.id); + if (index === -1) { + if (state.dialogMsgList.length >= 200) { + state.dialogMsgList.splice(0, 1); + } + state.dialogMsgList.push(data); + } else { + state.dialogMsgList.splice(index, 1, data); } - state.dialogMsgList.push(msgData); - } else { - state.dialogMsgList.splice(index, 1, msgData); } - } + })(msgDetail); + // 更新对话 + (function (msg, that) { + const {mode, data} = msg; + const {dialog_id} = data; + if (mode === "add") { + let dialog = state.dialogList.find(({id}) => id == dialog_id); + if (dialog) { + dialog.last_msg = data; + if (state.dialogId !== dialog_id) dialog.unread++; + // 移动到首位 + const index = state.dialogList.findIndex(({id}) => id == dialog_id); + if (index > -1) { + const tmp = state.dialogList[index]; + state.dialogList.splice(index, 1); + state.dialogList.unshift(tmp); + } + } else { + that.commit('getDialogOne', dialog_id); + } + if (!state.dialogShow || state.dialogId !== dialog_id) state.dialogMsgUnread++; + } + })(msgDetail, this); } break } diff --git a/resources/assets/js/store/state.js b/resources/assets/js/store/state.js index 6be5eeb4..940a4b1c 100644 --- a/resources/assets/js/store/state.js +++ b/resources/assets/js/store/state.js @@ -187,6 +187,7 @@ state.projectMsgUnread = 0; // 会话消息 state.dialogId = 0; state.dialogShow = false; +state.dialogList = []; state.dialogDetail = {}; state.dialogMsgLoad = 0; state.dialogMsgList = [];