Files
app-govern/App.vue
2026-04-13 10:12:04 +08:00

270 lines
11 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script>
import { queryDictDataCache } from './common/api/dictionary.js'
import { getImageUrl } from '@/common/api/basic'
export default {
onLaunch: function () {
// this.checkAppUpdate()
// uni.onPushMessage((res) => {
// console.log("收到推送消息:",res) //监听推送消息
// })
console.log(window)
// this.connect()
console.log('App Launch')
let devCode = uni.getStorageSync('devCode')
if (!devCode) {
uni.getSystemInfo({
success: (result) => {
uni.setStorageSync('devCode', result.deviceId)
},
})
}
uni.onPushMessage((res) => {
console.log('收到推送消息:', res.data.payload.path) //监听推送消息
if (res.data.payload && res.data.payload.path) {
uni.setStorageSync('messageParams', {
name: '',
id: '',
type: res.data.payload.path.split('type=')[1],
})
uni.switchTab({
url: res.data.payload.path,
})
// uni.navigateTo({
// url: res.data.payload.path,
// })
}
})
},
onHide: function () {
console.log('App Hide')
},
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()
// },
},
}
</script>
<style lang="scss">
/*每个页面公共css */
@import './common/css/base.scss';
@import '@/static/iconfont/iconfont.css';
/deep/ uni-tabbar .uni-tabbar__badge {
width: auto;
height: 28rpx;
line-height: 28rpx;
border-radius: 28rpx;
min-width: 28rpx;
font-size: 20rpx;
padding: 0 8rpx;
}
// .uni-page-refresh__path {
// stroke: #007aff !important; /* 改成你想要的颜色,比如红色 #ff0000 */
// }
// .uni-page-refresh__icon {
// fill: #007aff !important; /* 改成你想要的颜色,比如红色 #ff0000 */
// }
</style>