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.

293 lines
8.1 KiB

2 years ago
<script>
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 {
keepAlive: null,
reTimenum: 2000, //3秒一次重新链接
reTimer: null, //重新链接的对象
headerTimer: null, //心跳定时器对象
headerTime: 2000, //心跳时间
closeTimer: null, //自动关闭连接定时器对象
closeTime: 3000, //自动关闭时间
employeeId: '',
lockReconnect: false, //避免重复连接
}
},
2 years ago
onLaunch: function() {
console.log('App Launch');
this.keepAlive = uni.requireNativePlugin('Ba-KeepAliveSuit');
2 years ago
},
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.doConnectWebSocket();
}
if(this.keepAlive){
this.keepAlive.onCancelNotify();
}
2 years ago
},
onUnload() {
console.log('App onUnload')
if(this.keepAlive){
setTimeout(() => {
this.onShowNotify();
}, 5000);
}
},
2 years ago
onHide: function() {
console.log('App Hide')
if(this.keepAlive){
setTimeout(() => {
this.onShowNotify();
}, 5000);
}
2 years ago
},
methods: {
onKeep() {
//通用保活
this.keepAlive.onKeep({
//channelId: "Ba-KeepAlive",
//channelName: "Ba-KeepAlive",
title: "测试",
content: "常驻通知描述",
},
(res) => {
console.log(res);
});
},
onAutoStart() { //去设置自启动、后台运行
this.keepAlive.onAutoStart(
(res) => {
console.log(res);
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
requestIgnoreBattery() { //申请加入电池优化白名单
this.keepAlive.requestIgnoreBattery(
res => {
console.log(res);
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
goIgnoreBattery() { //跳转到电池优化设置页
this.keepAlive.goIgnoreBattery(
res => {
console.log(res);
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
isIgnoringBattery() { //是否加入电池优化白名单
this.keepAlive.isIgnoringBattery(
res => {
console.log(res);
// if (res.data) {
// this.msgList.unshift(JSON.stringify(res.data))
// this.msgList.unshift(dateUtil.now())
// }
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
onShowNotify() { //常驻通知保活
this.keepAlive.onShowNotify({
//channelId: "Ba-KeepAlive",
//channelName: "Ba-KeepAlive",
ID: 99,
title: "一楼202室无线入侵",
content: "一楼202室无线入侵报警",
},
(res) => {
console.log(res);
// uni.showToast({
// title: res.msg,
// icon: "none",
// duration: 3000
// })
});
},
onCancelNotify() { //取消常驻通知保活
this.keepAlive.onCancelNotify({
//channelId: "Ba-KeepAlive",
//channelName: "Ba-KeepAlive",
//ID:99
//title: "测试",
//content: "常驻通知描述",
},
(res) => {
console.log(res);
// uni.showToast({
// title: res.msg,
// icon: "none",
// duration: 3000
// })
});
},
//websocket
doConnectWebSocket() {
this.connectWebSocket();
//socket open监听
uni.onSocketOpen((res) => {
console.log('WebSocket连接已打开!');
//链接打开,开启心跳
this.sendHeader();
});
//监听WebSocket接受到服务器的消息事件
uni.onSocketMessage((res) => {
console.log('收到服务器内容:' + res.data);
this.sendHeader();
});
uni.onSocketError((res) => {
console.log('WebSocket连接打开失败,请检查!');
this.reConnectWebSocket();
});
uni.onSocketClose((res) => {
//关闭心跳 并重新链接
this.headerTimer && clearInterval(this.headerTimer);
console.log('WebSocket连接关闭,请检查!', res);
if (res.code != null && res.code != 1000) {
//非正常关闭 提示网络异常并开始重新链接
//开启重复重连机制
this.reConnectWebSocket();
}
});
},
//创建websocket
connectWebSocket() {
uni.connectSocket({
url: '',
method: 'GET',
success(data) {
console.log("websocket连接返回", data);
},
});
},
//重连机制
reConnectWebScoket() {
// 防止多个方法调用,多处重连
if (this.lockReconnect) {
return;
};
this.lockReconnect = true;
console.log('准备重连');
this.reTimer = setTimeout(() => {
this.doConnectWebSocket();
this.lockReconnect = false;
}, this.reTimenum);
},
//心跳
sendHeader() {
console.log("发送心跳sendHeader")
this.headerTimer && clearInterval(this.headerTimer)
this.headerTimer = setInterval(() => {
console.log("发送心跳")
uni.sendSocketMessage({
data: "header"
});
}, this.headerTime)
},
//关闭
closeConn() {
//6秒后没有心跳则 关闭链接
this.closeTimer = setTimeout(() => {
console.log("执行关闭定时器内容")
uni.closeSocket({
code: 1001,
reason: '断线',
complete(res) {
console.log("websocket关闭complete", res)
}
})
}, this.closeTime)
},
2 years ago
// 判断通知权限
getPermis() {
console.log(plus.os.name)
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");
console.log('plus====', NotificationManagerCompat, uid)
//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>