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

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

##使用方法:

可以在app.vueonLaunch中调用appUpdate,打开app就会检测

js 复制代码
onLaunch: function () {
    appUpdate()
},

完整代码如下:

js 复制代码
//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().appVersion

  const {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().appVersion
  uni.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-PLUS
  let 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:比对当前版本和后台管理版本 && 比对当前版本和应用商店版本,满足则提示更新
  • 同时也会校验后台管理是否配置强制更新,是的话,不显示关闭弹框按钮
相关推荐
千里马学框架1 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台1 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone1 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc1 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo2 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077002 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
其实防守也摸鱼2 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
码兄科技2 天前
24小时自助健身房系统软件开发实战指南:功能设计与部署方案
java·spring boot·uni-app
茶底世界之下2 天前
为什么预览、录制、离线导出不能共享同一个背压策略?
ios·swift
AFinalStone2 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui