<script>
|
import {
|
calcDate
|
} from '@/utils/date.js'
|
import {
|
tokenTime
|
} from '@/common/setting.js'
|
import {
|
isUpdate
|
} from "@/api/system/update.js"
|
export default {
|
data() {
|
return {
|
refreshLock: false,
|
nowVersion: "",
|
showdownLine: false,
|
downloadNum: 0
|
}
|
},
|
onLaunch: function() {
|
// #ifdef APP-PLUS
|
this.getPushPermission()
|
this.initPush()
|
this.isTokenOvertime()
|
// #endif
|
console.log("定时器执行")
|
this.refreshToken()
|
},
|
onShow: function() {
|
// #ifdef APP-PLUS
|
this.hasUpdate()
|
// #endif
|
// #ifdef MP-WEIXIN
|
this.wxUpdate()
|
// #endif
|
},
|
onHide: function() {},
|
methods: {
|
|
initPush() {
|
const _self = this;
|
//uni-push 2.0写法
|
uni.onPushMessage((res) => {
|
console.log(res)
|
if (res.type == 'click') {
|
// clearTimeout(timer);
|
|
// timer = setTimeout(() => {
|
// // 处理跳转的业务可以写这边
|
// }, 1500);
|
uni.reLaunch({
|
url: '/pages/login/index'
|
});
|
} else {
|
//aps为空说明是在线,aps有值的时候是离线,避免离线点击后再监听会出现两次通知栏
|
if (res.data.aps) {
|
return;
|
}
|
let platform = uni.getSystemInfoSync().platform;
|
let msg = res.data;
|
if (platform == 'ios') { //由于IOS 必须要创建本地消息 所以做这个判断
|
if (msg.payload && msg.payload != null) {
|
plus.push.createMessage(msg.content, msg.payload) //创建本地消息
|
}
|
}
|
if (platform == 'android') {
|
let options = {
|
cover: false,
|
sound: "system",
|
title: msg.title
|
}
|
plus.push.createMessage(msg.content, msg.payload, options);
|
}
|
|
|
}
|
})
|
|
},
|
|
//判断token是否过期
|
isTokenOvertime() {
|
const token = uni.getStorageSync('token')
|
//获取token目前持有时间
|
const date = calcDate(token.datetime, new Date().getTime())
|
if (!date) return
|
//进入首页
|
if (!isNaN(date.seconds) && date.seconds < tokenTime) {
|
//关闭启动页 直接进入首页
|
let tabbar = uni.getStorageSync("tabBarList")
|
uni.reLaunch({
|
url: tabbar[0].path,
|
success: () => {
|
//跳转完页面后再关闭启动页
|
this.closeSplash()
|
}
|
})
|
} else {
|
//跳转到登录页
|
uni.reLaunch({
|
url: "/pages/login/index",
|
success: () => {
|
//跳转完页面后再关闭启动页
|
this.closeSplash()
|
}
|
})
|
}
|
},
|
|
//关闭启动图
|
closeSplash() {
|
setTimeout(() => {
|
// #ifdef APP-PLUS
|
plus.navigator.closeSplashscreen()
|
// #endif
|
}, 500)
|
},
|
|
// 1 更新
|
compareVersion(version1, version2) {
|
const arr1 = version1.split('.')
|
const arr2 = version2.split('.')
|
const length1 = arr1.length
|
const length2 = arr2.length
|
const minlength = Math.min(length1, length2)
|
let i = 0
|
for (i; i < minlength; i++) {
|
let a = parseInt(arr1[i])
|
let b = parseInt(arr2[i])
|
if (a > b) {
|
return 1
|
} else if (a < b) {
|
return -1
|
}
|
}
|
if (length1 > length2) {
|
for (let j = i; j < length1; j++) {
|
if (parseInt(arr1[j]) != 0) {
|
return 1
|
}
|
}
|
return 0
|
} else if (length1 < length2) {
|
for (let j = i; j < length2; j++) {
|
if (parseInt(arr2[j]) != 0) {
|
return -1
|
}
|
}
|
return 0
|
}
|
return 0
|
},
|
|
AndroidCheckUpdate(data) {
|
let that = this;
|
plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
|
that.nowVersion = wgtinfo.version //客户端版本号
|
const flag = this.compareVersion(data.appVersion, that.nowVersion)
|
if (flag == 1) {
|
uni.showModal({
|
title: "检测到新版本,请立即更新",
|
content: data.remarks,
|
success(res) {
|
if (res.confirm) {
|
that.doUpData(data.path);
|
that.showdownLine = true;
|
// plus.runtime.openURL(androidUrl)
|
} else if (res.cancel) {
|
// 不更新强制退出app
|
plus.runtime.quit();
|
// }
|
}
|
}
|
})
|
|
}
|
})
|
},
|
|
doUpData(Url) {
|
const that = this
|
uni.showLoading({
|
title: '更新中……'
|
});
|
const downloadTask = uni.downloadFile({
|
//执行下载
|
url: Url, //下载地址
|
timeout: 1000 * 30, //30秒超时时间
|
success: downloadResult => {
|
//下载成功
|
uni.hideLoading();
|
if (downloadResult.statusCode == 200) {
|
uni.showModal({
|
title: '',
|
content: '更新成功,确定现在重启吗?',
|
confirmText: '重启',
|
confirmColor: '#EE8F57',
|
success(res) {
|
if (res.confirm) {
|
plus.runtime.install(
|
//安装软件
|
downloadResult.tempFilePath, {
|
force: true
|
},
|
function(res) {
|
plus.runtime.restart();
|
},
|
function(err) {
|
uni.showModal({
|
title: "安装失败",
|
content: err
|
})
|
},
|
);
|
}
|
}
|
})
|
}
|
},
|
fail: err => {
|
uni.hideLoading();
|
that.$u.toast(err.errMsg);
|
}
|
});
|
},
|
|
async hasUpdate() {
|
const res = await isUpdate()
|
if (res.code != 200) return
|
this.AndroidCheckUpdate(res.data)
|
},
|
|
wxUpdate() {
|
const that = this
|
const updateManager = uni.getUpdateManager()
|
updateManager.onCheckForUpdate(function(res) {
|
that.$store.commit("SET_HAS_UPDATE", res.hasUpdate)
|
// 请求完新版本信息的回调
|
if (res.hasUpdate) {
|
uni.showModal({
|
title: '更新提示',
|
content: '新版本已经准备好,是否重启应用?',
|
showCancel: false,
|
success: function(res) {
|
if (res.confirm) {
|
uni.clearStorageSync()
|
updateManager.onUpdateReady(function(ress) {
|
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
that.$store.commit("SET_HAS_UPDATE", false)
|
updateManager.applyUpdate()
|
})
|
|
} else {
|
that.$store.commit("SET_HAS_UPDATE", true)
|
}
|
}
|
})
|
}
|
})
|
updateManager.onUpdateFailed(function(res) {
|
that.$store.commit("SET_HAS_UPDATE", true)
|
// 新版本下载失败
|
uni.showModal({
|
showCancel: false,
|
title: '提示',
|
content: '版本更新失败,请检查网络或其他原因',
|
})
|
});
|
},
|
|
// 定时检测token
|
refreshToken() {
|
this.refreshTime = setInterval(() => {
|
const token = uni.getStorageSync('token')
|
const date = calcDate(token.datetime, new Date().getTime())
|
console.log(date)
|
if (!date) return
|
if (date.seconds >= tokenTime && !this.refreshLock) {
|
this.refreshLock = true
|
this.$u.api
|
.refreshToken(token.content, '000000')
|
.then(res => {
|
this.$u.func
|
.refreshToken(res)
|
.then(() => {
|
|
this.refreshLock = false
|
})
|
.catch(() => {
|
this.refreshLock = false
|
})
|
})
|
.catch(() => {
|
this.refreshLock = false
|
})
|
}
|
}, 600000) //30分钟
|
},
|
|
getPushPermission() {
|
let platform = uni.getSystemInfoSync().platform; //首先判断app是安卓还是ios
|
console.log(platform);
|
if (platform == "ios") { //这里是ios的方法
|
console.log("我是iOS");
|
var UIApplication = plus.ios.import("UIApplication");
|
var app = UIApplication.sharedApplication();
|
var enabledTypes = 0;
|
if (app.currentUserNotificationSettings) {
|
var settings = app.currentUserNotificationSettings();
|
enabledTypes = settings.plusGetAttribute("types");
|
console.log("enabledTypes1:" + enabledTypes);
|
}
|
plus.ios.deleteObject(settings);
|
} else if (platform == "android") { //下面是安卓的方法
|
console.log("我是安卓", plus.android);
|
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();
|
// 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置
|
// if (!areNotificationsEnabled) {
|
// this.tongzhi = true; //这里也一样未开启权限,弹出弹窗
|
// }
|
}
|
|
}
|
},
|
}
|
</script>
|
<style lang="scss">
|
@import "./static/iconfont.css";
|
@import "./static/base.css";
|
@import "./static/flex.css";
|
@import "./static/margin.css";
|
@import "./static/padding.css";
|
/*uview全局样式*/
|
@import "uview-ui/index.scss";
|
|
page {
|
font-family: PingFang SC;
|
}
|
|
uni-page-wrapper {
|
width: 100%;
|
height: 100%;
|
}
|
|
uni-page-body {
|
width: 100%;
|
height: 100%;
|
}
|
|
.uni-page {
|
font-family: PingFang SC;
|
}
|
|
.u-radio {
|
margin-right: 40rpx !important;
|
}
|
|
// 修改checkbox样式
|
checkbox.curdCheck .wx-checkbox-input,
|
checkbox.curdCheck .uni-checkbox-input {
|
border-radius: 50% !important;
|
border-color: #c8c9cc !important;
|
color: #5f88ff !important;
|
}
|
|
checkbox.curdCheck .wx-checkbox-input-checked,
|
checkbox.curdCheck .uni-checkbox-input-checked {
|
// background-color: #5f88ff;
|
color: #5f88ff !important;
|
border-color: #5f88ff !important;
|
}
|
|
.uni-picker-container,
|
.wx-picker-container {
|
z-index: 10074;
|
}
|
|
uni-modal {
|
z-index: 19999 !important;
|
}
|
</style>
|