diff --git a/im-platform/src/main/java/com/bx/implatform/enums/MessageType.java b/im-platform/src/main/java/com/bx/implatform/enums/MessageType.java index 04f30f9..5e6d2e8 100644 --- a/im-platform/src/main/java/com/bx/implatform/enums/MessageType.java +++ b/im-platform/src/main/java/com/bx/implatform/enums/MessageType.java @@ -4,8 +4,8 @@ package com.bx.implatform.enums; public enum MessageType { TEXT(0,"文字"), - FILE(1,"文件"), - IMAGE(2,"图片"), + IMAGE(1,"图片"), + FILE(2,"文件"), AUDIO(3,"音频"), VIDEO(4,"视频"), RECALL(10,"撤回"), diff --git a/im-platform/src/main/resources/db/db.sql b/im-platform/src/main/resources/db/db.sql index 5e2365b..ba33925 100644 --- a/im-platform/src/main/resources/db/db.sql +++ b/im-platform/src/main/resources/db/db.sql @@ -45,8 +45,8 @@ create table `im_group`( `head_image_thumb` varchar(255) default '' comment '群头像缩略图', `notice` varchar(1024) default '' comment '群公告', `remark` varchar(255) default '' comment '群备注', - `deleted` tinyint(1) DEFAULT 0 comment '是否已删除', - `created_time` datetime DEFAULT CURRENT_TIMESTAMP comment '创建时间' + `deleted` tinyint(1) default 0 comment '是否已删除', + `created_time` datetime default CURRENT_TIMESTAMP comment '创建时间' )ENGINE=InnoDB CHARSET=utf8mb3 comment '群'; create table `im_group_member`( diff --git a/im-ui/public/favicon.ico b/im-ui/public/favicon.ico deleted file mode 100644 index df36fcf..0000000 Binary files a/im-ui/public/favicon.ico and /dev/null differ diff --git a/im-ui/public/index.html b/im-ui/public/index.html index 5ae1079..724a362 100644 --- a/im-ui/public/index.html +++ b/im-ui/public/index.html @@ -4,7 +4,7 @@ - + 盒子IM diff --git a/im-ui/public/logo.png b/im-ui/public/logo.png new file mode 100644 index 0000000..d5690e3 Binary files /dev/null and b/im-ui/public/logo.png differ diff --git a/im-ui/src/api/enums.js b/im-ui/src/api/enums.js index e58857d..eb41941 100644 --- a/im-ui/src/api/enums.js +++ b/im-ui/src/api/enums.js @@ -1,8 +1,8 @@ const MESSAGE_TYPE = { TEXT: 0, - FILE:1, - IMAGE:2, + IMAGE:1, + FILE:2, AUDIO:3, VIDEO:4, RECALL:10, diff --git a/im-ui/src/assets/logo.png b/im-ui/src/assets/logo.png deleted file mode 100644 index f3d2503..0000000 Binary files a/im-ui/src/assets/logo.png and /dev/null differ diff --git a/im-ui/src/components/chat/ChatMessageItem.vue b/im-ui/src/components/chat/ChatMessageItem.vue index 2eddebc..72039ae 100644 --- a/im-ui/src/components/chat/ChatMessageItem.vue +++ b/im-ui/src/components/chat/ChatMessageItem.vue @@ -12,13 +12,13 @@
-
+
-
+
{{data.name}} diff --git a/im-ui/src/store/chatStore.js b/im-ui/src/store/chatStore.js index ed86801..c11ed1a 100644 --- a/im-ui/src/store/chatStore.js +++ b/im-ui/src/store/chatStore.js @@ -1,3 +1,4 @@ +import {MESSAGE_TYPE} from "../api/enums.js" export default { state: { @@ -85,11 +86,11 @@ export default { } } // 插入新的数据 - if(msgInfo.type == 1){ + if(msgInfo.type == MESSAGE_TYPE.IMAGE ){ chat.lastContent = "[图片]"; - }else if(msgInfo.type == 2){ + }else if(msgInfo.type == MESSAGE_TYPE.FILE){ chat.lastContent = "[文件]"; - }else if(msgInfo.type == 3){ + }else if(msgInfo.type == MESSAGE_TYPE.AUDIO){ chat.lastContent = "[语音]"; }else{ chat.lastContent = msgInfo.content; diff --git a/im-uniapp/App.vue b/im-uniapp/App.vue index 3c216ef..4463d59 100644 --- a/im-uniapp/App.vue +++ b/im-uniapp/App.vue @@ -11,16 +11,18 @@ } }, methods: { - init(loginInfo) { + init() { // 加载数据 store.dispatch("load").then(() => { // 初始化websocket - this.initWebSocket(loginInfo); + this.initWebSocket(); }).catch((e) => { + console.log(e); this.exit(); }) }, - initWebSocket(loginInfo) { + initWebSocket() { + let loginInfo = uni.getStorageSync("loginInfo") let userId = store.state.userStore.userInfo.id; wsApi.createWebSocket(process.env.WS_URL, loginInfo.accessToken); wsApi.onopen(() => { @@ -129,7 +131,7 @@ console.log("exit"); wsApi.closeWebSocket(); uni.removeStorageSync("loginInfo"); - uni.navigateTo({ + uni.reLaunch({ url: "/pages/login/login" }) }, @@ -142,10 +144,9 @@ }, onLaunch() { // 登录状态校验 - let loginInfo = uni.getStorageSync("loginInfo"); - if (loginInfo) { + if (uni.getStorageSync("loginInfo")) { // 初始化 - this.init(loginInfo) + this.init() } else { // 跳转到登录页 uni.navigateTo({ diff --git a/im-uniapp/common/enums.js b/im-uniapp/common/enums.js index e58857d..a902aee 100644 --- a/im-uniapp/common/enums.js +++ b/im-uniapp/common/enums.js @@ -1,8 +1,8 @@ const MESSAGE_TYPE = { TEXT: 0, - FILE:1, - IMAGE:2, + IMAGE: 1, + FILE:2, AUDIO:3, VIDEO:4, RECALL:10, diff --git a/im-uniapp/common/request.js b/im-uniapp/common/request.js index c70648a..f0ca4d7 100644 --- a/im-uniapp/common/request.js +++ b/im-uniapp/common/request.js @@ -24,28 +24,30 @@ const request = (options) => { console.log("token失效,尝试重新获取") if (isRefreshToken) { // 正在刷新token,把其他请求存起来 - return new Promise(resolve => { - requestList.push(() => { - resolve(request(options)) - }) + requestList.push(() => { + resolve(request(options)) }) + return; } isRefreshToken = true; // 发送请求, 进行刷新token操作, 获取新的token - const res = await reqRefreshToken(loginInfo).catch((res) => { - return navToLogin(); - }).finally(()=>{ - requestList.forEach(cb => cb()); + const res = await reqRefreshToken(loginInfo); + if (!res || res.data.code != 200) { requestList = []; isRefreshToken = false; - }) - if (res.data.code != 200) { - return navToLogin(); + console.log("刷新token失败") + navToLogin(); + return; } - // 保存token uni.setStorageSync("loginInfo", res.data.data); + requestList.forEach(cb => cb()); + requestList = []; + isRefreshToken = false; + // 保存token + console.log(res.data.data.accessToken) + // 重新发送刚才的请求 - return request(options) + return resolve(request(options)) } else { uni.showToast({ diff --git a/im-uniapp/common/wssocket.js b/im-uniapp/common/wssocket.js index 6da3e33..22e6fda 100644 --- a/im-uniapp/common/wssocket.js +++ b/im-uniapp/common/wssocket.js @@ -4,7 +4,7 @@ let messageCallBack = null; let openCallBack = null; let isConnect = false; //连接标识 避免重复连接 let hasLogin = false; - +let hasInit = false; let createWebSocket = (url, token) => { wsurl = url; accessToken = token; @@ -27,7 +27,12 @@ let initWebSocket = () => { reConnect(); //如果无法连接上webSocket 那么重新连接!可能会因为服务器重新部署,或者短暂断网等导致无法创建连接 } }); - + + // 不能绑定多次事件,不然多触发,即便之前已经调了uni.closeSocket + if(hasInit){ + return; + } + hasInit = true; uni.onSocketOpen((res) => { console.log("WebSocket连接已打开"); isConnect = true; @@ -77,7 +82,7 @@ let initWebSocket = () => { showCancel: false, }) }) - + }; //定义重连函数 @@ -97,15 +102,19 @@ let closeWebSocket = () => { resolve(); return; } - console.log("关闭websocket连接"); uni.closeSocket({ - code: 1000, + code: 3000, complete: (res) => { + console.log("关闭websocket连接"); hasLogin = false; isConnect = false; resolve(); + }, + fail:(e)=>{ + console.log("关闭websocket连接失败",e); } }) + }) diff --git a/im-uniapp/components/chat-message-item/chat-message-item.vue b/im-uniapp/components/chat-message-item/chat-message-item.vue index a5a3e2e..313ca3a 100644 --- a/im-uniapp/components/chat-message-item/chat-message-item.vue +++ b/im-uniapp/components/chat-message-item/chat-message-item.vue @@ -1,49 +1,58 @@ @@ -62,36 +71,52 @@ msgInfo: { type: Object, required: true - }, - menu:{ - type: Boolean, - default: true } }, data() { return { audioPlayState: 'STOP', - rightMenu: { + menu: { show: false, - pos: { - x: 0, - y: 0 - } + style: "" } } }, methods: { - handleSendFail() { - this.$message.error("该文件已发送失败,目前不支持自动重新发送,建议手动重新发送") + onShowMenu(e) { + uni.getSystemInfo({ + success: (res) => { + let touches = e.touches[0]; + let style = ""; + /* 因 非H5端不兼容 style 属性绑定 Object ,所以拼接字符 */ + if (touches.clientY > (res.windowHeight / 2)) { + style = `bottom:${res.windowHeight-touches.clientY}px;`; + } else { + style = `top:${touches.clientY}px;`; + } + if (touches.clientX > (res.windowWidth / 2)) { + style += `right:${res.windowWidth-touches.clientX}px;`; + } else { + style += `left:${touches.clientX}px;`; + } + this.menu.style = style; + // + this.$nextTick(() => { + this.menu.show = true; + }); + } + }) + + }, - showFullImageBox() { - let imageUrl = JSON.parse(this.msgInfo.content).originUrl; - if (imageUrl) { - this.$store.commit('showFullImageBox', imageUrl); - } + onSendFail() { + uni.showToast({ + title: "该文件已发送失败,目前不支持自动重新发送,建议手动重新发送", + icon: "none" + }) }, - handlePlayVoice() { + onPlayVoice() { if (!this.audio) { this.audio = new Audio(); } @@ -99,27 +124,33 @@ this.audio.play(); this.handlePlayVoice = 'RUNNING'; }, - showRightMenu(e) { - this.rightMenu.pos = { - x: e.x, - y: e.y - }; - this.rightMenu.show = "true"; - }, - handleSelectMenu(item) { + onSelectMenu(item) { this.$emit(item.key.toLowerCase(), this.msgInfo); + this.menu.show = false; + }, + onShowFullImage() { + let imageUrl = JSON.parse(this.msgInfo.content).originUrl; + uni.previewImage({ + urls: [imageUrl] + }) + }, + onShowUserInfo(userId){ + uni.navigateTo({ + url: "/pages/common/user-info?id=" + userId + }) } + }, computed: { loading() { return !this.isTimeout && this.msgInfo.loadStatus && this.msgInfo.loadStatus === "loading"; }, loadFail() { - return this.msgInfo.loadStatus && (this.isTimeout || this.msgInfo.loadStatus === "fail"); + return this.msgInfo.loadStatus && (this.isTimeout || this.msgInfo.loadStatus === "fail"); }, - isTimeout(){ - return (new Date().getTime() - new Date(this.msgInfo.sendTime).getTime()) > 30*1000; - + isTimeout() { + return (new Date().getTime() - new Date(this.msgInfo.sendTime).getTime()) > 30 * 1000; + }, data() { return JSON.parse(this.msgInfo.content) @@ -139,29 +170,36 @@ items.push({ key: 'DELETE', name: '删除', - icon: 'el-icon-delete' + icon: 'trash' }); if (this.msgInfo.selfSend && this.msgInfo.id > 0) { items.push({ key: 'RECALL', name: '撤回', - icon: 'el-icon-refresh-left' + icon: 'refreshempty' + }); + } + if (this.msgInfo.type == this.$enums.MESSAGE_TYPE.FILE) { + items.push({ + key: 'DOWNLOAD', + name: '下载并打开', + icon: 'download' }); } return items; } - }, - mounted() { - //console.log(this.msgInfo); } + } - + \ No newline at end of file diff --git a/im-uniapp/components/file-upload/file-upload.vue b/im-uniapp/components/file-upload/file-upload.vue new file mode 100644 index 0000000..a2b54ac --- /dev/null +++ b/im-uniapp/components/file-upload/file-upload.vue @@ -0,0 +1,94 @@ + + + + + \ No newline at end of file diff --git a/im-uniapp/components/image-upload/image-upload.vue b/im-uniapp/components/image-upload/image-upload.vue index 75b970f..30723ec 100644 --- a/im-uniapp/components/image-upload/image-upload.vue +++ b/im-uniapp/components/image-upload/image-upload.vue @@ -17,7 +17,11 @@ props: { maxSize: { type: Number, - default: null + default: 5*1024*1024 + }, + sourceType:{ + type: String, + default: 'album' }, onBefore: { type: Function, @@ -34,14 +38,12 @@ }, methods: { selectAndUpload() { - console.log("selectAndUpload"); uni.chooseImage({ count: 9, //最多可以选择的图片张数,默认9 - sourceType: ['album'], //album 从相册选图,camera 使用相机,默认二者都有。如需直接开相机或直接选相册,请只使用一个选项 + sourceType: [this.sourceType], //album 从相册选图,camera 使用相机,默认二者都有。如需直接开相机或直接选相册,请只使用一个选项 sizeType: ['original'], //original 原图,compressed 压缩图,默认二者都有 success: (res) => { res.tempFiles.forEach((file) => { - console.log("选择文件"); // 校验大小 if (this.maxSize && file.size > this.maxSize) { this.$message.error(`文件大小不能超过 ${this.fileSizeStr}!`); @@ -58,7 +60,6 @@ }) }, uploadImage(file) { - console.log("上传文件") uni.uploadFile({ url: process.env.BASE_URL + '/image/upload', header: { @@ -71,13 +72,11 @@ if(data.code != 200){ this.onError && this.onError(file, data); }else{ - console.log("上传成功") this.onSuccess && this.onSuccess(file, data); } }, fail: (err) => { - console.log("上传失败") - console.log(this.onError) + console.log(err); this.onError && this.onError(file, err); } }) diff --git a/im-uniapp/components/pop-menu/pop-menu.vue b/im-uniapp/components/pop-menu/pop-menu.vue new file mode 100644 index 0000000..065890a --- /dev/null +++ b/im-uniapp/components/pop-menu/pop-menu.vue @@ -0,0 +1,67 @@ + + + + + \ No newline at end of file diff --git a/im-uniapp/manifest.json b/im-uniapp/manifest.json index e90c7d3..bd24642 100644 --- a/im-uniapp/manifest.json +++ b/im-uniapp/manifest.json @@ -68,5 +68,8 @@ "uniStatistics" : { "enable" : false }, - "vueVersion" : "3" + "vueVersion" : "3", + "h5" : { + "title" : "盒子IM" + } } diff --git a/im-uniapp/pages/chat/chat-box.vue b/im-uniapp/pages/chat/chat-box.vue index 4850439..ca29d9b 100644 --- a/im-uniapp/pages/chat/chat-box.vue +++ b/im-uniapp/pages/chat/chat-box.vue @@ -2,12 +2,13 @@ {{title}} - + @@ -17,13 +18,11 @@ + :adjust-position="false" @confirm="sendTextMessage()" @keyboardheightchange="onKeyboardheightchange" + confirm-type="send" confirm-hold :hold-keyboard="true"> - - + + @@ -32,16 +31,36 @@ - 相册 - - - {{ tool.name }} + + + + + 拍摄 + + + + + + 文件 + + + + 语音输入 + + + + 呼叫 + + @@ -69,24 +88,7 @@ scrollMsgIdx: 0, // 滚动条定位为到哪条消息 chatTabBox: 'none', showKeyBoard: false, - keyboardHeight: 322, - tools: [{ - name: "拍摄", - icon: "icon-camera" - }, - { - name: "语音输入", - icon: "icon-microphone" - }, - { - name: "文件", - icon: "icon-folder" - }, - { - name: "呼叫", - icon: "icon-call" - } - ] + keyboardHeight: 322 } }, methods: { @@ -151,7 +153,7 @@ } }, scrollToMsgIdx(idx) { - // 踩坑:如果scrollMsgIdx值没变化,滚动条不会移动 + // 如果scrollMsgIdx值没变化,滚动条不会移动 if (idx == this.scrollMsgIdx && idx > 0) { this.$nextTick(() => { // 先滚动到上一条 @@ -168,7 +170,6 @@ }, switchChatTabBox(chatTabBox, hideKeyBoard) { this.chatTabBox = chatTabBox; - this.scrollToBottom(); if (hideKeyBoard) { uni.hideKeyboard(); } @@ -176,7 +177,8 @@ selectEmoji(emoText) { this.sendText += `#${emoText};`; }, - onKeyboardheightchange(e) {; + onKeyboardheightchange(e) { + ; if (e.detail.height > 0) { this.showKeyBoard = true; this.switchChatTabBox('none', false) @@ -185,9 +187,6 @@ this.showKeyBoard = false; } }, - onSendTextBlur() { - //this.switchChatTabBox("none") - }, onUploadImageBefore(file) { let data = { originUrl: file.path, @@ -200,7 +199,7 @@ content: JSON.stringify(data), sendTime: new Date().getTime(), selfSend: true, - type: 1, + type: this.$enums.MESSAGE_TYPE.IMAGE, loadStatus: "loading" } // 填充对方id @@ -231,13 +230,121 @@ msgInfo.loadStatus = 'fail'; this.$store.commit("insertMessage", msgInfo); }, - onClickTool(tool) { - switch (tool.name) { - case "相册": - break; - + onUploadFileBefore(file) { + let data = { + name: file.name, + size: file.size, + url: file.path + } + let msgInfo = { + id: 0, + sendId: this.mine.id, + content: JSON.stringify(data), + sendTime: new Date().getTime(), + selfSend: true, + type: this.$enums.MESSAGE_TYPE.FILE, + loadStatus: "loading" + } + // 填充对方id + this.fillTargetId(msgInfo, this.chat.targetId); + // 插入消息 + this.$store.commit("insertMessage", msgInfo); + // 借助file对象保存 + file.msgInfo = msgInfo; + // 滚到最低部 + this.scrollToBottom(); + return true; + }, + onUploadFileSuccess(file, res) { + let data = { + name: file.name, + size: file.size, + url: res.data + } + let msgInfo = JSON.parse(JSON.stringify(file.msgInfo)); + msgInfo.content = JSON.stringify(data); + this.$http({ + url: this.messageAction, + method: 'POST', + data: msgInfo + }).then((id) => { + msgInfo.loadStatus = 'ok'; + msgInfo.id = id; + this.$store.commit("insertMessage", msgInfo); + }) + }, + onUploadFileFail(file, res) { + let msgInfo = JSON.parse(JSON.stringify(file.msgInfo)); + msgInfo.loadStatus = 'fail'; + this.$store.commit("insertMessage", msgInfo); + }, + onDeleteMessage(msgInfo) { + uni.showModal({ + title: '删除消息', + content: '确认删除消息?', + success: (res) => { + if (!res.cancel) { + this.$store.commit("deleteMessage", msgInfo); + uni.showToast({ + title: "删除成功", + icon: "none" + }) + } + } + }) + }, + onRecallMessage(msgInfo) { + uni.showModal({ + title: '撤回消息', + content: '确认撤回消息?', + success: (res) => { + if (!res.cancel) { + let url = `/message/${this.chat.type.toLowerCase()}/recall/${msgInfo.id}` + this.$http({ + url: url, + method: 'DELETE' + }).then(() => { + msgInfo = JSON.parse(JSON.stringify(msgInfo)); + msgInfo.type = this.$enums.MESSAGE_TYPE.RECALL; + msgInfo.content = '你撤回了一条消息'; + this.$store.commit("insertMessage", msgInfo); + }) + } + } + }) + }, + onDownloadFile(msgInfo) { + let url = JSON.parse(msgInfo.content).url; + uni.downloadFile({ + url: url, + success(res) { + if (res.statusCode === 200) { + var filePath = encodeURI(res.tempFilePath); + uni.openDocument({ + filePath: filePath, + showMenu: true + }); + } + }, + fail(e){ + console.log(e); + uni.showToast({ + title: "文件下载失败", + icon: "none" + }) + } + }); + }, + onShowMore(){ + if (this.chat.type == "GROUP") { + uni.navigateTo({ + url: "/pages/group/group-info?id="+this.group.id + }) + }else{ + uni.navigateTo({ + url: "/pages/common/user-info?id="+this.friend.id + }) } - }, loadGroup(groupId) { this.$http({ diff --git a/im-uniapp/pages/login/login.vue b/im-uniapp/pages/login/login.vue index ad8c3cc..65c535b 100644 --- a/im-uniapp/pages/login/login.vue +++ b/im-uniapp/pages/login/login.vue @@ -49,8 +49,8 @@ console.log("登录成功,自动跳转到聊天页面...") uni.setStorageSync("loginInfo", data); // 调用App.vue的初始化方法 - getApp().init(data) - // 跳转到聊天页面 + getApp().init() + // 跳转到聊天页面 uni.switchTab({ url: "/pages/chat/chat" }) diff --git a/im-uniapp/static/icon/iconfont.css b/im-uniapp/static/icon/iconfont.css index 1c4976b..e2eb2a9 100644 --- a/im-uniapp/static/icon/iconfont.css +++ b/im-uniapp/static/icon/iconfont.css @@ -1,6 +1,6 @@ @font-face { font-family: "iconfont"; /* Project id 4272106 */ - src: url('iconfont.ttf?t=1696173135884') format('truetype'); + src: url('iconfont.ttf?t=1697301725830') format('truetype'); } .iconfont { @@ -11,6 +11,14 @@ -moz-osx-font-smoothing: grayscale; } +.icon-file:before { + content: "\e671"; +} + +.icon-add:before { + content: "\e66c"; +} + .icon-warning-circle-fill:before { content: "\e848"; } @@ -19,10 +27,6 @@ content: "\e93d"; } -.icon-add-circle:before { - content: "\e664"; -} - .icon-camera:before { content: "\e600"; } diff --git a/im-uniapp/static/icon/iconfont.ttf b/im-uniapp/static/icon/iconfont.ttf index 2b19ac5..0eed9cc 100644 Binary files a/im-uniapp/static/icon/iconfont.ttf and b/im-uniapp/static/icon/iconfont.ttf differ diff --git a/im-uniapp/static/logo.png b/im-uniapp/static/logo.png deleted file mode 100644 index b5771e2..0000000 Binary files a/im-uniapp/static/logo.png and /dev/null differ diff --git a/im-uniapp/store/chatStore.js b/im-uniapp/store/chatStore.js index 9d953d9..7db922f 100644 --- a/im-uniapp/store/chatStore.js +++ b/im-uniapp/store/chatStore.js @@ -1,5 +1,8 @@ -export default { +import {MESSAGE_TYPE} from '@/common/enums.js'; +import userStore from './userStore'; +export default { + state: { chats: [] }, @@ -34,7 +37,7 @@ export default { }; state.chats.unshift(chat); } - uni.setStorageSync("chats",state.chats); + this.commit("saveToStorage"); }, activeChat(state, idx) { state.activeIndex = idx; @@ -44,7 +47,7 @@ export default { }, removeChat(state, idx) { state.chats.splice(idx, 1); - uni.setStorageSync("chats",state.chats); + this.commit("saveToStorage"); }, removeGroupChat(state, groupId) { for (let idx in state.chats) { @@ -77,11 +80,11 @@ export default { } } // 插入新的数据 - if(msgInfo.type == 1){ + if(msgInfo.type == MESSAGE_TYPE.IMAGE){ chat.lastContent = "[图片]"; - }else if(msgInfo.type == 2){ + }else if(msgInfo.type == MESSAGE_TYPE.FILE){ chat.lastContent = "[文件]"; - }else if(msgInfo.type == 3){ + }else if(msgInfo.type == MESSAGE_TYPE.AUDIO){ chat.lastContent = "[语音]"; }else{ chat.lastContent = msgInfo.content; @@ -99,18 +102,21 @@ export default { for (let idx in chat.messages) { if(msgInfo.id && chat.messages[idx].id == msgInfo.id){ Object.assign(chat.messages[idx], msgInfo); + this.commit("saveToStorage"); return; } // 正在发送中的消息可能没有id,通过发送时间判断 if(msgInfo.selfSend && chat.messages[idx].selfSend && chat.messages[idx].sendTime == msgInfo.sendTime){ Object.assign(chat.messages[idx], msgInfo); + this.commit("saveToStorage"); return; } } // 新的消息 chat.messages.push(msgInfo); - uni.setStorageSync("chats",state.chats); + console.log(chat.unreadCount) + this.commit("saveToStorage"); }, deleteMessage(state, msgInfo){ @@ -139,7 +145,7 @@ export default { break; } } - uni.setStorageSync("chats",state.chats); + this.commit("saveToStorage"); }, updateChatFromFriend(state, friend) { for (let i in state.chats) { @@ -150,7 +156,7 @@ export default { break; } } - uni.setStorageSync("chats",state.chats); + this.commit("saveToStorage"); }, updateChatFromGroup(state, group) { for (let i in state.chats) { @@ -161,14 +167,22 @@ export default { break; } } - uni.setStorageSync("chats",state.chats); + this.commit("saveToStorage"); + }, + saveToStorage(state){ + let userId = userStore.state.userInfo.id; + uni.setStorage({ + key:"chats-"+userId, + data: state.chats + }) } - }, + }, actions:{ loadChat(context) { return new Promise((resolve, reject) => { + let userId = userStore.state.userInfo.id; uni.getStorage({ - key:"chats", + key:"chats-"+userId, success(res) { context.commit("setChats",res.data); resolve() diff --git a/im-uniapp/store/groupStore.js b/im-uniapp/store/groupStore.js index bd87b57..1920270 100644 --- a/im-uniapp/store/groupStore.js +++ b/im-uniapp/store/groupStore.js @@ -44,8 +44,8 @@ export default { }).then((groups) => { context.commit("setGroups", groups); resolve(); - }).catch(() => { - reject(); + }).catch((res) => { + reject(res); }) }); } diff --git a/im-uniapp/store/index.js b/im-uniapp/store/index.js index 7496fc6..83cf159 100644 --- a/im-uniapp/store/index.js +++ b/im-uniapp/store/index.js @@ -2,7 +2,9 @@ import chatStore from './chatStore.js'; import friendStore from './friendStore.js'; import userStore from './userStore.js'; import groupStore from './groupStore.js'; -import {createStore} from 'vuex'; +import { + createStore +} from 'vuex'; const store = createStore({ modules: { chatStore, @@ -13,12 +15,14 @@ const store = createStore({ state: {}, actions: { load(context) { - const promises = []; - promises.push(this.dispatch("loadUser")); - promises.push(this.dispatch("loadFriend")); - promises.push(this.dispatch("loadGroup")); - promises.push(this.dispatch("loadChat")); - return Promise.all(promises); + return this.dispatch("loadUser").then(() => { + const promises = []; + promises.push(this.dispatch("loadFriend")); + promises.push(this.dispatch("loadGroup")); + promises.push(this.dispatch("loadChat")); + return Promise.all(promises); + }) + } }, strict: true diff --git a/im-uniapp/store/userStore.js b/im-uniapp/store/userStore.js index 5c3971f..2626691 100644 --- a/im-uniapp/store/userStore.js +++ b/im-uniapp/store/userStore.js @@ -24,10 +24,11 @@ export default { url: '/user/self', method: 'GET' }).then((userInfo) => { + console.log(userInfo) context.commit("setUserInfo",userInfo); resolve(); - }).catch(()=>{ - reject(); + }).catch((res)=>{ + reject(res); }); }) }