wangxy 1 개월 전
부모
커밋
87235ee2d8
2개의 변경된 파일130개의 추가작업 그리고 61개의 파일을 삭제
  1. 12 9
      pages/login/index.vue
  2. 118 52
      utils/versionUpdate.js

+ 12 - 9
pages/login/index.vue

@@ -81,13 +81,8 @@
 		error
 	} from "uview-plus";
 	import TipDialog from "@/components/dialog/tipDialog";
-	import {
-		useVersionUpdate
-	} from "@/utils/versionUpdate.js"
-
-	const {
-		showUpdateDialog
-	} = useVersionUpdate();
+  import {} from "@/utils/versionUpdate.js"
+  import {useVersionUpdate} from "../../utils/versionUpdate";
 
 	let indexData = reactive({
 		phoneNumber: null,
@@ -114,6 +109,10 @@
 	const tipDialogRef2 = ref(null)
 	const updateUrl = ref(null)
 
+  const {
+    requestInstallPermission,
+    initDownload,
+    progress} = useVersionUpdate()
 
 	const store = useIsCanBack();
 	onUnload(() => {
@@ -151,7 +150,7 @@
 			plus.runtime.openURL(appStoreUrl)
 		} else {
 			// 处理 Android/HarmonyOS 设备
-			const {
+		/*	const {
 				brand
 			} = systemInfo;
 			const androidPackage = 'com.llisoft.ezy'; // 安卓包名
@@ -192,7 +191,11 @@
 						});
 					});
 				});
-			});
+			});*/
+
+      initDownload(updateUrl.value)
+
+
 
 		}
 	}

+ 118 - 52
utils/versionUpdate.js

@@ -1,68 +1,134 @@
+import {ref} from "vue"
+
 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);
-						}
-					}
+
+	const progress = ref(0);
+	let downloadTask = null;
+
+	function checkInstallPermission() {
+		// 获取主Activity对象
+		const mainActivity = plus.android.runtimeMainActivity();
+		// 获取PackageManager对象
+		const packageManager = mainActivity.getPackageManager();
+
+		// 正确方式:使用plus.android.invoke调用方法
+		const canRequest = plus.android.invoke(packageManager, "canRequestPackageInstalls");
+
+		if (canRequest) {
+			console.log("应用有安装权限");
+			return true;
+		} else {
+			console.log("应用无安装权限,需要请求");
+			return false;
+		}
+	}
+
+
+	function requestInstallPermission() {
+		return new Promise((resolve) => {
+			// 跳转到允许安装未知来源应用的设置界面
+			const Intent = plus.android.importClass("android.content.Intent");
+			const Settings = plus.android.importClass("android.provider.Settings");
+
+			const mainActivity = plus.android.runtimeMainActivity();
+			const intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
+
+			// 设置应用的包名
+			const uri = plus.android.invoke(
+				plus.android.invoke("android.net.Uri", "parse", "package:" + plus.runtime.appid)
+			);
+			intent.setData(uri);
+
+			// 启动Activity并处理结果
+			mainActivity.startActivityForResult(intent, 1001);
+
+			// 监听Activity结果(需要在App.vue的onActivityResult中全局监听)
+			plus.globalEvent.addEventListener("onActivityResult", (result) => {
+				if (result.requestCode === 1001) {
+					// 再次检查权限状态
+					checkInstallPermission().then(hasPermission => {
+						resolve(hasPermission);
+					});
 				}
 			});
 		});
 	}
 
-	function compareVersion(v1, v2) {
-		v1 = v1.split('.');
-		v2 = v2.split('.');
-		const len = Math.max(v1.length, v2.length);
+	function downloadAndInstallApk(apkUrl) {
+		return new Promise((resolve, reject) => {
+			uni.showLoading({ title: '下载中...', mask: true });
 
-		while (v1.length < len) v1.push('0');
-		while (v2.length < len) v2.push('0');
+			uni.downloadFile({
+				url: apkUrl,
+				success: (downloadResult) => {
+					uni.hideLoading();
+					if (downloadResult.statusCode === 200) {
+						const tempFilePath = downloadResult.tempFilePath;
 
-		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;
+						// 安装APK
+						plus.runtime.install(tempFilePath, { force: false }, () => {
+							uni.showToast({ title: '安装成功', icon: 'success' });
+							setTimeout(() => {
+								plus.runtime.restart(); // 重启应用
+							}, 1500);
+							resolve();
+						}, (err) => {
+							console.error('安装失败: ', err);
+							uni.showToast({ title: '安装失败', icon: 'none' });
+							reject(err);
+						});
+					}
+				},
+				fail: (error) => {
+					uni.hideLoading();
+					console.error('下载失败: ', error);
+					uni.showToast({ title: '下载失败', icon: 'none' });
+					reject(error);
+				}
+			});
+		});
 	}
 
-	// 提示用户更新并下载安装包[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)
-				}
+
+	async function initDownload(url) {
+		console.log('url下载', url)
+		try {
+			// 1. 检查安装权限
+			const hasPermission = await checkInstallPermission();
+
+			console.log('权限', hasPermission)
+
+			if (!hasPermission) {
+				// 2. 如果没有权限,请求权限
+				uni.showModal({
+					title: '权限申请',
+					content: '需要您允许应用安装权限,以便完成版本更新。',
+					confirmText: '去设置',
+					success: async (res) => {
+						if (res.confirm) {
+							const permissionGranted = await requestInstallPermission();
+							if (permissionGranted) {
+								// 3. 权限获取成功,开始下载安装
+								await downloadAndInstallApk(url);
+							} else {
+								uni.showToast({ title: '权限获取失败', icon: 'none' });
+							}
+						}
+					}
+				});
+			} else {
+				// 已经有权限,直接下载安装
+				await downloadAndInstallApk(url);
 			}
-		});
+		} catch (error) {
+			console.error('更新流程出错: ', error);
+			uni.showToast({ title: '更新失败', icon: 'none' });
+		}
 	}
 
 	return {
-		getVersion,
-		compareVersion,
-		showUpdateDialog
+		requestInstallPermission,
+		initDownload,
+		progress
 	}
 }