You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

340 lines
10 KiB

<script>
const keepAlive = uni.requireNativePlugin('Ba-KeepAliveSuit');
const websocket = uni.requireNativePlugin('Ba-Websocket');
import {
dateFormat
} from "@/util/date.js";
export default {
globalData: {
// apiUrl: 'http://192.168.1.102:81', //接口地址
// apiUrl: 'http://192.168.1.34:81', //接口地址
// apiUrl: 'http://171.16.8.51:81', //接口地址
// apiUrl: 'http://192.169.9.252:1883', //接口地址
apiUrl: 'http://171.16.8.53:1883', //接口地址
// mobileUrl: 'http://171.16.8.51:81', //语音调度网关地址
// mobileUrl: 'http://192.169.9.252:1883', //语音调度网关地址
// mobileUrl: 'http://172.19.2.177:8081', //语音调度网关地址
mobileUrl: 'http://171.16.8.53:1883', //语音调度网关地址
fileUrl: 'http://171.16.8.51:8080' //文件资源前缀
// fileUrl: 'http://192.168.1.34:1888' //文件资源前缀
},
data() {
return {
heartBeatEate: 10, //单位秒
employeeId: '',
message: '', //报警信息通知
timer: null,
reportLastTime: null, //报警信息最后一次时间
msgIds:[]
}
},
onLaunch: function() {
console.log('App Launch');
this.onKeep();
},
beforeDestroy() {
console.log('销毁');
this.stop();
},
onShow: function() {
//清除通知消息
this.msgIds.map(id => {
console.log('取消通知', id)
this.onCancelNotify(id);
})
this.msgIds = [];
console.log('App Show');
let userInfo = uni.getStorageSync('mobileInfo') ? JSON.parse(uni.getStorageSync('mobileInfo')) : null;
if (userInfo && userInfo.employeeId != this.employeeId) {
this.employeeId = userInfo.employeeId;
this.getStatus();
this.subStatus();
} else {
// uni.navigateTo({
// url: '/pages/setting/user'
// })
}
},
onHide: function() {
// setTimeout(() => {
// console.log('通知', new Date().getTime())
// this.onShowNotify();
// }, 1000)
// setTimeout(() => {
// console.log('通知', new Date().getTime())
// this.onShowNotify();
// }, 3000)
// console.log('App Hide', this.employeeId);
if (this.employeeId) {
this.getStatus();
this.receiveMsg();
this.subStatus(1);
} else {
let userInfo = uni.getStorageSync('mobileInfo') ? JSON.parse(uni.getStorageSync('mobileInfo')) : null;
if (userInfo && userInfo.employeeId != this.employeeId) {
this.employeeId = userInfo.employeeId;
this.getStatus();
this.receiveMsg();
this.subStatus(2);
}
}
},
methods: {
// websocket
start() {
//开启 websocket 服务
websocket.start({
// wsUrl: 'ws://192.168.3.32:81/websocket', //websocket地址
// wsUrl: 'ws://192.168.1.34:81/websocket', //websocket地址
// wsUrl: 'ws://171.16.8.51:81/websocket', //websocket地址
wsUrl: 'ws://171.16.8.53:1883/websocket', //websocket地址
deviceId: this.employeeId, //唯一标识,用于区分(注意,有值时,websocket的真实请求地址为:wsUrl+'/'+deviceId)
heartBeatEate: this.heartBeatEate, //心跳,默认3分钟,单位秒
}, res => {
console.log('start websocket', 'ws://192.168.3.32:81/websocket/' + this.employeeId);
console.log(res);
uni.showToast({
title:'start websocket,' + 'ws://192.168.3.32:81/websocket/' + this.employeeId,
icon:'none'
})
});
},
stop() {
//停止服务
websocket.stop(res => {
console.log('关闭websocket', res);
});
},
receiveMsg() {
//接收消息
websocket.receiveMsg(res => {
console.log(res);
uni.showToast({
title:'接收信息 =======>' + res.msg,
icon:'none'
})
if (res.ok && res.msg != '连接成功' && res.msg != 'header' && res.msg.indexOf('拒绝') < 0) {
let obj = JSON.parse(res.msg);
if (this.reportLastTime != obj.reportTime) {
this.reportLastTime = obj.reportTime;
this.message = obj.buildId + '号楼' + obj.floorNo + '层' + dateFormat(new Date(obj.reportTime)) +
'发生报警:' + obj
.content;
this.onShowNotify();
}
}
});
},
sendMsg() { //发送消息
websocket.sendMsg({
msg: 'header'
}, res => {
console.log(res);
});
},
subStatus(num) {
//监听 websocket 连接状态
websocket.subStatus(res => {
console.log("监听 websocket 连接状态" + num, res);
if (!res.websocketStatus) {
this.stop();
this.timer && clearTimeout(this.timer);
this.timer = setTimeout(() => {
this.start();
}, 30 * 1000);
}
});
},
getStatus(type) {
//获取 websocket 服务开启状态
websocket.getStatus(res => {
console.log("websocket开启状态", res);
if (!res.data.websocketStatus) {
this.stop();
this.timer && clearTimeout(this.timer);
this.timer = setTimeout(() => {
this.start();
}, 10 * 1000);
}
});
},
//通用保活
onKeep() {
//通用保活
keepAlive.onKeep({
title: "齐鲁医院报警通知",
content: "保持运行中...",
isRouge: true
},
(res) => {
console.log('onkeep', res);
// if (res.ok) {
// this.isIgnoringBattery();
// }
});
},
onAutoStart() { //去设置自启动、后台运行
keepAlive.onAutoStart(
(res) => {
console.log(res);
});
},
requestIgnoreBattery() { //申请加入电池优化白名单
keepAlive.requestIgnoreBattery(
res => {
console.log(res);
});
},
goIgnoreBattery() { //跳转到电池优化设置页
keepAlive.goIgnoreBattery(
res => {
console.log(res);
});
},
isIgnoringBattery() { //是否加入电池优化白名单
keepAlive.isIgnoringBattery(
res => {
console.log(res);
if (!res.data.isIgnoring) {
uni.showModal({
title: '是否忽略电池优化',
content: '忽略后才能在后台保持连接接收消息通知,请前往设置!',
showCancel: false,
confirmText: '去设置',
success: (res) => {
if (res.confirm) {
this.goIgnoreBattery();
}
}
});
}
});
},
onShowNotify() { //常驻通知保活
let id = new Date().getTime();
keepAlive.onShowNotify({
// channelId: "Ba-KeepAlive",
// channelName: "Ba-KeepAlive",
ID: id,
title: "入侵报警信息",
content: this.message,
},
(res) => {
console.log(res);
this.msgIds.push(id);
this.message = '';
});
},
onCancelNotify(id) {
//取消常驻通知保活
keepAlive.onCancelNotify({
// channelId: "Ba-KeepAlive",
// channelName: "Ba-KeepAlive",
ID:id
},
(res) => {
console.log(res);
});
},
requestAndroidPermission() {
return new Promise((resolve, reject) => {
plus.android.requestPermissions(
[
"android.permission.RECEIVE_BOOT_COMPLETED", //开机自启权限
"android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS", //忽略电池优化
],
(resultObj) => {
var result = 0;
for (var i = 0; i < resultObj.granted.length; i++) {
var grantedPermission = resultObj.granted[i];
console.log('已获取的权限:' + grantedPermission);
result = 0;
}
for (var i = 0; i < resultObj.deniedPresent.length; i++) {
var deniedPresentPermission = resultObj.deniedPresent[i];
console.log('拒绝本次申请的权限:' + deniedPresentPermission);
result = 0;
}
for (var i = 0; i < resultObj.deniedAlways.length; i++) {
var deniedAlwaysPermission = resultObj.deniedAlways[i];
console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
result = -1;
}
resolve(result);
},
(error) => {
console.log('申请权限错误:' + error.code + " = " + error.message);
resolve({
code: error.code,
message: error.message
});
}
);
});
},
// 判断通知权限
getPermis() {
var main = plus.android.runtimeMainActivity();
var pkName = main.getPackageName();
var uid = main.getApplicationInfo().plusGetAttribute("uid");
var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
//android.support.v4升级为androidx
if (NotificationManagerCompat == null) {
NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
}
var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
// 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置
console.log('areNotificationsEnabled', areNotificationsEnabled)
if (!areNotificationsEnabled) {
uni.showModal({
title: '通知权限开启提醒',
content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
showCancel: false,
confirmText: '去设置',
success: function(res) {
if (res.confirm) {
var Intent = plus.android.importClass('android.content.Intent');
var Build = plus.android.importClass("android.os.Build");
//android 8.0引导
if (Build.VERSION.SDK_INT >= 26) {
var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra("app_package", pkName);
intent.putExtra("app_uid", uid);
} else { //(<21)其他--跳转到该应用管理的详情页
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
intent.setData(uri);
}
// 跳转到该应用的系统通知设置页
main.startActivity(intent);
}
}
});
}
},
}
}
</script>
<style lang="scss">
/*每个页面公共css */
@import '@/uni_modules/uni-scss/index.scss';
/* #ifndef APP-NVUE */
@import '@/static/customicons.css';
// 设置整个项目的背景色
page {
background-color: #f5f5f5;
}
/* #endif */
.example-info {
font-size: 14px;
color: #333;
padding: 10px;
}
</style>