Skip to content

Commit 5842c05

Browse files
authored
Feature/socket (#439)
* refactor(notify): Rebuild Message Center re #436 * feat: add whether to enable websocket configuration
1 parent c252084 commit 5842c05

File tree

7 files changed

+91
-83
lines changed

7 files changed

+91
-83
lines changed

.env.development

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
ENV = 'development'
22

3-
VUE_APP_BASE_URL = 'http://localhost:5000/'
3+
VUE_APP_BASE_URL = 'http://api.s.colorful3.com/'

src/component/layout/nav-bar.vue

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
<div class="app-nav-bar">
33
<div class="nav-content">
44
<breadcrumb />
5-
<!-- 暂时放这里 -->
65
<div class="right-info">
76
<lin-notify
87
height="370"
9-
@readMessages="readMessages"
8+
:value="value"
9+
:hidden="hidden"
1010
:trigger="'click'"
11-
:messages="messages"
1211
@readAll="readAll"
1312
@viewAll="viewAll"
14-
class="lin-notify"
15-
:value="value"
16-
:hidden="hidden"
13+
:messages="messages"
14+
@readMessages="readMessages"
1715
>
1816
</lin-notify>
1917
<clear-tab></clear-tab>
@@ -24,28 +22,39 @@
2422
</template>
2523

2624
<script>
27-
import Breadcrumb from './breadcrumb'
28-
import Screenfull from './screen-full'
2925
import User from './user'
26+
import store from '@/store'
27+
import Config from '@/config'
3028
import ClearTab from './clear-tab'
31-
// import { getToken } from '@/lin/util/token'
32-
// import store from '@/store'
29+
import Breadcrumb from './breadcrumb'
30+
import Screenfull from './screen-full'
31+
import { getToken } from '@/lin/util/token'
3332
3433
export default {
3534
name: 'NavBar',
35+
data() {
36+
return {
37+
value: 0,
38+
hidden: false,
39+
messages: [],
40+
path: `//api.s.colorful3.com/ws/message?token=${getToken('access_token').split(' ')[1]}`,
41+
}
42+
},
3643
created() {
37-
// this.$connect(this.path, { format: 'json' })
38-
// this.$options.sockets.onmessage = data => {
39-
// console.log(JSON.parse(data.data))
40-
// this.messages.push(JSON.parse(data.data))
41-
// }
42-
// this.$options.sockets.onerror = err => {
43-
// console.log(err)
44-
// this.$message.error('token已过期,请重新登录')
45-
// store.dispatch('loginOut')
46-
// const { origin } = window.location
47-
// window.location.href = origin
48-
// }
44+
if (Config.websocketEnable) {
45+
this.$connect(this.path, { format: 'json' })
46+
this.$options.sockets.onmessage = data => {
47+
console.log(JSON.parse(data.data))
48+
this.messages.push(JSON.parse(data.data))
49+
}
50+
this.$options.sockets.onerror = err => {
51+
console.error(err)
52+
this.$message.error('token已过期,请重新登录')
53+
store.dispatch('loginOut')
54+
const { origin } = window.location
55+
window.location.href = origin
56+
}
57+
}
4958
},
5059
watch: {
5160
messages: {
@@ -60,14 +69,6 @@ export default {
6069
immediate: true,
6170
},
6271
},
63-
data() {
64-
return {
65-
value: 0,
66-
hidden: true,
67-
messages: [],
68-
// path: `//api.s.colorful3.com/ws/message?token=${getToken('access_token').split(' ')[1]}`,
69-
}
70-
},
7172
methods: {
7273
readAll() {
7374
console.log('点击了readAll')
@@ -89,9 +90,6 @@ export default {
8990
</script>
9091

9192
<style lang="scss" scoped>
92-
.lin-notify {
93-
margin-right: 20px;
94-
}
9593
.app-nav-bar {
9694
width: 100%;
9795
height: $navbar-height;

src/component/notify/emitter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
/* eslint-disable*/
21
class Emitter {
32
constructor() {
43
this.listeners = new Map()
54
}
65

76
addListener(label, callback, vm) {
87
if (typeof callback === 'function') {
8+
// eslint-disable-next-line no-unused-expressions
99
this.listeners.has(label) || this.listeners.set(label, [])
1010
this.listeners.get(label).push({ callback, vm })
1111
return true
@@ -20,6 +20,7 @@ class Emitter {
2020
if (listeners?.length) {
2121
index = listeners.reduce((i, listener, index) => {
2222
if (typeof listener.callback === 'function' && listener.callback === callback && listener.vm === vm) {
23+
// eslint-disable-next-line no-param-reassign
2324
i = index
2425
}
2526
return i

src/component/notify/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
/* eslint-disable*/
2-
31
/* Author: https://github.com/nathantsoi/vue-native-websocket */
42
import Notify from './notify.vue'
53
import Observer from './observer'
64
import Emitter from './emitter'
75

86
export default {
9-
install(Vue, connection, opts = {}) {
7+
install(app, connection, opts = {}) {
108
if (typeof connection === 'object') {
9+
// eslint-disable-next-line no-param-reassign
1110
opts = connection
11+
// eslint-disable-next-line no-param-reassign
1212
connection = ''
1313
}
1414
let observer = null
1515

1616
opts.$setInstance = wsInstance => {
17-
Vue.prototype.$socket = wsInstance
17+
app.config.globalProperties.$socket = wsInstance
1818
}
19-
Vue.prototype.$connect = (connectionUrl = connection, connectionOpts = opts) => {
19+
app.config.globalProperties.$connect = (connectionUrl = connection, connectionOpts = opts) => {
2020
connectionOpts.$setInstance = opts.$setInstance
2121
observer = new Observer(connectionUrl, connectionOpts)
22-
Vue.prototype.$socket = observer.WebSocket
22+
app.config.globalProperties.$socket = observer.WebSocket
2323
}
2424

25-
Vue.prototype.$disconnect = () => {
25+
app.config.globalProperties.$disconnect = () => {
2626
if (observer?.reconnection) {
2727
observer.reconnection = false
2828
}
29-
if (Vue.prototype.$socket) {
30-
Vue.prototype.$socket.close()
31-
delete Vue.prototype.$socket
29+
if (app.config.globalProperties.$socket) {
30+
app.config.globalProperties.$socket.close()
31+
delete app.config.globalProperties.$socket
3232
}
3333
}
3434
const hasProxy = typeof Proxy !== 'undefined' && typeof Proxy === 'function' && /native code/.test(Proxy.toString())
35-
Vue.component('LinNotify', Notify)
36-
Vue.mixin({
35+
app.component('LinNotify', Notify)
36+
app.mixin({
3737
created() {
3838
const vm = this
3939
const { sockets } = this.$options
@@ -70,7 +70,7 @@ export default {
7070
}
7171
}
7272
},
73-
beforeDestroy() {
73+
beforeUnmount() {
7474
if (hasProxy) {
7575
const { sockets } = this.$options
7676

src/component/notify/notify.vue

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
<template>
22
<!-- eslint-disable max-len -->
3-
<el-dropdown :trigger="trigger" :placement="placement" :hide-on-click="hideOnClick">
3+
<el-dropdown :trigger="trigger" :placement="placement" :hide-on-click="hideOnClick" style="margin-right: 20px;">
44
<div class="notify">
55
<el-badge :value="value" class="item" :hidden="hidden" :max="max" :is-dot="isDot">
66
<i :class="icon"></i>
77
</el-badge>
88
</div>
9-
<el-dropdown-menu slot="dropdown">
10-
<div class="notify-title">
11-
<p>消息提醒</p>
12-
<p class="button" @click="readAll">全部已读</p>
13-
</div>
14-
<div class="content" :style="{ 'min-height': height + 'px', 'max-height': height + 'px' }">
15-
<div
16-
class="css-nomessage"
17-
v-if="messages.length === 0"
18-
:style="{ 'min-height': height + 'px', 'max-height': height + 'px' }"
19-
>
20-
<div class="css-sumlaa">
21-
<svg width="150" height="120" viewBox="0 0 150 120" fill="currentColor">
22-
<path
23-
fill="#EBEEF5"
24-
d="M46.76 78.71a1.895 1.895 0 0 0-1.378 2.092c.13.948.94 1.648 1.904 1.635h55.468a1.882 1.882 0 0 0 1.884-1.635c.13-.95-.46-1.846-1.367-2.09a8.61 8.61 0 0 1-6.4-7.872l-2.473-20.928c-.96-7.872-6.567-14.37-14.178-16.435l-.986-.267-.113-1.014c-.24-2.106-2.01-3.696-4.11-3.696s-3.87 1.59-4.104 3.696l-.114 1.014-.98.267c-7.61 2.063-13.22 8.563-14.18 16.43L53.15 70.84c-.2 3.74-2.79 6.926-6.393 7.87zm50.61-29.155l2.482 20.982c.127 2.562 1.817 4.654 4.19 5.276a4.895 4.895 0 0 1 3.568 5.397c-.336 2.446-2.434 4.26-4.876 4.227H47.306a4.883 4.883 0 0 1-4.896-4.227 4.897 4.897 0 0 1 3.58-5.4 5.614 5.614 0 0 0 4.17-5.168l2.49-21.093c1.068-8.77 7.135-16.06 15.46-18.7.807-3.11 3.615-5.35 6.9-5.35s6.094 2.24 6.9 5.35c8.325 2.64 14.393 9.93 15.46 18.7zm-16.417 38.91c-.288 3.184-3.007 5.36-5.943 5.36-2.936 0-5.655-2.176-5.943-5.36l-2.988.27c.43 4.82 4.52 8.09 8.93 8.09s8.49-3.27 8.93-8.09l-2.99-.27z"
25-
></path>
26-
</svg>
27-
<div>还没有消息</div>
28-
</div>
9+
<template #dropdown>
10+
<el-dropdown-menu>
11+
<div class="notify-title">
12+
<p>消息提醒</p>
13+
<p class="button" @click="readAll">全部已读</p>
2914
</div>
30-
<el-dropdown-item v-for="(msg, index) in messages" :key="index" @click="readMessages(msg, index)">
31-
<slot :row="msg">
32-
<p :class="msg[props.is_read] ? 'read-messages' : 'unread-messages'">{{ msg[props.content] }}</p>
33-
<div class="sketchynformation">
34-
<p class="user">{{ msg[props.user] }}</p>
35-
<p class="date-time">{{ msg[props.time] }}</p>
15+
<div class="content" :style="{ 'min-height': height + 'px', 'max-height': height + 'px' }">
16+
<div
17+
class="css-nomessage"
18+
v-if="messages.length === 0"
19+
:style="{ 'min-height': height + 'px', 'max-height': height + 'px' }"
20+
>
21+
<div class="css-sumlaa">
22+
<svg width="150" height="120" viewBox="0 0 150 120" fill="currentColor">
23+
<path
24+
fill="#EBEEF5"
25+
d="M46.76 78.71a1.895 1.895 0 0 0-1.378 2.092c.13.948.94 1.648 1.904 1.635h55.468a1.882 1.882 0 0 0 1.884-1.635c.13-.95-.46-1.846-1.367-2.09a8.61 8.61 0 0 1-6.4-7.872l-2.473-20.928c-.96-7.872-6.567-14.37-14.178-16.435l-.986-.267-.113-1.014c-.24-2.106-2.01-3.696-4.11-3.696s-3.87 1.59-4.104 3.696l-.114 1.014-.98.267c-7.61 2.063-13.22 8.563-14.18 16.43L53.15 70.84c-.2 3.74-2.79 6.926-6.393 7.87zm50.61-29.155l2.482 20.982c.127 2.562 1.817 4.654 4.19 5.276a4.895 4.895 0 0 1 3.568 5.397c-.336 2.446-2.434 4.26-4.876 4.227H47.306a4.883 4.883 0 0 1-4.896-4.227 4.897 4.897 0 0 1 3.58-5.4 5.614 5.614 0 0 0 4.17-5.168l2.49-21.093c1.068-8.77 7.135-16.06 15.46-18.7.807-3.11 3.615-5.35 6.9-5.35s6.094 2.24 6.9 5.35c8.325 2.64 14.393 9.93 15.46 18.7zm-16.417 38.91c-.288 3.184-3.007 5.36-5.943 5.36-2.936 0-5.655-2.176-5.943-5.36l-2.988.27c.43 4.82 4.52 8.09 8.93 8.09s8.49-3.27 8.93-8.09l-2.99-.27z"
26+
></path>
27+
</svg>
28+
<div>还没有消息</div>
3629
</div>
37-
</slot>
38-
</el-dropdown-item>
39-
</div>
40-
<div class="notify-footer">
41-
<p class="viewAll" @click="viewAll">查看全部 &gt;</p>
42-
</div>
43-
</el-dropdown-menu>
30+
</div>
31+
<el-dropdown-item v-for="(msg, index) in messages" :key="index" @click="readMessages(msg, index)">
32+
<slot :row="msg">
33+
<p :class="msg[props.is_read] ? 'read-messages' : 'unread-messages'">{{ msg[props.content] }}</p>
34+
<div class="sketchynformation">
35+
<p class="user">{{ msg[props.user] }}</p>
36+
<p class="date-time">{{ msg[props.time] }}</p>
37+
</div>
38+
</slot>
39+
</el-dropdown-item>
40+
</div>
41+
<div class="notify-footer">
42+
<p class="viewAll" @click="viewAll">查看全部 &gt;</p>
43+
</div>
44+
</el-dropdown-menu>
45+
</template>
4446
</el-dropdown>
4547
</template>
4648

@@ -110,7 +112,7 @@ export default {
110112

111113
<style lang="scss" scoped>
112114
.notify {
113-
font-size: 18px;
115+
font-size: 19px;
114116
border-radius: 50%;
115117
display: flex;
116118
justify-content: center;

src/config/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Config = {
22
sidebarLevel: 3, // 侧边栏层级(无限级)限制, 默认为 3 级,可根据需求自行扩充
33
openAutoJumpOut: true, // 是否开启无操作跳出
44
defaultRoute: '/about', // 默认打开的路由
5+
websocketEnable: false, // 默认关闭 websocket
56
showSidebarSearch: true, // 默认打开侧边栏搜索
67
notLoginRoute: ['login'], // 无需登录即可访问的路由 name,
78
useFrontEndErrorMsg: false, // 默认采用后端返回异常

src/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import App from '@/app.vue'
1212
import store from '@/store'
1313
import router from '@/router'
1414

15+
import LinNotify from '@/component/notify'
1516
import LIcon from '@/component/base/icon/lin-icon'
1617
import StickyTop from '@/component/base/sticky-top/sticky-top'
1718
import SourceCode from '@/component/base/source-code/source-code'
@@ -25,6 +26,11 @@ const app = createApp(App)
2526
app.use(store)
2627
app.use(router)
2728
app.use(ElementPlus, { locale })
29+
app.use(LinNotify, {
30+
reconnection: true,
31+
reconnectionAttempts: 5,
32+
reconnectionDelay: 3000,
33+
})
2834

2935
// base 组件注册
3036
app.component('l-icon', LIcon)

0 commit comments

Comments
 (0)