|
|
@@ -17,6 +17,66 @@ const compareVersion = (a = "", b = "") => {
|
|
|
|
|
|
return 0;
|
|
|
};
|
|
|
+
|
|
|
+let appUpgradePromise = null;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 全局执行 APK 整包更新。
|
|
|
+ *
|
|
|
+ * App.vue / 页面全局 onShow 都可能触发更新检查,因此这里不能依赖某个页面
|
|
|
+ * 是否挂载了 upgrade 组件。使用 uni 的全局提示能力后,用户停留在任意页面
|
|
|
+ * 都能收到更新提示;模块内 Promise 同时避免重复弹窗和重复下载。
|
|
|
+ */
|
|
|
+export const runAppUpgrade = ({
|
|
|
+ appVersion: name,
|
|
|
+ note: content = "",
|
|
|
+ url,
|
|
|
+ forceUpdate = true,
|
|
|
+}) => {
|
|
|
+ if (appUpgradePromise) return appUpgradePromise;
|
|
|
+
|
|
|
+ appUpgradePromise = new Promise((resolve, reject) => {
|
|
|
+ uni.showModal({
|
|
|
+ title: `发现新版本 V${name}`,
|
|
|
+ content: content || "检测到新的应用版本,是否立即更新?",
|
|
|
+ showCancel: !forceUpdate,
|
|
|
+ confirmText: "立即更新",
|
|
|
+ cancelText: "稍后再说",
|
|
|
+ success: async ({ confirm }) => {
|
|
|
+ if (!confirm) {
|
|
|
+ resolve({ status: "cancelled" });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ uni.showLoading({
|
|
|
+ title: "下载更新中",
|
|
|
+ mask: true,
|
|
|
+ });
|
|
|
+
|
|
|
+ const fileName = await downloadApp(url, (percent) => {
|
|
|
+ uni.showLoading({
|
|
|
+ title: `下载中 ${percent}%`,
|
|
|
+ mask: true,
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ uni.hideLoading();
|
|
|
+ installApp(fileName);
|
|
|
+ resolve({ status: "install-triggered" });
|
|
|
+ } catch (error) {
|
|
|
+ uni.hideLoading();
|
|
|
+ reject(error);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: reject,
|
|
|
+ });
|
|
|
+ }).finally(() => {
|
|
|
+ appUpgradePromise = null;
|
|
|
+ });
|
|
|
+
|
|
|
+ return appUpgradePromise;
|
|
|
+};
|
|
|
|
|
|
/**
|
|
|
* @description 检查app版本是否需要升级
|
|
|
@@ -33,8 +93,8 @@ export const checkVersion = async ({
|
|
|
url, //下载链接
|
|
|
forceUpdate, //是否强制升级
|
|
|
}) => {
|
|
|
- const currentAppVersion = plus.runtime.version;
|
|
|
- if (compareVersion(code, currentAppVersion) > 0) {
|
|
|
+ const currentAppVersion = plus.runtime.version;
|
|
|
+ if (compareVersion(code, currentAppVersion) > 0) {
|
|
|
let platform = uni.getSystemInfoSync().platform; //手机平台
|
|
|
if (platform === "android") {
|
|
|
uni.$emit("upgrade-app", {
|