{{ $t('webmsg.web_msg') }}
@@ -49,17 +48,10 @@
-
+
-
+
@@ -81,7 +73,8 @@ export default {
total: 0
},
timer: null,
- count: 0
+ count: 0,
+ loading: false
}
},
computed: {
@@ -101,14 +94,23 @@ export default {
// 先加载消息类型
loadMsgTypes()
this.queryCount()
+ // this.search()
// 每30s定时刷新拉取消息
- this.timer = setInterval(() => {
+ /* this.timer = setInterval(() => {
this.queryCount()
- }, 30000)
+ }, 30000) */
},
mounted() {
bus.$on('refresh-top-notification', () => {
- this.search()
+ if (this.visible) this.search()
+ else this.queryCount()
+ })
+
+ bus.$on('web-msg-topic-call', msg => {
+ console.log('收到websocket消息')
+ this.count = (this.count || this.paginationConfig.total) + 1
+ // this.queryCount()
+ // this.search()
})
},
beforeDestroy() {
@@ -195,6 +197,7 @@ export default {
})
},
search() {
+ this.loading = true
const param = {
status: false,
orders: [' create_time desc ']
@@ -204,7 +207,9 @@ export default {
this.data = response.data.listObject
this.paginationConfig.total = response.data.itemCount
this.count = this.paginationConfig.total
+ this.loading = false
}).catch(() => {
+ this.loading = false
const token = getToken()
if (!token || token === 'null' || token === 'undefined') {
this.timer && clearInterval(this.timer)
diff --git a/frontend/src/main.js b/frontend/src/main.js
index fa7049b608..13e4e465ac 100644
--- a/frontend/src/main.js
+++ b/frontend/src/main.js
@@ -25,6 +25,7 @@ import '@/components/canvas/custom-component' // 注册自定义组件
import '@/utils/DateUtil'
import draggable from 'vuedraggable'
+import deWebsocket from '@/websocket'
Vue.config.productionTip = false
Vue.use(VueClipboard)
Vue.use(widgets)
@@ -113,6 +114,7 @@ Vue.prototype.checkPermission = function(pers) {
})
return hasPermission
}
+Vue.use(deWebsocket)
new Vue({
router,
diff --git a/frontend/src/permission.js b/frontend/src/permission.js
index c1d473f23b..e06dc31f53 100644
--- a/frontend/src/permission.js
+++ b/frontend/src/permission.js
@@ -19,6 +19,8 @@ import {
import Layout from '@/layout/index'
// import bus from './utils/bus'
+import { getSocket } from '@/websocket'
+
NProgress.configure({
showSpinner: false
}) // NProgress Configuration
@@ -57,6 +59,8 @@ router.beforeEach(async(to, from, next) => {
if (store.getters.roles.length === 0) { // 判断当前用户是否已拉取完user_info信息
// get user info
store.dispatch('user/getInfo').then(() => {
+ const deWebsocket = getSocket()
+ deWebsocket && deWebsocket.reconnect && deWebsocket.reconnect()
store.dispatch('lic/getLicInfo').then(() => {
loadMenus(next, to)
}).catch(() => {
diff --git a/frontend/src/websocket/index.js b/frontend/src/websocket/index.js
new file mode 100644
index 0000000000..7787e1ba58
--- /dev/null
+++ b/frontend/src/websocket/index.js
@@ -0,0 +1,94 @@
+import bus from '@/utils/bus'
+import SockJS from 'sockjs-client'
+import Stomp from 'stompjs'
+import store from '@/store'
+
+class DeWebsocket {
+ constructor() {
+ this.ws_url = '/websocket'
+ this.client = null
+ this.channels = [
+ {
+ topic: '/web-msg-topic',
+ event: 'web-msg-topic-call'
+ }
+ ]
+ this.timer = null
+ this.initialize()
+ }
+
+ initialize() {
+ this.connection()
+ const _this = this
+ this.timer = this.isLoginStatu() && setInterval(() => {
+ this.isLoginStatu() || this.destroy()
+ try {
+ _this.client && _this.client.send('heart detection')
+ } catch (error) {
+ console.log('Disconnection reconnection...')
+ _this.connection()
+ }
+ }, 5000)
+ }
+
+ destroy() {
+ this.timer && clearInterval(this.timer)
+ this.disconnect()
+ return true
+ }
+
+ reconnect() {
+ this.initialize()
+ }
+
+ isLoginStatu() {
+ return store.state && store.state.user && store.state.user.user && store.state.user.user.userId
+ }
+
+ connection() {
+ const socket = new SockJS(this.ws_url)
+ /* const socket = new SockJS('http://localhost:8081' + this.ws_url) */
+ if (!this.isLoginStatu()) {
+ return
+ }
+ this.client = Stomp.over(socket)
+ const heads = {
+ /* Authorization: '', */
+ userId: store.state.user.user.userId
+ }
+
+ this.client.connect(
+ heads,
+ res => {
+ // 连接成功 订阅所有主题
+ this.subscribe()
+ },
+ err => {
+ // 连接失败 打印错误信息
+ console.error(err)
+ }
+ ).bind(this)
+ }
+ subscribe() {
+ this.channels.forEach(channel => {
+ this.client.subscribe('/user/' + store.state.user.user.userId + channel.topic, res => {
+ res && res.body && bus.$emit(channel.event, res.body)
+ })
+ })
+ }
+ disconnect() {
+ this.client && this.client.disconnect()
+ }
+}
+
+const result = new DeWebsocket()
+export const getSocket = () => {
+ return result
+}
+export default {
+ install(Vue) {
+ // 使用$$前缀,避免与Element UI的冲突
+ Vue.prototype.$deWebsocket = result
+ }
+}
+
diff --git a/frontend/vue.config.js b/frontend/vue.config.js
index 9f40bcca0e..34b4f12eb4 100644
--- a/frontend/vue.config.js
+++ b/frontend/vue.config.js
@@ -20,7 +20,7 @@ module.exports = {
proxy: {
'^(?!/login)': {
target: 'http://localhost:8081/',
- ws: false
+ ws: true
}
},
open: true,