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

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

##使用方法:

可以在app.vue的onLaunch中调用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:比对当前版本和后台管理版本 && 比对当前版本和应用商店版本,满足则提示更新
  • 同时也会校验后台管理是否配置强制更新,是的话,不显示关闭弹框按钮
相关推荐
FungLeo9 小时前
FengCMS · PHP+MySQL 升级实录 · 危险默认值之殇:root/123456、认证码 8888 写进配置加载器
android·mysql·php·安全加固·fengcms
yz_aiks9 小时前
Termux 使用教程:把安卓手机变成随身 Linux(装 Python、SSH 远程、跑自动化脚本)
android·linux·python·智能手机·ssh·termux
booboosui17 小时前
编号没变,消息还是来了:Mac 端通知读取漏第二条的原因
android
茶底世界之下18 小时前
洗衣房的午后斜光:用 Awaking 把光和影分开
ios·app·产品
雨白1 天前
单例设计模式:从 Java/Kotlin 实现到 Android 实战
android
我是谁的程序员2 天前
iOS 性能分析在 Windows 上怎么做,用 KXBuild 的 kxdevice 替代 Instruments
后端·ios
开心就好20252 天前
通配符 Bundle ID 和精确 Bundle ID 怎么选,推送、iCloud 能不能用通配符
后端·ios
hai_android2 天前
一行 asLiveData,背后是一座生命周期的桥
android·java·kotlin
阿杰m2 天前
Android源码第二十三章:Telephony、RIL 与 IMS——来电怎么到应用?
android
booboosui2 天前
没人认领 .md:安卓把报告交给了音乐播放器打开
android