diff --git a/src/components/DeptSelect/index.vue b/src/components/DeptSelect/index.vue index 4a720ee..2d45018 100644 --- a/src/components/DeptSelect/index.vue +++ b/src/components/DeptSelect/index.vue @@ -45,6 +45,7 @@ }, // 接收绑定参数 data() { return { + originList: [], // 原始数据 deptTreeList: [], multiData: false, defaultProps: { @@ -80,6 +81,7 @@ } getDeptTreeList(listQuery).then(response => { const list = response.data.list + this.originList = [...response.data.list] if (!this.deptShow && list.length <= 1) { this.showDeptSelect = false } else { @@ -100,6 +102,10 @@ } } }) + }, + // 获取列表 + fetchDeptTree() { + return this.originList } } } diff --git a/src/components/DeptSelect/index.vue b/src/components/DeptSelect/index.vue index 4a720ee..2d45018 100644 --- a/src/components/DeptSelect/index.vue +++ b/src/components/DeptSelect/index.vue @@ -45,6 +45,7 @@ }, // 接收绑定参数 data() { return { + originList: [], // 原始数据 deptTreeList: [], multiData: false, defaultProps: { @@ -80,6 +81,7 @@ } getDeptTreeList(listQuery).then(response => { const list = response.data.list + this.originList = [...response.data.list] if (!this.deptShow && list.length <= 1) { this.showDeptSelect = false } else { @@ -100,6 +102,10 @@ } } }) + }, + // 获取列表 + fetchDeptTree() { + return this.originList } } } diff --git a/src/store/getters.js b/src/store/getters.js index 8846aab..c5581c6 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -24,6 +24,9 @@ communications: state => state.user.communications, // 用户支持的通讯方式 area: state => state.user.area, // 用户所在区域 lng: state => state.user.lng, // 纬度 - lat: state => state.user.lat // 经度 + lat: state => state.user.lat, // 经度 + websocket: state => state.websocket.websocket, // websocket对象 + wsStatus: state => state.websocket.wsStatus, // websocket连接状态 + needRefresh: state => state.websocket.needRefresh // 是否需要刷新 } export default getters diff --git a/src/components/DeptSelect/index.vue b/src/components/DeptSelect/index.vue index 4a720ee..2d45018 100644 --- a/src/components/DeptSelect/index.vue +++ b/src/components/DeptSelect/index.vue @@ -45,6 +45,7 @@ }, // 接收绑定参数 data() { return { + originList: [], // 原始数据 deptTreeList: [], multiData: false, defaultProps: { @@ -80,6 +81,7 @@ } getDeptTreeList(listQuery).then(response => { const list = response.data.list + this.originList = [...response.data.list] if (!this.deptShow && list.length <= 1) { this.showDeptSelect = false } else { @@ -100,6 +102,10 @@ } } }) + }, + // 获取列表 + fetchDeptTree() { + return this.originList } } } diff --git a/src/store/getters.js b/src/store/getters.js index 8846aab..c5581c6 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -24,6 +24,9 @@ communications: state => state.user.communications, // 用户支持的通讯方式 area: state => state.user.area, // 用户所在区域 lng: state => state.user.lng, // 纬度 - lat: state => state.user.lat // 经度 + lat: state => state.user.lat, // 经度 + websocket: state => state.websocket.websocket, // websocket对象 + wsStatus: state => state.websocket.wsStatus, // websocket连接状态 + needRefresh: state => state.websocket.needRefresh // 是否需要刷新 } export default getters diff --git a/src/store/index.js b/src/store/index.js index c7e23ff..f55c01c 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -4,6 +4,7 @@ import user from './modules/user' import permission from './modules/permission' import tagsView from './modules/tagsView' +import websocket from './modules/websocket' import getters from './getters' Vue.use(Vuex) @@ -13,7 +14,8 @@ app, user, permission, - tagsView + tagsView, + websocket }, getters }) diff --git a/src/components/DeptSelect/index.vue b/src/components/DeptSelect/index.vue index 4a720ee..2d45018 100644 --- a/src/components/DeptSelect/index.vue +++ b/src/components/DeptSelect/index.vue @@ -45,6 +45,7 @@ }, // 接收绑定参数 data() { return { + originList: [], // 原始数据 deptTreeList: [], multiData: false, defaultProps: { @@ -80,6 +81,7 @@ } getDeptTreeList(listQuery).then(response => { const list = response.data.list + this.originList = [...response.data.list] if (!this.deptShow && list.length <= 1) { this.showDeptSelect = false } else { @@ -100,6 +102,10 @@ } } }) + }, + // 获取列表 + fetchDeptTree() { + return this.originList } } } diff --git a/src/store/getters.js b/src/store/getters.js index 8846aab..c5581c6 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -24,6 +24,9 @@ communications: state => state.user.communications, // 用户支持的通讯方式 area: state => state.user.area, // 用户所在区域 lng: state => state.user.lng, // 纬度 - lat: state => state.user.lat // 经度 + lat: state => state.user.lat, // 经度 + websocket: state => state.websocket.websocket, // websocket对象 + wsStatus: state => state.websocket.wsStatus, // websocket连接状态 + needRefresh: state => state.websocket.needRefresh // 是否需要刷新 } export default getters diff --git a/src/store/index.js b/src/store/index.js index c7e23ff..f55c01c 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -4,6 +4,7 @@ import user from './modules/user' import permission from './modules/permission' import tagsView from './modules/tagsView' +import websocket from './modules/websocket' import getters from './getters' Vue.use(Vuex) @@ -13,7 +14,8 @@ app, user, permission, - tagsView + tagsView, + websocket }, getters }) diff --git a/src/store/modules/websocket.js b/src/store/modules/websocket.js new file mode 100644 index 0000000..63c87c9 --- /dev/null +++ b/src/store/modules/websocket.js @@ -0,0 +1,88 @@ +import { Notification } from 'element-ui' +import router from '../../router' +import user from './user' +const websocket = { + state: { + wsUrl: 'ws://111.198.10.15:11307/websocket/', + websocket: null, + wsStatus: false, // websocket连接状态 + needRefresh: false // 是否需要刷新数据 + }, + mutations: { + SET_WEBSOCKET: (state, websocket) => { + state.websocket = websocket + }, + SET_WS_STATUS: (state, wsStatus) => { + state.wsStatus = wsStatus + }, + SET_NEED_REFRESH: (state, needRefresh) => { + state.needRefresh = needRefresh + } + }, + + actions: { + // 连接websocket + initWebSocket({ commit }) { + if (typeof (WebSocket) === 'undefined') { + Notification({ + title: '提示', + message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器或360浏览器极速模式!', + type: 'warning', + duration: 0 + }) + } else { + // 获取token保存到vuex中的用户信息,此处仅适用于本项目,注意删除或修改 + // 实例化socket,这里我把用户名传给了后台,使后台能判断要把消息发给哪个用户,其实也可以后台直接获取用户IP来判断并推送 + const socketUrl = websocket.state.wsUrl + user.state.id + console.log(socketUrl) + const socket = new WebSocket(socketUrl) + commit('SET_WEBSOCKET', socket) + commit('SET_WS_STATUS', true) + // 监听socket打开 + socket.onopen = function() { + console.log('浏览器WebSocket已打开') + } + // 监听socket消息接收 + socket.onmessage = function(msg) { + // 转换为json对象 + const data = JSON.parse(msg.data) + console.log(data) + if (data.type === 'alarm') { + commit('SET_NEED_REFRESH', true) + Notification({ + title: '新报警来了', + // 这里也可以把返回信息加入到message中显示 + message: data.message, + type: 'warning', + onClick: () => { + router.push({ + path: '/overview', + query: { refresh: true } + }) + } + }) + setTimeout(function() { + commit('SET_NEED_REFRESH', false) + }, 2000) + } + } + // 监听socket错误 + socket.onerror = function() { + Notification({ + title: '服务器错误', + message: '无法接收实时报警信息,请检查服务器后重新刷新页面', + type: 'error', + duration: 0 + }) + } + // 监听socket关闭 + socket.onclose = function() { + console.log('WebSocket已关闭') + commit('SET_WS_STATUS', false) + } + } + } + } +} + +export default websocket diff --git a/src/components/DeptSelect/index.vue b/src/components/DeptSelect/index.vue index 4a720ee..2d45018 100644 --- a/src/components/DeptSelect/index.vue +++ b/src/components/DeptSelect/index.vue @@ -45,6 +45,7 @@ }, // 接收绑定参数 data() { return { + originList: [], // 原始数据 deptTreeList: [], multiData: false, defaultProps: { @@ -80,6 +81,7 @@ } getDeptTreeList(listQuery).then(response => { const list = response.data.list + this.originList = [...response.data.list] if (!this.deptShow && list.length <= 1) { this.showDeptSelect = false } else { @@ -100,6 +102,10 @@ } } }) + }, + // 获取列表 + fetchDeptTree() { + return this.originList } } } diff --git a/src/store/getters.js b/src/store/getters.js index 8846aab..c5581c6 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -24,6 +24,9 @@ communications: state => state.user.communications, // 用户支持的通讯方式 area: state => state.user.area, // 用户所在区域 lng: state => state.user.lng, // 纬度 - lat: state => state.user.lat // 经度 + lat: state => state.user.lat, // 经度 + websocket: state => state.websocket.websocket, // websocket对象 + wsStatus: state => state.websocket.wsStatus, // websocket连接状态 + needRefresh: state => state.websocket.needRefresh // 是否需要刷新 } export default getters diff --git a/src/store/index.js b/src/store/index.js index c7e23ff..f55c01c 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -4,6 +4,7 @@ import user from './modules/user' import permission from './modules/permission' import tagsView from './modules/tagsView' +import websocket from './modules/websocket' import getters from './getters' Vue.use(Vuex) @@ -13,7 +14,8 @@ app, user, permission, - tagsView + tagsView, + websocket }, getters }) diff --git a/src/store/modules/websocket.js b/src/store/modules/websocket.js new file mode 100644 index 0000000..63c87c9 --- /dev/null +++ b/src/store/modules/websocket.js @@ -0,0 +1,88 @@ +import { Notification } from 'element-ui' +import router from '../../router' +import user from './user' +const websocket = { + state: { + wsUrl: 'ws://111.198.10.15:11307/websocket/', + websocket: null, + wsStatus: false, // websocket连接状态 + needRefresh: false // 是否需要刷新数据 + }, + mutations: { + SET_WEBSOCKET: (state, websocket) => { + state.websocket = websocket + }, + SET_WS_STATUS: (state, wsStatus) => { + state.wsStatus = wsStatus + }, + SET_NEED_REFRESH: (state, needRefresh) => { + state.needRefresh = needRefresh + } + }, + + actions: { + // 连接websocket + initWebSocket({ commit }) { + if (typeof (WebSocket) === 'undefined') { + Notification({ + title: '提示', + message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器或360浏览器极速模式!', + type: 'warning', + duration: 0 + }) + } else { + // 获取token保存到vuex中的用户信息,此处仅适用于本项目,注意删除或修改 + // 实例化socket,这里我把用户名传给了后台,使后台能判断要把消息发给哪个用户,其实也可以后台直接获取用户IP来判断并推送 + const socketUrl = websocket.state.wsUrl + user.state.id + console.log(socketUrl) + const socket = new WebSocket(socketUrl) + commit('SET_WEBSOCKET', socket) + commit('SET_WS_STATUS', true) + // 监听socket打开 + socket.onopen = function() { + console.log('浏览器WebSocket已打开') + } + // 监听socket消息接收 + socket.onmessage = function(msg) { + // 转换为json对象 + const data = JSON.parse(msg.data) + console.log(data) + if (data.type === 'alarm') { + commit('SET_NEED_REFRESH', true) + Notification({ + title: '新报警来了', + // 这里也可以把返回信息加入到message中显示 + message: data.message, + type: 'warning', + onClick: () => { + router.push({ + path: '/overview', + query: { refresh: true } + }) + } + }) + setTimeout(function() { + commit('SET_NEED_REFRESH', false) + }, 2000) + } + } + // 监听socket错误 + socket.onerror = function() { + Notification({ + title: '服务器错误', + message: '无法接收实时报警信息,请检查服务器后重新刷新页面', + type: 'error', + duration: 0 + }) + } + // 监听socket关闭 + socket.onclose = function() { + console.log('WebSocket已关闭') + commit('SET_WS_STATUS', false) + } + } + } + } +} + +export default websocket diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index d875d75..3e52c4d 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -73,6 +73,14 @@ 'communications', 'area' ]) + }, + mounted() { + this.webSocket() + }, + methods: { + webSocket() { + this.$store.dispatch('initWebSocket') + } } } diff --git a/src/components/DeptSelect/index.vue b/src/components/DeptSelect/index.vue index 4a720ee..2d45018 100644 --- a/src/components/DeptSelect/index.vue +++ b/src/components/DeptSelect/index.vue @@ -45,6 +45,7 @@ }, // 接收绑定参数 data() { return { + originList: [], // 原始数据 deptTreeList: [], multiData: false, defaultProps: { @@ -80,6 +81,7 @@ } getDeptTreeList(listQuery).then(response => { const list = response.data.list + this.originList = [...response.data.list] if (!this.deptShow && list.length <= 1) { this.showDeptSelect = false } else { @@ -100,6 +102,10 @@ } } }) + }, + // 获取列表 + fetchDeptTree() { + return this.originList } } } diff --git a/src/store/getters.js b/src/store/getters.js index 8846aab..c5581c6 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -24,6 +24,9 @@ communications: state => state.user.communications, // 用户支持的通讯方式 area: state => state.user.area, // 用户所在区域 lng: state => state.user.lng, // 纬度 - lat: state => state.user.lat // 经度 + lat: state => state.user.lat, // 经度 + websocket: state => state.websocket.websocket, // websocket对象 + wsStatus: state => state.websocket.wsStatus, // websocket连接状态 + needRefresh: state => state.websocket.needRefresh // 是否需要刷新 } export default getters diff --git a/src/store/index.js b/src/store/index.js index c7e23ff..f55c01c 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -4,6 +4,7 @@ import user from './modules/user' import permission from './modules/permission' import tagsView from './modules/tagsView' +import websocket from './modules/websocket' import getters from './getters' Vue.use(Vuex) @@ -13,7 +14,8 @@ app, user, permission, - tagsView + tagsView, + websocket }, getters }) diff --git a/src/store/modules/websocket.js b/src/store/modules/websocket.js new file mode 100644 index 0000000..63c87c9 --- /dev/null +++ b/src/store/modules/websocket.js @@ -0,0 +1,88 @@ +import { Notification } from 'element-ui' +import router from '../../router' +import user from './user' +const websocket = { + state: { + wsUrl: 'ws://111.198.10.15:11307/websocket/', + websocket: null, + wsStatus: false, // websocket连接状态 + needRefresh: false // 是否需要刷新数据 + }, + mutations: { + SET_WEBSOCKET: (state, websocket) => { + state.websocket = websocket + }, + SET_WS_STATUS: (state, wsStatus) => { + state.wsStatus = wsStatus + }, + SET_NEED_REFRESH: (state, needRefresh) => { + state.needRefresh = needRefresh + } + }, + + actions: { + // 连接websocket + initWebSocket({ commit }) { + if (typeof (WebSocket) === 'undefined') { + Notification({ + title: '提示', + message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器或360浏览器极速模式!', + type: 'warning', + duration: 0 + }) + } else { + // 获取token保存到vuex中的用户信息,此处仅适用于本项目,注意删除或修改 + // 实例化socket,这里我把用户名传给了后台,使后台能判断要把消息发给哪个用户,其实也可以后台直接获取用户IP来判断并推送 + const socketUrl = websocket.state.wsUrl + user.state.id + console.log(socketUrl) + const socket = new WebSocket(socketUrl) + commit('SET_WEBSOCKET', socket) + commit('SET_WS_STATUS', true) + // 监听socket打开 + socket.onopen = function() { + console.log('浏览器WebSocket已打开') + } + // 监听socket消息接收 + socket.onmessage = function(msg) { + // 转换为json对象 + const data = JSON.parse(msg.data) + console.log(data) + if (data.type === 'alarm') { + commit('SET_NEED_REFRESH', true) + Notification({ + title: '新报警来了', + // 这里也可以把返回信息加入到message中显示 + message: data.message, + type: 'warning', + onClick: () => { + router.push({ + path: '/overview', + query: { refresh: true } + }) + } + }) + setTimeout(function() { + commit('SET_NEED_REFRESH', false) + }, 2000) + } + } + // 监听socket错误 + socket.onerror = function() { + Notification({ + title: '服务器错误', + message: '无法接收实时报警信息,请检查服务器后重新刷新页面', + type: 'error', + duration: 0 + }) + } + // 监听socket关闭 + socket.onclose = function() { + console.log('WebSocket已关闭') + commit('SET_WS_STATUS', false) + } + } + } + } +} + +export default websocket diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index d875d75..3e52c4d 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -73,6 +73,14 @@ 'communications', 'area' ]) + }, + mounted() { + this.webSocket() + }, + methods: { + webSocket() { + this.$store.dispatch('initWebSocket') + } } } diff --git a/src/views/overview/overview.vue b/src/views/overview/overview.vue index 7bb3a37..df08f76 100644 --- a/src/views/overview/overview.vue +++ b/src/views/overview/overview.vue @@ -1,3 +1,4 @@ +