uniapp在线下载安装包更新app

首先用getSystemInfo判断平台、 再通过json文件模拟接口 判断版本号是否一致 不一致则下载服务器apk进行更新

外加网络波动导致失败重新下载更新包

js 复制代码
 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
      },
    });
相关推荐
&白帝&4 小时前
uniapp中使用picker-view选择时间
前端·uni-app
fakaifa5 小时前
八戒农场小程序V2最新源码
小程序·uni-app·php·生活·开源软件
平凡シンプル9 小时前
安卓 uniapp跨端开发
android·uni-app
艾小逗11 小时前
uniapp快速入门教程,内容来源于官方文档,仅仅记录快速入门需要了解到的知识点
小程序·uni-app·app·es6
鸭子嘎鹅子呱1 天前
uniapp使用高德地图设置marker标记点,后续根据接口数据改变某个marker标记点,动态更新
uni-app·map·高德地图
计算机源码社1 天前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
Angus-zoe1 天前
uniapp+vue+微信小程序实现侧边导航
vue.js·微信小程序·uni-app
Pluto & Ethereal1 天前
uni-app尺寸单位、flex布局于背景图片
uni-app
天空下sky2 天前
uniapp+若依 开发租房小程序源码分享
uni-app
帅过二硕ฅ2 天前
uniapp点击跳转到对应位置
前端·javascript·uni-app