diff --git a/src/assets/audio/alarm.mp3 b/src/assets/audio/alarm.mp3 new file mode 100644 index 0000000..f376e89 --- /dev/null +++ b/src/assets/audio/alarm.mp3 Binary files differ diff --git a/src/assets/audio/alarm.mp3 b/src/assets/audio/alarm.mp3 new file mode 100644 index 0000000..f376e89 --- /dev/null +++ b/src/assets/audio/alarm.mp3 Binary files differ diff --git a/src/main.js b/src/main.js index 5afd2e8..306b2e0 100644 --- a/src/main.js +++ b/src/main.js @@ -59,3 +59,29 @@ render: h => h(App) }) +// 引入音频文件 +import audio from '@/assets/audio/alarm.mp3' + +// 方法1:注册播放音频事件到Vue实例上 +Vue.prototype.playAudio = (audioName) => { + const buttonAudio = document.getElementById('eventAudio') + buttonAudio.setAttribute('src', audio) + console.log(buttonAudio) + const playPromise = buttonAudio.play() + if (playPromise) { + playPromise.then(() => { + // 音频加载成功 + console.log('音频加载成功') + }).catch((e) => { + // 音频加载失败 + console.error(e.message) + }) + } + if (buttonAudio.paused === true) { + buttonAudio.load() + } else { + buttonAudio.pause() + buttonAudio.play() + } +} + diff --git a/src/assets/audio/alarm.mp3 b/src/assets/audio/alarm.mp3 new file mode 100644 index 0000000..f376e89 --- /dev/null +++ b/src/assets/audio/alarm.mp3 Binary files differ diff --git a/src/main.js b/src/main.js index 5afd2e8..306b2e0 100644 --- a/src/main.js +++ b/src/main.js @@ -59,3 +59,29 @@ render: h => h(App) }) +// 引入音频文件 +import audio from '@/assets/audio/alarm.mp3' + +// 方法1:注册播放音频事件到Vue实例上 +Vue.prototype.playAudio = (audioName) => { + const buttonAudio = document.getElementById('eventAudio') + buttonAudio.setAttribute('src', audio) + console.log(buttonAudio) + const playPromise = buttonAudio.play() + if (playPromise) { + playPromise.then(() => { + // 音频加载成功 + console.log('音频加载成功') + }).catch((e) => { + // 音频加载失败 + console.error(e.message) + }) + } + if (buttonAudio.paused === true) { + buttonAudio.load() + } else { + buttonAudio.pause() + buttonAudio.play() + } +} + diff --git a/src/store/getters.js b/src/store/getters.js index 20a98dc..937bbf9 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -7,6 +7,8 @@ publicKey: state => state.app.publicKey, // 公钥 sid: state => state.app.sid, // 用户标识 token: state => state.user.token, // token + id: state => state.user.id, // id + account: state => state.user.account, // 账户 name: state => state.user.name, // 姓名 roleList: state => state.user.roleList, // 角色列表 roleNames: state => state.user.roleNames, // 角色名称列表 diff --git a/src/assets/audio/alarm.mp3 b/src/assets/audio/alarm.mp3 new file mode 100644 index 0000000..f376e89 --- /dev/null +++ b/src/assets/audio/alarm.mp3 Binary files differ diff --git a/src/main.js b/src/main.js index 5afd2e8..306b2e0 100644 --- a/src/main.js +++ b/src/main.js @@ -59,3 +59,29 @@ render: h => h(App) }) +// 引入音频文件 +import audio from '@/assets/audio/alarm.mp3' + +// 方法1:注册播放音频事件到Vue实例上 +Vue.prototype.playAudio = (audioName) => { + const buttonAudio = document.getElementById('eventAudio') + buttonAudio.setAttribute('src', audio) + console.log(buttonAudio) + const playPromise = buttonAudio.play() + if (playPromise) { + playPromise.then(() => { + // 音频加载成功 + console.log('音频加载成功') + }).catch((e) => { + // 音频加载失败 + console.error(e.message) + }) + } + if (buttonAudio.paused === true) { + buttonAudio.load() + } else { + buttonAudio.pause() + buttonAudio.play() + } +} + diff --git a/src/store/getters.js b/src/store/getters.js index 20a98dc..937bbf9 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -7,6 +7,8 @@ publicKey: state => state.app.publicKey, // 公钥 sid: state => state.app.sid, // 用户标识 token: state => state.user.token, // token + id: state => state.user.id, // id + account: state => state.user.account, // 账户 name: state => state.user.name, // 姓名 roleList: state => state.user.roleList, // 角色列表 roleNames: state => state.user.roleNames, // 角色名称列表 diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 265da3c..c0bf1fe 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -4,6 +4,8 @@ const user = { state: { token: getToken(), + id: '', // 用户id + account: '', // 用户名 name: '', // 姓名 deptId: '', // 部门编号 deptName: '', // 部门名称 @@ -21,6 +23,12 @@ SET_TOKEN: (state, token) => { state.token = token }, + SET_ID: (state, id) => { + state.id = id + }, + SET_ACCOUNT: (state, account) => { + state.account = account + }, SET_NAME: (state, name) => { state.name = name }, @@ -96,6 +104,8 @@ } else { reject('该用户无组织机构') } + commit('SET_ID', data.id) + commit('SET_ACCOUNT', data.account) commit('SET_NAME', data.name) resolve(response) }).catch(error => { diff --git a/src/assets/audio/alarm.mp3 b/src/assets/audio/alarm.mp3 new file mode 100644 index 0000000..f376e89 --- /dev/null +++ b/src/assets/audio/alarm.mp3 Binary files differ diff --git a/src/main.js b/src/main.js index 5afd2e8..306b2e0 100644 --- a/src/main.js +++ b/src/main.js @@ -59,3 +59,29 @@ render: h => h(App) }) +// 引入音频文件 +import audio from '@/assets/audio/alarm.mp3' + +// 方法1:注册播放音频事件到Vue实例上 +Vue.prototype.playAudio = (audioName) => { + const buttonAudio = document.getElementById('eventAudio') + buttonAudio.setAttribute('src', audio) + console.log(buttonAudio) + const playPromise = buttonAudio.play() + if (playPromise) { + playPromise.then(() => { + // 音频加载成功 + console.log('音频加载成功') + }).catch((e) => { + // 音频加载失败 + console.error(e.message) + }) + } + if (buttonAudio.paused === true) { + buttonAudio.load() + } else { + buttonAudio.pause() + buttonAudio.play() + } +} + diff --git a/src/store/getters.js b/src/store/getters.js index 20a98dc..937bbf9 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -7,6 +7,8 @@ publicKey: state => state.app.publicKey, // 公钥 sid: state => state.app.sid, // 用户标识 token: state => state.user.token, // token + id: state => state.user.id, // id + account: state => state.user.account, // 账户 name: state => state.user.name, // 姓名 roleList: state => state.user.roleList, // 角色列表 roleNames: state => state.user.roleNames, // 角色名称列表 diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 265da3c..c0bf1fe 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -4,6 +4,8 @@ const user = { state: { token: getToken(), + id: '', // 用户id + account: '', // 用户名 name: '', // 姓名 deptId: '', // 部门编号 deptName: '', // 部门名称 @@ -21,6 +23,12 @@ SET_TOKEN: (state, token) => { state.token = token }, + SET_ID: (state, id) => { + state.id = id + }, + SET_ACCOUNT: (state, account) => { + state.account = account + }, SET_NAME: (state, name) => { state.name = name }, @@ -96,6 +104,8 @@ } else { reject('该用户无组织机构') } + commit('SET_ID', data.id) + commit('SET_ACCOUNT', data.account) commit('SET_NAME', data.name) resolve(response) }).catch(error => { diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index 8b8b043..db4e774 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -63,6 +63,12 @@ DeviceCount, DeviceCountByOnline }, + data() { + return { + // wsPath: 'ws://10.9.39.8:8080/army/webSocket/' + wsPath: 'ws://192.168.0.166:14537/websocket/' + } + }, computed: { ...mapGetters([ 'name', @@ -73,6 +79,92 @@ 'communications', 'area' ]) + }, + mounted() { + this.webSocket() + }, + methods: { + webSocket() { + console.log('websocket') + const that = this + if (typeof (WebSocket) === 'undefined') { + this.$notify({ + title: '提示', + message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器或360浏览器极速模式!', + type: 'warning', + duration: 0 + }) + } else { + // 获取token保存到vuex中的用户信息,此处仅适用于本项目,注意删除或修改 + // 实例化socket,这里我把用户名传给了后台,使后台能判断要把消息发给哪个用户,其实也可以后台直接获取用户IP来判断并推送 + const socketUrl = this.wsPath + this.$store.getters.id + console.log(socketUrl) + this.socket = new WebSocket(socketUrl) + // 监听socket打开 + this.socket.onopen = function() { + console.log('浏览器WebSocket已打开') + } + // 监听socket消息接收 + this.socket.onmessage = function(msg) { + debugger + // 转换为json对象 + const data = JSON.parse(msg.data) + if (data.type === 'alarm') { + that.$notify({ + title: '新报警来了', + // 这里也可以把返回信息加入到message中显示 + message: data.message, + type: 'warning', + onClick: () => { + that.$router.push({ + path: '/overview', + query: { refresh: true } + }) + } + }) + } else if (data.type === 'overtimeGet') { + that.$notify({ + title: '有新超时未接收工单', + // 这里也可以把返回信息加入到message中显示 + message: data.message, + type: 'warning', + onClick: () => { + that.$router.push({ + path: '/listOvertimeGetJob', + query: { refresh: true } + }) + } + }) + } else if (data.type === 'overtimeHandle') { + that.$notify({ + title: '有新超时未处理工单', + // 这里也可以把返回信息加入到message中显示 + message: data.message, + type: 'warning', + onClick: () => { + that.$router.push({ + path: '/listOvertimeHandleJob', + query: { refresh: true } + }) + } + }) + } + } + // 监听socket错误 + this.socket.onerror = function() { + that.$notify({ + title: '服务器错误', + message: '无法接收实时报警信息,请检查服务器后重新刷新页面', + type: 'error', + duration: 0 + }) + } + // 监听socket关闭 + this.socket.onclose = function() { + console.log('WebSocket已关闭') + } + } + } } } diff --git a/src/assets/audio/alarm.mp3 b/src/assets/audio/alarm.mp3 new file mode 100644 index 0000000..f376e89 --- /dev/null +++ b/src/assets/audio/alarm.mp3 Binary files differ diff --git a/src/main.js b/src/main.js index 5afd2e8..306b2e0 100644 --- a/src/main.js +++ b/src/main.js @@ -59,3 +59,29 @@ render: h => h(App) }) +// 引入音频文件 +import audio from '@/assets/audio/alarm.mp3' + +// 方法1:注册播放音频事件到Vue实例上 +Vue.prototype.playAudio = (audioName) => { + const buttonAudio = document.getElementById('eventAudio') + buttonAudio.setAttribute('src', audio) + console.log(buttonAudio) + const playPromise = buttonAudio.play() + if (playPromise) { + playPromise.then(() => { + // 音频加载成功 + console.log('音频加载成功') + }).catch((e) => { + // 音频加载失败 + console.error(e.message) + }) + } + if (buttonAudio.paused === true) { + buttonAudio.load() + } else { + buttonAudio.pause() + buttonAudio.play() + } +} + diff --git a/src/store/getters.js b/src/store/getters.js index 20a98dc..937bbf9 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -7,6 +7,8 @@ publicKey: state => state.app.publicKey, // 公钥 sid: state => state.app.sid, // 用户标识 token: state => state.user.token, // token + id: state => state.user.id, // id + account: state => state.user.account, // 账户 name: state => state.user.name, // 姓名 roleList: state => state.user.roleList, // 角色列表 roleNames: state => state.user.roleNames, // 角色名称列表 diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 265da3c..c0bf1fe 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -4,6 +4,8 @@ const user = { state: { token: getToken(), + id: '', // 用户id + account: '', // 用户名 name: '', // 姓名 deptId: '', // 部门编号 deptName: '', // 部门名称 @@ -21,6 +23,12 @@ SET_TOKEN: (state, token) => { state.token = token }, + SET_ID: (state, id) => { + state.id = id + }, + SET_ACCOUNT: (state, account) => { + state.account = account + }, SET_NAME: (state, name) => { state.name = name }, @@ -96,6 +104,8 @@ } else { reject('该用户无组织机构') } + commit('SET_ID', data.id) + commit('SET_ACCOUNT', data.account) commit('SET_NAME', data.name) resolve(response) }).catch(error => { diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index 8b8b043..db4e774 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -63,6 +63,12 @@ DeviceCount, DeviceCountByOnline }, + data() { + return { + // wsPath: 'ws://10.9.39.8:8080/army/webSocket/' + wsPath: 'ws://192.168.0.166:14537/websocket/' + } + }, computed: { ...mapGetters([ 'name', @@ -73,6 +79,92 @@ 'communications', 'area' ]) + }, + mounted() { + this.webSocket() + }, + methods: { + webSocket() { + console.log('websocket') + const that = this + if (typeof (WebSocket) === 'undefined') { + this.$notify({ + title: '提示', + message: '当前浏览器无法接收实时报警信息,请使用谷歌浏览器或360浏览器极速模式!', + type: 'warning', + duration: 0 + }) + } else { + // 获取token保存到vuex中的用户信息,此处仅适用于本项目,注意删除或修改 + // 实例化socket,这里我把用户名传给了后台,使后台能判断要把消息发给哪个用户,其实也可以后台直接获取用户IP来判断并推送 + const socketUrl = this.wsPath + this.$store.getters.id + console.log(socketUrl) + this.socket = new WebSocket(socketUrl) + // 监听socket打开 + this.socket.onopen = function() { + console.log('浏览器WebSocket已打开') + } + // 监听socket消息接收 + this.socket.onmessage = function(msg) { + debugger + // 转换为json对象 + const data = JSON.parse(msg.data) + if (data.type === 'alarm') { + that.$notify({ + title: '新报警来了', + // 这里也可以把返回信息加入到message中显示 + message: data.message, + type: 'warning', + onClick: () => { + that.$router.push({ + path: '/overview', + query: { refresh: true } + }) + } + }) + } else if (data.type === 'overtimeGet') { + that.$notify({ + title: '有新超时未接收工单', + // 这里也可以把返回信息加入到message中显示 + message: data.message, + type: 'warning', + onClick: () => { + that.$router.push({ + path: '/listOvertimeGetJob', + query: { refresh: true } + }) + } + }) + } else if (data.type === 'overtimeHandle') { + that.$notify({ + title: '有新超时未处理工单', + // 这里也可以把返回信息加入到message中显示 + message: data.message, + type: 'warning', + onClick: () => { + that.$router.push({ + path: '/listOvertimeHandleJob', + query: { refresh: true } + }) + } + }) + } + } + // 监听socket错误 + this.socket.onerror = function() { + that.$notify({ + title: '服务器错误', + message: '无法接收实时报警信息,请检查服务器后重新刷新页面', + type: 'error', + duration: 0 + }) + } + // 监听socket关闭 + this.socket.onclose = function() { + console.log('WebSocket已关闭') + } + } + } } } diff --git a/src/views/overview/overview_cmap.vue b/src/views/overview/overview_cmap.vue index 247ec02..5662bc3 100644 --- a/src/views/overview/overview_cmap.vue +++ b/src/views/overview/overview_cmap.vue @@ -197,6 +197,11 @@ const that = this that.fetchWellType() }, + activated() { + // if (this.$route.query && this.$route.query.refresh) { + this.refreshAlarm() + // } + }, methods: { // 地图准备好后的操作 readyMap() {