-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
130 lines (126 loc) · 3.41 KB
/
App.vue
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
<script>
import ws from '@/websocket/ws.js'
import HttpClient from '@/HttpClient.js'
import dateUtil from '@/utils/date.util.js';
import mqtt from '@/utils/mqtt.js'
import {
mapGetters,
mapState
} from 'vuex'
var _this;
var wsConnect = false;
export default {
computed: {
...mapGetters(['lable']),
...mapState(['token', 'IPAddress', 'homeData'])
},
onLaunch: function() {
_this = this;
_this.wsConnect = wsConnect;
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
mounted() {
uni.$on('business', res => {
let str = res.data;
if (str === 'login') {
_this.connect();
}
if (str === 'logout') {
_this.unconnect();
}
});
},
methods: {
connect: function() {
uni.connectSocket({
url: HttpClient.getWsIpAddress(),
header: {
'content-type': 'application/json'
},
method: 'GET'
});
uni.onSocketOpen(function(res) {
uni.showToast({
title: '连接成功',
duration: 1000
});
_this.wsConnect = true;
console.log('WebSocket连接已打开!');
_this.subscribe();
});
},
subscribe: function() {
uni.onSocketMessage(function(res) {
let data = JSON.parse(res.data).data;
uni.$emit('websocket', {
data: data
});
_this.android_addLocalNotification(data);
});
},
onSocketError: function() {
uni.onSocketError(function(res) {
_this.wsConnect = false;
uni.showToast({
title: '连接失败',
duration: 1000
});
_this.connect();
let timer = setTimeout(() => {
if (_this.wsConnect) {
uni.showToast({
title: '重连成功',
duration: 1000
});
} else {
uni.showToast({
title: '重连失败',
duration: 1000
});
}
if (timer) clearTimeout(timer);
}, 3000)
console.log('WebSocket连接打开失败,请检查!');
});
},
android_addLocalNotification(data) {
// uni.runtime.setBadgeNumber()
const date = new Date(data.time_norm);
const jyJPush = uni.requireNativePlugin('JY-JPush');
jyJPush.android_addLocalNotification({
builderId: "1", // builderId 编号,自己定义,如果不管,可以全部传递1
content: __this.getLaleText(item.alarmClass), // 标签
title: data.device_name, // 摄像头名称
notificationId: "1", // 消息ID,需要为数字,后续可以通过这个取消,
year: dateUtil.getFullYear(date), // 预约发送的时间,若小于当前时间,则立即发送;若大于当前时间,则预约时间,时间到了就发送;但是APP需要在前台
month: dateUtil.getMonth(date),
day: dateUtil.getDay(date),
hour: dateUtil.dateFormatH(date),
minute: dateUtil.getMinutes(date),
second: dateUtil.getSeconds(date)
}, result => {});
},
addJYJPushReceiveOpenNotificationListener() {
jyJPush.addJYJPushReceiveOpenNotificationListener(result => {
// 监听成功后,若点击推送消息,会触发result;数据格式保持极光返回的安卓/iOS数据一致
uni.navigateTo({
url: 'pages/event/event-detail/event-detail'
});
});
},
getLaleText(alarmClass) { // 将label转换成文字
return alarmClass.map(item => {
return _this.lable.getValueByLable[item];
}).join(',')
}
}
}
</script>
<style>
@import '@/common/qiun.css';
</style>