gaoluyang
2026-05-29 0e053f43fe591be3e0e92e3cfb4ad90d660b0546
src/utils/versionUpgrade.js
@@ -1,6 +1,7 @@
import config from "@/config";
import { getAllVersion } from "@/api/viewIndex";
import bus from "@/plugins/bus";
let hasTriggeredVersionCheckInSession = false;
function compareVersion(v1, v2) {
  const s1 = String(v1 || "").replace(/[^\d.]/g, "").split(".").map((n) => Number(n) || 0);
@@ -25,12 +26,12 @@
}
async function getCurrentVersion(logPrefix) {
  let currentVersion = config?.appInfo?.version || "1.0.0";
  let currentVersion = (config && config.appInfo && config.appInfo.version) || "1.0.0";
  console.log(`${logPrefix} 开始获取当前版本,默认值:`, currentVersion);
  // #ifdef MP-WEIXIN
  try {
    const accountInfo = uni.getAccountInfoSync();
    if (accountInfo?.miniProgram?.version) {
    if (accountInfo && accountInfo.miniProgram && accountInfo.miniProgram.version) {
      currentVersion = accountInfo.miniProgram.version;
      console.log(`${logPrefix} 当前环境=MP-WEIXIN,版本=`, currentVersion);
      return currentVersion;
@@ -44,14 +45,14 @@
    // APP-PLUS 下,plus.runtime.version 不是业务版本号(经常是运行时/SDK版本),
    // 这里改用 getProperty 取系统层面的 app version。
    // @ts-ignore
    if (plus?.runtime?.getProperty) {
    if (plus && plus.runtime && plus.runtime.getProperty) {
      // @ts-ignore
      const appid = plus.runtime.appid;
      const appInfo = await new Promise((resolve) => {
        // @ts-ignore
        plus.runtime.getProperty(appid, (info) => resolve(info || {}));
      });
      const v = appInfo?.version || appInfo?.versionName || appInfo?.appVersion || "";
      const v = (appInfo && appInfo.version) || (appInfo && appInfo.versionName) || (appInfo && appInfo.appVersion) || "";
      if (v) {
        currentVersion = String(v);
        console.log(`${logPrefix} 当前环境=APP-PLUS,版本=`, currentVersion);
@@ -89,7 +90,7 @@
    },
    (err) => {
      console.log(`${logPrefix} 安装失败:`, err);
      uni.showToast({ title: err?.message || "安装更新包失败", icon: "none" });
      uni.showToast({ title: (err && err.message) || "安装更新包失败", icon: "none" });
    }
  );
  // #endif
@@ -100,7 +101,7 @@
  if (!u) return "";
  // 已经是绝对地址,直接返回
  if (/^https?:\/\//i.test(u)) return u;
  const base = String(config?.fileUrl || config?.baseUrl || "").replace(/\/+$/, "");
  const base = String((config && config.fileUrl) || (config && config.baseUrl) || "").replace(/\/+$/, "");
  const path = u.startsWith("/") ? u : `/${u}`;
  return `${base}${path}`;
}
@@ -264,12 +265,17 @@
  let lastVersionCheckAt = 0;
  const triggerVersionCheck = async (from = "unknown") => {
    if (hasTriggeredVersionCheckInSession) {
      console.log(`${logPrefix} 跳过版本检查,本次会话已检测过,来源=${from}`);
      return;
    }
    const now = Date.now();
    if (now - lastVersionCheckAt < throttleMs) {
      console.log(`${logPrefix} 跳过重复检查,来源=${from}`);
      return;
    }
    lastVersionCheckAt = now;
    hasTriggeredVersionCheckInSession = true;
    console.log(`${logPrefix} 触发版本检查,来源=${from}`);
    const currentVersion = await getCurrentVersion(logPrefix);
    await checkAppVersionUpgrade(logPrefix, currentVersion);