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.
320 lines
9.8 KiB
320 lines
9.8 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.3.32:81', //接口地址 |
|
mobileUrl: 'http://172.19.2.177:8081', //语音调度网关地址 |
|
fileUrl: 'http://192.168.3.32:1888' //文件资源前缀 |
|
}, |
|
data() { |
|
return { |
|
heartBeatEate: 60, //单位秒 |
|
employeeId: '', |
|
message: '', //报警信息通知 |
|
timer: null, |
|
reportLastTime: null,//报警信息最后一次时间 |
|
} |
|
}, |
|
onLaunch: function() { |
|
console.log('App Launch'); |
|
/********设置开机自动启动开始********/ |
|
this.requestAndroidPermission().then(res => { |
|
if (res == 0) { |
|
let userInfo = uni.getStorageSync('mobileInfo') ? JSON.parse(uni.getStorageSync('mobileInfo')) : null; |
|
if (userInfo && userInfo.employeeId != this.employeeId) { |
|
this.employeeId = userInfo.employeeId; |
|
// this.getStatus(); |
|
} else { |
|
uni.navigateTo({ |
|
url: '/pages/setting/user' |
|
}) |
|
} |
|
} |
|
}); |
|
/********设置开机自动启动结束********/ |
|
}, |
|
beforeDestroy() { |
|
console.log('销毁'); |
|
weboscket.stop(); |
|
}, |
|
onShow: function() { |
|
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(); |
|
} |
|
if (keepAlive) { |
|
console.log('清除消息'); |
|
this.onCancelNotify(); |
|
} |
|
}, |
|
onHide: function() { |
|
console.log('App Hide'); |
|
if(this.employeeId) { |
|
this.receiveMsg(); |
|
this.getStatus(1); |
|
} |
|
}, |
|
methods: { |
|
// websocket |
|
start() { |
|
//开启 websocket 服务 |
|
websocket.start({ |
|
wsUrl: 'ws://192.168.3.32:81/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); |
|
}); |
|
}, |
|
stop() { |
|
//停止服务 |
|
websocket.stop(res => { |
|
console.log('关闭websocket',res); |
|
}); |
|
}, |
|
receiveMsg() { |
|
//接收消息 |
|
websocket.receiveMsg(res => { |
|
console.log(res); |
|
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() { |
|
//监听 websocket 连接状态 |
|
console.log('监听状态') |
|
websocket.subStatus(res => { |
|
console.log("监听 websocket 连接状态", res); |
|
if (!res.serviceStatus && !res.websocketStatus) { |
|
this.stop(); |
|
this.timer && clearTimeout(this.timer); |
|
this.timer = setTimeout(() => { |
|
this.start(); |
|
}, 10 * 1000); |
|
} |
|
}); |
|
}, |
|
getStatus(type) { |
|
//获取 websocket 服务开启状态 |
|
websocket.getStatus(res => { |
|
console.log("websocket开启状态", res); |
|
if (res.data.serviceStatus) { |
|
if(type == 1) { |
|
this.subStatus(); |
|
} |
|
}else if(!res.data.serviceStatus && !res.data.websocketStatus){ |
|
this.timer && clearTimeout(this.timer); |
|
this.timer = setTimeout(() => { |
|
this.start(); |
|
}, 10 * 1000); |
|
} |
|
}); |
|
}, |
|
//通用保活 |
|
onKeep() { |
|
//通用保活 |
|
keepAlive.onKeep({ |
|
//channelId: "Ba-KeepAlive", |
|
//channelName: "Ba-KeepAlive", |
|
// title: "测试", |
|
// content: "常驻通知描述", |
|
}, |
|
(res) => { |
|
console.log(res); |
|
}); |
|
}, |
|
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() { //常驻通知保活 |
|
keepAlive.onShowNotify({ |
|
//channelId: "Ba-KeepAlive", |
|
//channelName: "Ba-KeepAlive", |
|
ID: new Date().getTime(), |
|
title: "入侵报警信息", |
|
content: this.message, |
|
}, |
|
(res) => { |
|
console.log(res); |
|
this.message = ''; |
|
}); |
|
}, |
|
onCancelNotify() { |
|
//取消常驻通知保活 |
|
keepAlive.onCancelNotify({ |
|
//channelId: "Ba-KeepAlive", |
|
//channelName: "Ba-KeepAlive", |
|
//ID:99 |
|
//title: "测试", |
|
//content: "常驻通知描述", |
|
}, |
|
(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); |
|
//若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限 |
|
if (result == -1) { |
|
reject("授权失败:" + JSON.stringify(resultObj.deniedAlways)); |
|
if (resultObj.deniedAlways.indexOf('android.permission.RECEIVE_BOOT_COMPLETED') > -1) { |
|
this.onAutoStart(); |
|
} else if (resultObj.deniedAlways.indexOf( |
|
'android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS') > -1) { |
|
this.requestIgnoreBattery(); |
|
} |
|
} |
|
}, |
|
(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> |