2 Commits e2f3cf532a ... 6b1cdce1f4

Auteur SHA1 Message Date
  leo 6b1cdce1f4 fix: 优化安卓应用更新安装流程,适配未知应用权限 il y a 4 semaines
  leo 7f57ca62a7 fix: 自动更新逻辑调整 il y a 4 semaines
3 fichiers modifiés avec 103 ajouts et 11 suppressions
  1. 3 2
      manifest.json
  2. 58 5
      pages/login/login.vue
  3. 42 4
      pages/my/about/about.vue

+ 3 - 2
manifest.json

@@ -2,8 +2,8 @@
     "name" : "云飞智控",
     "appid" : "__UNI__DBA6730",
     "description" : "",
-    "versionName" : "1.15.31",
-    "versionCode" : 11531,
+    "versionName" : "1.15.35",
+    "versionCode" : 11535,
     "transformPx" : false,
     "sassImplementationName" : "node-sass",
     /* 5+App特有相关 */
@@ -64,6 +64,7 @@
                     "<uses-permission android:name=\"android.permission.READ_LOGS\"/>",
                     "<uses-permission android:name=\"android.permission.READ_PHONE_STATE\"/>",
                     "<uses-permission android:name=\"android.permission.RECORD_AUDIO\"/>",
+                    "<uses-permission android:name=\"android.permission.REQUEST_INSTALL_PACKAGES\"/>",
                     "<uses-permission android:name=\"android.permission.UPDATE_DEVICE_STATS\"/>",
                     "<uses-permission android:name=\"android.permission.VIBRATE\"/>",
                     "<uses-permission android:name=\"android.permission.WAKE_LOCK\"/>",

+ 58 - 5
pages/login/login.vue

@@ -179,6 +179,7 @@ export default {
       contentA: '',
       isShow: false,
       percentNum: 0,
+      apkPath: '',
       passvalue: false,
       turnover: true,
       agree: false,
@@ -231,6 +232,12 @@ export default {
         this.turnover = res.data;
       },
     });
+    // 从“安装未知应用”设置页返回后,继续安装已下载的更新包
+    // #ifdef APP-PLUS
+    if (this.apkPath) {
+      this.installApk(this.apkPath, true);
+    }
+    // #endif
     this.getEquipList();
   },
   methods: {
@@ -332,11 +339,8 @@ export default {
         success: (res) => {
           console.log(res);
           if (res.statusCode === 200) {
-            console.log('下载成功');
-            console.log(
-              '安装包下载成功,即将安装:' + JSON.stringify(res, null, 4),
-            );
-            plus.runtime.openFile(res.tempFilePath);
+            console.log('安装包下载成功,即将安装:' + res.tempFilePath);
+            this.installApk(res.tempFilePath);
             this.showA = false;
             this.isShow = false;
           }
@@ -355,6 +359,55 @@ export default {
         }
       });
     },
