wangxy 1 månad sedan
förälder
incheckning
a8b392d019
1 ändrade filer med 68 tillägg och 0 borttagningar
  1. 68 0
      utils/versionUpdate.js

+ 68 - 0
utils/versionUpdate.js

@@ -0,0 +1,68 @@
+export function useVersionUpdate() {
+	function getVersion() {
+		// 获取当前应用版本
+		plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
+			// 请求服务器,获取最新版本信息 (示例)
+			uni.request({
+				url: 'https://your-api-domain.com/check-update',
+				method: 'GET',
+				data: {
+					appid: plus.runtime.appid,
+					version: widgetInfo.version,
+					versionCode: widgetInfo.versionCode
+				},
+				success: (res) => {
+					if (res.statusCode === 200) {
+						const serverData = res.data;
+						// 对比版本号[2](@ref)
+						if (compareVersion(serverData.version, widgetInfo.version) > 0) {
+							// 发现新版本,提示用户更新
+							this.showUpdateDialog(serverData);
+						}
+					}
+				}
+			});
+		});
+	}
+
+	function compareVersion(v1, v2) {
+		v1 = v1.split('.');
+		v2 = v2.split('.');
+		const len = Math.max(v1.length, v2.length);
+
+		while (v1.length < len) v1.push('0');
+		while (v2.length < len) v2.push('0');
+
+		for (let i = 0; i < len; i++) {
+			const num1 = parseInt(v1[i]);
+			const num2 = parseInt(v2[i]);
+
+			if (num1 > num2) return 1;
+			if (num1 < num2) return -1;
+		}
+		return 0;
+	}
+
+	// 提示用户更新并下载安装包[4](@ref)
+	function showUpdateDialog(updateData) {
+		uni.showModal({
+			title: '发现新版本',
+			showCancel: false,
+			content: updateData.description || '有新的版本可用,请更新体验更多功能',
+			confirmText: '立即更新',
+			success: (res) => {
+				if (res.confirm) {
+					// 下载APK文件[4](@ref)
+					plus.runtime.openURL(updateData.url); // 调用系统浏览器下载
+					// 或使用下载管理器直接下载安装[5](@ref)
+				}
+			}
+		});
+	}
+
+	return {
+		getVersion,
+		compareVersion,
+		showUpdateDialog
+	}
+}