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 View 位移原理:源码级深度剖析
android·面试·android jetpack
_一条咸鱼_1 小时前
深度剖析:Android View 滑动原理大揭秘
android·面试·android jetpack
_一条咸鱼_1 小时前
深度揭秘:Android View 滑动冲突原理全解析
android·面试·android jetpack
_一条咸鱼_1 小时前
揭秘 Android View 惯性滑动原理:从源码到实战
android·面试·android jetpack
ansondroider3 小时前
Android adb 安装应用失败(安装次数限制)
android·adb·install
zhishishe4 小时前
如何在 iPhone 上恢复已删除的联系人:简短指南
ios·智能手机·iphone
zhishishe4 小时前
如何修复卡在恢复模式下的 iPhone:简短指南
windows·macos·ios·objective-c·cocoa·iphone
桃花仙丶4 小时前
iOS/Flutter混合开发之PlatformView配置与使用
flutter·ios·xcode·swift·dart