Layout.vue
6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<template>
<div :class="classObj" class="app-wrapper">
<div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside"/>
<sidebar class="sidebar-container"/>
<div class="main-container">
<navbar/>
<app-main/>
</div>
</div>
</template>
<script>
import { Navbar, Sidebar, AppMain } from './components'
import ResizeMixin from './mixin/ResizeHandler'
export default {
name: 'Layout',
components: {
Navbar,
Sidebar,
AppMain
},
mixins: [ResizeMixin],
computed: {
sidebar() {
return this.$store.state.app.sidebar
},
device() {
return this.$store.state.app.device
},
classObj() {
return {
hideSidebar: !this.sidebar.opened,
openSidebar: this.sidebar.opened,
withoutAnimation: this.sidebar.withoutAnimation,
mobile: this.device === 'mobile'
}
}
},
methods: {
handleClickOutside() {
this.$store.dispatch('closeSideBar', { withoutAnimation: false })
},
notificationFn(data){
const _this = this;
// var notification = new Notification('有个竞价单超过3小时无人报价', {
// body: data.orderSn+' , '+data.shopName + ' '+data.serviceTypes,
// icon: 'favicon.ico'
// });
// notification.onclick = function () {
// console.log('点击');
// _this.$router.push({
// path: '/order/order',
// })
// notification.close();
// };
// return
console.log(data,'web data from server')
if (window.Notification) {
var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf('safari') != -1) {
if (ua.indexOf('chrome') > -1 || ua.indexOf('firefox') > -1) {
// Chrome
Notification.requestPermission().then(function (permission) {
console.log(permission,'Notification');
if (permission == "granted") {
var notification = new Notification('有个竞价单超过'+data.hour+'小时无人报价', {
body: data.orderSn+' , '+data.shopName + ' '+data.serviceTypes,
icon: 'favicon.ico'
});
notification.onclick = function () {
console.log('点击');
_this.$router.push({
path: '/order/bidding',
})
notification.close();
};
} else {
Notification.requestPermission();
console.log('没有权限,用户拒绝:Notification');
}
});
} else {
// Safari
Notification.requestPermission(function (permission) {
if (permission == "granted") {
var notification = new Notification('有个竞价单超过'+data.hour+'小时无人报价', {
body: data.orderSn+' , '+data.shopName + ' '+data.serviceTypes,
icon: 'favicon.ico'
});
notification.onclick = function () {
console.log('点击');
notification.close();
};
} else {
Notification.requestPermission();
console.log('没有权限,用户拒绝:Notification');
}
})
}
}
} else {
console.log('不支持Notification');
}
},
initWebSocket(){
//浏览器消息通知弹框
// this.notificationFn()
const that = this;
if (typeof (WebSocket) == 'undefined') {
this.$notify({
title: '提示',
message: '当前浏览器无法接收实时通知信息,请使用谷歌浏览器!',
type: 'warning',
position: 'bottom-right',
duration: 0
});
} else {
// 实例化socket,这里我把用户名传给了后台,使后台能判断要把消息发给哪个用户,其实也可以后台直接获取用户IP来判断并推送
const socketUrl = process.env.BASE_WS+'/carservice/usercenter/websocket/'+this.$store.getters.navbarId;
this.socket = new WebSocket(socketUrl);
//心跳测试
const heartCheck = {
timeout:5000,
timeObj:null,
reset:function(){
clearInterval(this.timeObj);
return this;
},
start:function(){
this.timeObj = setInterval(function(){
that.socket.send('1');
},this.timeout)
}
}
// 监听socket打开
this.socket.onopen = function() {
console.log('浏览器WebSocket已打开');
heartCheck.reset().start();
};
// 监听socket消息接收
this.socket.onmessage = function(msg) {
// 转换为json对象
const data = JSON.parse(msg.data);
if(!data||data.code != 200) return;
that.notificationFn(data.data)
};
// 监听socket错误
this.socket.onerror = function() {
that.$notify({
title: '错误',
message: '服务器連接错误,无法接收实时通知信息',
type: 'error',
position: 'bottom-right',
duration: 0
});
};
// 监听socket关闭
this.socket.onclose = function() {
console.log('WebSocket已关闭');
}
}
},
},
created(){
//启动websocket
if(!this.$store.getters.isOpenWebsocket&&this.$store.getters.navbarType === 0){
this.$store.dispatch('SetISWebsocket',true);
// this.initWebSocket();
}
}
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "~@/styles/mixin.scss";
.app-wrapper {
@include clearfix;
position: relative;
height: 100%;
width: 100%;
&.mobile.openSidebar{
position: fixed;
top: 0;
}
}
.drawer-bg {
background: #000;
opacity: 0.3;
width: 100%;
top: 0;
height: 100%;
position: absolute;
z-index: 999;
}
</style>