diff --git a/pages/products/_id.vue b/pages/products/_id.vue index e3d1540..db18826 100644 --- a/pages/products/_id.vue +++ b/pages/products/_id.vue @@ -295,7 +295,7 @@ export default { }, /**打开聊天窗口,并建立连接*/ chat(){ - this.$refs.chatFrom.show() + this.$refs.chatFrom.show(this.prodDetail.id) } } } diff --git a/pages/products/chatForm.vue b/pages/products/chatForm.vue index a6dac79..f9ee402 100644 --- a/pages/products/chatForm.vue +++ b/pages/products/chatForm.vue @@ -68,12 +68,13 @@ export default { text: null, //聊天信息 chatMain: {}, - + //产品id + productId:'', } }, beforeDestroy() { - // 离开页面生命周期函数 + // 离开页面生命周期函数 this.$store.dispatch('modules/websocket/websocket_close') }, @@ -82,6 +83,7 @@ export default { }, methods: { + /**用户列表*/ getUserList() { @@ -90,15 +92,14 @@ export default { /** * 组件显示 */ - show() { + show(id) { this.open = true - this.getChatMain() + this.getChatMain(id) }, /**获取原有聊天记录*/ - getChatMain() { - //获取当前浏览器唯一id - // this.$store.dispatch('modules/websocket/websocket_send', murmur + ',userface,hello'); + getChatMain(id) { + console.log(id,'prodId') //获取当前浏览器唯一id Fingerprint2.get((components) => { const values = components.map((component) => component.value); @@ -106,11 +107,16 @@ export default { const data = { cusCode: fingerprintHash, tenantId: 'main', + prodId:id }; // 这里使用箭头函数,确保 `this` 指向当前 Vue 实例 this.$axios.$post('/web/chatMain', data).then((res) => { if (res.id != null) { this.chatMain = res; + if (this.chatMain.jsonArray != null){ + this.message = this.chatMain.jsonArray + this.$store.dispatch('modules/websocket/set_message',this.chatMain.jsonArray); + } const websocketUrl = process.env.NUXT_ENV.VUE_APP_WEBSOCKET+`${fingerprintHash}`; // 调用Vuex dispatch,初始化WebSocket this.$store.dispatch('modules/websocket/websocket_init', websocketUrl) @@ -131,7 +137,16 @@ export default { /**弹窗关闭方法*/ close() { + console.log(this.chatMain,'this.chatMain') this.$store.dispatch('modules/websocket/websocket_close') + let data = { + id:this.chatMain.id, + jsonArray:this.message + } + //关闭时将消息保存到数据库 + this.$axios.$post('/web/saveMessage', data).then((res)=>{ + console.log(res) + }) }, /**发送消息*/ diff --git a/store/modules/websocket.js b/store/modules/websocket.js index faa2fda..32b4cb2 100644 --- a/store/modules/websocket.js +++ b/store/modules/websocket.js @@ -42,6 +42,9 @@ export const mutations = { state.socket.close(); state.socket = null; } + }, + SET_MESSAGE(state,msg){ + state.messages = msg } } @@ -54,5 +57,8 @@ export const actions = { }, websocket_close({commit}){ commit('WEBSOCKET_CLOSE') + }, + set_message({commit},msg){ + commit('SET_MESSAGE',msg) } } \ No newline at end of file