Files
app-govern/App.vue

276 lines
11 KiB
Vue
Raw Normal View History

2023-01-11 16:33:13 +08:00
<script>
2023-10-18 08:53:50 +08:00
import { queryDictDataCache } from './common/api/dictionary.js'
import { getImageUrl } from '@/common/api/basic'
2023-08-11 11:03:31 +08:00
2023-01-11 16:33:13 +08:00
export default {
onLaunch: function () {
2026-04-13 10:12:04 +08:00
// this.checkAppUpdate()
2023-09-13 16:40:03 +08:00
// uni.onPushMessage((res) => {
// console.log("收到推送消息:",res) //监听推送消息
// })
2023-08-10 09:18:17 +08:00
console.log(window)
2023-03-30 09:04:07 +08:00
// this.connect()
2023-07-06 14:24:03 +08:00
console.log('App Launch')
2023-10-18 08:53:50 +08:00
2023-07-06 14:24:03 +08:00
let devCode = uni.getStorageSync('devCode')
2023-07-03 20:29:24 +08:00
if (!devCode) {
2023-07-06 14:24:03 +08:00
uni.getSystemInfo({
success: (result) => {
uni.setStorageSync('devCode', result.deviceId)
},
})
2023-03-16 15:26:25 +08:00
}
2023-10-24 09:01:10 +08:00
uni.onPushMessage((res) => {
console.log('收到推送消息:', res.data.payload.path) //监听推送消息
if (res.data.payload && res.data.payload.path) {
2026-04-03 14:48:45 +08:00
uni.setStorageSync('messageParams', {
2026-04-17 08:50:07 +08:00
engineeringName: '',
engineeringId: '', //工程ID
projectName: '',
projectId:'', //項目ID
deviceName: '',
deviceId: '', //设备ID
lineName: '',
lineId: '', //测点ID
2026-04-03 14:48:45 +08:00
type: res.data.payload.path.split('type=')[1],
})
uni.switchTab({
2023-10-24 09:01:10 +08:00
url: res.data.payload.path,
})
2026-04-03 14:48:45 +08:00
// uni.navigateTo({
// url: res.data.payload.path,
// })
2023-10-24 09:01:10 +08:00
}
})
2023-01-11 16:33:13 +08:00
},
onHide: function () {
console.log('App Hide')
2023-07-06 14:24:03 +08:00
},
2026-04-13 10:12:04 +08:00
methods: {
// 1. 检查应用更新(已分平台:安卓 + iOS
checkAppUpdate() {
// 开发环境跳过检查
const isDev = process.env.NODE_ENV === 'development'
if (isDev) {
return console.log('开发环境,不执行更新检查')
}
let isforce = 1
// uni.showModal({
// title: '更新提示',
// content: '发现新版本,是否立即更新?',
// showCancel: isforce == '0', // 强制更新隐藏取消按钮
// confirmText: '去更新',
// success: (modalRes) => {
// if (modalRes.confirm) {
// this.downloadAndInstallApk('http://112.4.144.18:8040/shiningCloud/file/canneng_wulian.apk')
// } else {
// }
// },
// })
// 获取当前应用信息
plus.runtime.getProperty(plus.runtime.appid, (info) => {
const currentVersion = info.version // 当前本地版本号
// 调用 API 获取服务器上的最新版本信息
getLastestVersion()
.then((res) => {
if (!res.data) {
return
}
const { version, appFileList, iosUrl } = res?.data || {}
// let isforce = 1
// 版本不一样才更新
if (currentVersion != version) {
// ==============================================
// 🔴 关键:判断手机系统(安卓 / iOS
// ==============================================
const isAndroid = plus.os.name === 'Android'
const isIos = plus.os.name === 'iOS'
// ----------------------
// ① iOS跳 App Store
// ----------------------
if (isIos) {
uni.showModal({
title: '更新提示',
content: '发现新版本,请前往 App Store 更新',
showCancel: isforce === '0', // 强制更新隐藏取消按钮
confirmText: '去更新',
success: (modalRes) => {
if (modalRes.confirm) {
// 跳转到 App Store 链接
plus.runtime.openURL(iosUrl)
// 强制更新:退出 App
if (isforce !== '0') {
plus.runtime.quit()
}
} else {
// 不更新直接退出 App强制
if (isforce !== '0') {
plus.runtime.quit()
}
}
},
})
return
}
// ----------------------
// ② Android下载安装
// ----------------------
if (isAndroid) {
uni.showModal({
title: '更新提示',
content: '发现新版本,是否立即更新?',
showCancel: isforce === '0', // 强制更新隐藏取消按钮
confirmText: '去更新',
success: (modalRes) => {
if (modalRes.confirm) {
// 跳转到 App Store 链接
this.downloadAndInstallApk(appFileList[0].filePath)
}
},
})
return
}
}
})
.catch((err) => {
console.log('获取版本接口失败', err)
})
})
},
// 2. 安卓专用:下载并安装 APK
downloadAndInstallApk(url) {
// 防止重复点击下载
if (this.downloadLoading) return
this.downloadLoading = true
uni.showLoading({
title: '正在下载更新...',
mask: true, // 加遮罩,防止重复点
})
// 下载配置(修复路径、覆盖安装)
const options = {
filename: '_doc/update/canneng_wulian.apk', // 固定文件名,更稳定
timeout: 120, // 超时时间
}
// 创建下载任务
const downloadTask = plus.downloader.createDownload(url, options, (downloadedFile, status) => {
this.downloadLoading = false
uni.hideLoading()
if (status === 200) {
// 开始安装
plus.runtime.install(
downloadedFile.filename,
{
force: true, // 强制覆盖安装
},
() => {
uni.showModal({
title: '安装成功',
content: '请重启APP',
showCancel: false,
confirmText: '确定',
success() {
plus.runtime.restart()
},
})
},
(e) => {
console.error('安装失败', e)
uni.showModal({
title: '安装失败',
content: '请开启安装权限后重试:' + e.message,
confirmText: '重试',
success: () => {
this.downloadAndInstallApk(url)
},
})
},
)
} else {
uni.showModal({
title: '下载失败',
content: '网络异常或下载链接失效',
confirmText: '重试',
success: () => {
this.downloadAndInstallApk(url)
},
})
}
})
// 下载进度(优化体验)
downloadTask.addEventListener('statechanged', (task) => {
if (task.state === 3 && task.totalSize > 0) {
const percent = ((task.downloadedSize / task.totalSize) * 100).toFixed(0)
uni.showLoading({
title: `正在下载更新 ${percent}%`,
mask: true,
})
}
})
// 开始下载
downloadTask.start()
},
// downloadAndInstallApk(url) {
// uni.showLoading({ title: '下载新版本...' })
// const downloadTask = plus.downloader.createDownload(
// url,
// { filename: '_doc/update/' },
// (downloadedFile, status) => {
// uni.hideLoading()
// if (status === 200) {
// plus.runtime.install(
// downloadedFile.filename,
// { force: true },
// () => {
// // 安装成功
// },
// (e) => {
// uni.showToast({ title: '安装失败: ' + e.message, icon: 'none' })
// },
// )
// } else {
// uni.showToast({ title: '下载失败', icon: 'none' })
// }
// },
// )
// downloadTask.start()
// },
},
2023-01-11 16:33:13 +08:00
}
</script>
<style lang="scss">
/*每个页面公共css */
@import './common/css/base.scss';
2026-04-03 14:48:45 +08:00
@import '@/static/iconfont/iconfont.css';
2023-03-16 15:26:25 +08:00
/deep/ uni-tabbar .uni-tabbar__badge {
2023-02-09 08:50:01 +08:00
width: auto;
height: 28rpx;
line-height: 28rpx;
border-radius: 28rpx;
min-width: 28rpx;
font-size: 20rpx;
padding: 0 8rpx;
}
2026-03-17 14:00:55 +08:00
// .uni-page-refresh__path {
// stroke: #007aff !important; /* 改成你想要的颜色,比如红色 #ff0000 */
// }
// .uni-page-refresh__icon {
// fill: #007aff !important; /* 改成你想要的颜色,比如红色 #ff0000 */
// }
2023-01-11 16:33:13 +08:00
</style>