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

uniapp在线下载安装包更新app

首先用getSystemInfo判断平台、 再通过json文件模拟接口 判断版本号是否一致 不一致则下载服务器apk进行更新
外加网络波动导致失败重新下载更新包

 uni.getSystemInfo({success: function (e) {// #ifndef H5// 获取手机系统版本const system = e.system.toLowerCase();const platform = e.platform.toLowerCase();// 判断是否为ios设备if (platform.indexOf("ios") != -1 &&(system.indexOf("ios") != -1 || system.indexOf("macos") != -1)) {Vue.prototype.SystemPlatform = "apple";} else if (platform.indexOf("android") != -1 && system.indexOf("android") != -1) {Vue.prototype.SystemPlatform = "android";uni.request({url: "https://xxxxxxx.love/0.json", // JSON 文件地址success: (res) => {if (res.statusCode === 200) {const version = res.data.version; // 获取 版本字段if (version != "1.0.1") {uni.showModal({title: "更新提示",content: "有新的版本可用,是否立即更新?",success: (result) => {if (result.confirm) {const downloadTask = uni.downloadFile({url: "https://xxxxxxx.love/lock.apk", // 更新包地址success: (downloadResult) => {if (downloadResult.statusCode === 200) {const tempFilePath = downloadResult.tempFilePath;// 保存文件到持久目录uni.saveFile({tempFilePath: tempFilePath,success: (saveResult) => {console.log("文件保存成功,路径:",saveResult.savedFilePath);uni.openDocument({filePath: saveResult.savedFilePath,success: function (res) {console.log(res, "打开安装包");},});return;// 提示用户去文件管理器中安装uni.showModal({title: "下载完成",content:"更新包下载完成,请在文件管理器中找到并安装更新包。",showCancel: false,success: () => {uni.hideLoading(); // 隐藏进度条},});},fail: (err) => {console.error("保存文件失败", err);uni.hideLoading(); // 隐藏进度条},});}},fail: (err) => {console.error("下载失败", err);uni.hideLoading(); // 隐藏进度条},});uni.showLoading({title: "正在下载更新包",mask: true,});let lastProgress = 0;let lastUpdateTime = new Date().getTime();const updateInterval = 1500;downloadTask.onProgressUpdate((res) => {const currentTime = new Date().getTime();if (res.progress - lastProgress >= 5 ||currentTime - lastUpdateTime >= updateInterval) {lastProgress = res.progress;lastUpdateTime = currentTime;uni.showLoading({title: `下载中... ${res.progress}%`,mask: true,});}});}},});}}},fail: (err) => {console.error("请求失败", err);},});} else {Vue.prototype.SystemPlatform = "devtools";}// #endif},});

在这里插入图片描述

http://www.lryc.cn/news/425187.html

相关文章:

  • Unity | AmplifyShaderEditor插件基础(第一集:简单了解ASE和初识)
  • Windows文件资源管理器未响应,磁盘状态正常,很可能是这个原因
  • 良好的代码习惯
  • 音乐生成模型应用
  • DBEUG:二维图尺寸没思路
  • 【图像去雾系列】使用SSR/MSR/MSRCR/MSRCP/automatedMSRCR算法对单图像进行图像增强,达到去雾效果
  • oracle普通导出导入
  • 如何将CSDN文章导出为pdf文件
  • 利用Python实现供应链管理中的线性规划与资源优化——手机生产计划1
  • Spring Cloud全解析:配置中心之springCloudConfig分布式配置动态刷新
  • mac如何查看shell是 zsh还是bash
  • STM32cubeMX配置Systick的bug
  • 分享几个好用js片段
  • web前端之实现一只可爱的小杰尼乌龟、伪元素、动画
  • 银河麒麟服务器版在rc.local使用ifcong 配置IP和nmcli的区别
  • 【运维】深入理解 Linux 中的 `mv` 命令,使用 `mv` 移动所有文件但排除特定文件或文件夹
  • Xilinx课程,就这么水灵灵地上线了~
  • 【axios get请求 中文乱码】
  • 智能分析/视频汇聚EasyCVR安防视频融合管理云平台技术优势分析
  • arcgis-坡度坡向分析
  • 【银河麒麟高级服务器操作系统】实际案例分析,xfsaild占用过高
  • JS中【setTimeout】使用注意事项总结
  • 已解决ArkTS开发webview,html页面中的input和按钮等操作均无响应
  • ChatGPT无法登录,提示我们检测到可疑的登录行为,将阻止进一步的尝试。请与管理员联系
  • 【数据结构篇】~复杂度
  • 深入理解Python中的JSON模块:解析与生成JSON数据的实用指南
  • 机器学习三要素:模型、策略和算法
  • 利用红黑树封装map和set
  • python pyqt5暂停和恢复功能
  • CAN总线详解-理论知识部分