+    // 调起系统安装器安装更新包,失败时引导开启“安装未知应用”后自动重试一次
+    // fromGuide:从系统设置页返回后的重试,不再引导,直接提示失败原因
+    installApk(path, fromGuide) {
+      this.apkPath = '';
+      plus.runtime.install(
+        path,
+        { force: false },
+        () => {
+          console.log('已调起安装');
+        },
+        (e) => {
+          console.log('安装失败:', e);
+          // Android 8.0 及以上需用户在系统设置中开启“允许安装未知应用”,
+          // 该权限属于特殊权限,系统不会弹窗申请,只能引导用户到设置页手动开启。
+          // 注意:不做安装前预检查,部分机型(如小米)已授权时检查接口仍返回未授权
+          if (
+            !fromGuide &&
+            plus.os.name === 'Android' &&
+            parseFloat(plus.os.version) >= 8
+          ) {
+            this.apkPath = path;
+            uni.showModal({
+              title: '需要安装权限',
+              content: '请在接下来打开的页面中,允许本应用“安装未知应用”',
+              showCancel: false,
+              success: () => {
+                var main = plus.android.runtimeMainActivity();
+                var Intent = plus.android.importClass(
+                  'android.content.Intent',
+                );
+                var Uri = plus.android.importClass('android.net.Uri');
+                var intent = new Intent(
+                  'android.settings.MANAGE_UNKNOWN_APP_SOURCES',
+                );
+                intent.setData(Uri.parse('package:' + main.getPackageName()));
+                main.startActivity(intent);
+              },
+            });
+          } else {
+            uni.showModal({
+              title: '安装失败',
+              content:
+                '若已开启“安装未知应用”仍失败,小米手机请检查系统“纯净模式”等安装限制后重新更新',
+              showCancel: false,
+            });
+          }
+        },
+      );
+    },
     // 登录表单提交
     async formSubmit() {
       // 1. 必填校验:用户名

+ 42 - 4
pages/my/about/about.vue

@@ -80,6 +80,7 @@
 				contentA: '',
 				isShow: false, //进度条
 				percentNum: 0, //在线下载进度,
+				apkPath: "", //已下载待安装的更新包路径
 				value: "",
 				updateTF:false
 			}
@@ -158,9 +159,8 @@
 					success: (res) => {
 						console.log(res)
 						if (res.statusCode === 200) {
-							console.log('下载成功');
-							console.log('安装包下载成功,即将安装:' + JSON.stringify(res, null, 4));
-							plus.runtime.openFile(res.tempFilePath);
+							console.log('安装包下载成功,即将安装:' + res.tempFilePath);
+							this.installApk(res.tempFilePath)
 							this.showA = false
 							this.isShow = false
 						}
@@ -176,10 +176,44 @@
 					this.percentNum = res.progress
 					if (res.progress == 100) {
 						console.log('下载完成了')
-						plus.runtime.quit();
 					}
 				});
 			},
+			// 调起系统安装器安装更新包,失败时引导开启“安装未知应用”后自动重试一次
+			// fromGuide:从系统设置页返回后的重试,不再引导,直接提示失败原因
+			installApk(path, fromGuide) {
+				this.apkPath = '';
+				plus.runtime.install(path, { force: false }, () => {
+					console.log('已调起安装')
+				}, (e) => {
+					console.log('安装失败:', e)
+					// Android 8.0 及以上需用户在系统设置中开启“允许安装未知应用”,
+					// 该权限属于特殊权限,系统不会弹窗申请,只能引导用户到设置页手动开启。
+					// 注意:不做安装前预检查,部分机型(如小米)已授权时检查接口仍返回未授权
+					if (!fromGuide && plus.os.name === 'Android' && parseFloat(plus.os.version) >= 8) {
+						this.apkPath = path;
+						uni.showModal({
+							title: '需要安装权限',
+							content: '请在接下来打开的页面中,允许本应用“安装未知应用”',
+							showCancel: false,
+							success: () => {
+								var main = plus.android.runtimeMainActivity();
+								var Intent = plus.android.importClass('android.content.Intent');
+								var Uri = plus.android.importClass('android.net.Uri');
+								var intent = new Intent('android.settings.MANAGE_UNKNOWN_APP_SOURCES');
+								intent.setData(Uri.parse('package:' + main.getPackageName()));
+								main.startActivity(intent);
+							}
+						})
+					} else {
+						uni.showModal({
+							title: '安装失败',
+							content: '若已开启“安装未知应用”仍失败,小米手机请检查系统“纯净模式”等安装限制后重新更新',
+							showCancel: false
+						})
+					}
+				})
+			},
 			record() {
 				uni.navigateTo({
 					url: "../record/record"
@@ -187,6 +221,10 @@
 			}
 		},
 		onShow() {
+			// 从“安装未知应用”设置页返回后,继续安装已下载的更新包
+			if (this.apkPath) {
+				this.installApk(this.apkPath, true)
+			}
 			this.id = plus.runtime.version
 			this.getEquipList()
 		},