当前位置: 首页 > web >正文

uniapp中检查版本,提示升级app,安卓下载apk,ios跳转应用商店

uniapp中检查版本,提示升级app,安卓下载apk,ios跳转应用商店

##使用方法:
可以在app.vueonLaunch中调用appUpdate,打开app就会检测

onLaunch: function () {appUpdate()
},

完整代码如下:

//APP更新
import {queryAppVersion} from "@/apis/app";// 比较版本号:v1 < v2 返回 -1,v1 > v2 返回 1,相等返回 0
function compareVersions(v1, v2) {const v1Parts = v1.split('.').map(Number);const v2Parts = v2.split('.').map(Number);const maxLength = Math.max(v1Parts.length, v2Parts.length);for (let i = 0; i < maxLength; i++) {const part1 = v1Parts[i] || 0;const part2 = v2Parts[i] || 0;if (part1 > part2) return 1;if (part1 < part2) return -1;}return 0;
}async function checkAndroid(remoteRes) {let curVersion = uni.getAppBaseInfo().appVersionconst {appDownloadUrl, izForceFlush, appVersion} = remoteRes;if (compareVersions(appVersion, curVersion) === 1) {uni.showModal({title: "提示",content: "发现新版本,立即升级!",confirmText: "确定",cancelText: "取消",showCancel: izForceFlush !== '1',success: function (res) {if (res.confirm) {uni.showLoading({title: '下载中',mask: true});uni.downloadFile({url: appDownloadUrl, // 这个是最新版本apk包的地址success: (res) => {uni.hideLoading();if (res.tempFilePath) {plus.runtime.install(res.tempFilePath, {force: true}, _res => {uni.showToast({title: "更新成功,重启中",duration: 1500,});plus.runtime.restart();});} else {uni.showToast({title: "下载失败!",icon: "none",});}},fail(err) {uni.showToast({title: "下载失败!",icon: "none",});}})}}})}
}// TODO 你的appStore中的id
const appleId = '';async function checkIos(remoteRes) {const {iosForceFlush, iosAppVersion} = remoteRes;let curVersion = uni.getAppBaseInfo().appVersionuni.request({url: `http://itunes.apple.com/cn/lookup?id=${appleId}`,method: 'POST',success: (res) => {if (res.data.results && res.data.results.length) {const onlineVersion = res.data.results[0].version;// 服务器版本 > 当前版本 && appStore版本 > 当前版本,则提示更新if (compareVersions(onlineVersion, curVersion) === 1 && compareVersions(iosAppVersion, curVersion) === 1) {uni.showModal({title: '更新提示',content: '发现新版本,是否要更新?',showCancel: iosForceFlush !== '1',success(res) {if (res.confirm) {plus.runtime.launchApplication({action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`},function (e) {console.log(e.message);});}}})}}}});
}export default async function appUpdate() {// #ifdef APP-PLUSlet system = uni.getSystemInfoSync();const [err, res] = await queryAppVersion();if (err || !res.result) return;if (system.platform === 'ios') {checkIos(res.result);}if (system.platform === 'android') {checkAndroid(res.result);}// #endif
}

queryAppVersion是后台管理配置的版本信息

对比方法:

  • 安卓:比对当前版本和后台管理版本,提示更新
  • IOS:比对当前版本和后台管理版本 && 比对当前版本和应用商店版本,满足则提示更新
  • 同时也会校验后台管理是否配置强制更新,是的话,不显示关闭弹框按钮
http://www.xdnf.cn/news/1952.html

相关文章:

  • ubiquant比赛系列——用docker准备ubipoker开发环境
  • volatile缓存可见性实现原理
  • 支付场景下,乐观锁的实现(简洁版)
  • AiFlutter 低代码平台介绍
  • hadoop-3.3.5.tar.gz 镜像
  • Hi3516CV608 超高清智慧视觉 SoC 芯片 可提供开发资料
  • Python 快速获取Excel工作表名称
  • Easysearch 基础运维扫盲指南:从 HTTP 到 HTTPS、认证与安全访问全解析
  • 侧扫声呐概述
  • Ollama 常见命令速览:本地大模型管理指南
  • arcpy列表函数的应用(2)
  • 数据作为新生产要素,如何实现价值变现?
  • 向量数据库实践:存储和检索向量数据
  • Python实验三
  • 论文学习:《聚类矩阵正则化指导的层次图池化》
  • Java多线程
  • 2.5 桥梁桥面系及附属结构施工
  • kafka课后总结
  • Spring @Transactional 自调用问题深度解析
  • 【Unity 与c++通信】Unity与c++通信注意事项,参数传递
  • websheet之 自定义函数
  • 成都种业博览会预登记火热进行中,6月8日-9日成都世纪城新国际会展中心与您不见不散!
  • [密码学实战]商用密码产品密钥体系架构:从服务器密码机到动态口令系统
  • vue前端SSE工具库|EventSource 替代方案推荐|PUSDN平行宇宙软件开发者网
  • 如何申请游戏支付平台通道接口?
  • PyTorch生成式人工智能实战(3)——分类任务详解
  • 施磊老师基于muduo网络库的集群聊天服务器(七)
  • 容器的网络类型
  • 视频噪点多,如何去除画面噪点?
  • 【基于Qt的QQMusic项目演示第一章】从界面交互到核心功能实现