Просмотр исходного кода

fix: 优化安卓应用更新安装流程,适配未知应用权限

leo 12 часов назад
Родитель
Сommit
6b1cdce1f4
3 измененных файлов с 100 добавлено и 30 удалено
  1. 2 2
      manifest.json
  2. 57 17
      pages/login/login.vue
  3. 41 11
      pages/my/about/about.vue

+ 2 - 2
manifest.json

@@ -2,8 +2,8 @@
     "name" : "云飞智控",
     "name" : "云飞智控",
     "appid" : "__UNI__DBA6730",
     "appid" : "__UNI__DBA6730",
     "description" : "",
     "description" : "",
-    "versionName" : "1.15.31",
-    "versionCode" : 11531,
+    "versionName" : "1.15.35",
+    "versionCode" : 11535,
     "transformPx" : false,
     "transformPx" : false,
     "sassImplementationName" : "node-sass",
     "sassImplementationName" : "node-sass",
     /* 5+App特有相关 */
     /* 5+App特有相关 */

+ 57 - 17
pages/login/login.vue

@@ -179,6 +179,7 @@ export default {
       contentA: '',
       contentA: '',
       isShow: false,
       isShow: false,
       percentNum: 0,
       percentNum: 0,
+      apkPath: '',
       passvalue: false,
       passvalue: false,
       turnover: true,
       turnover: true,
       agree: false,
       agree: false,
@@ -231,6 +232,12 @@ export default {
         this.turnover = res.data;
         this.turnover = res.data;
       },
       },
     });
     });
+    // 从“安装未知应用”设置页返回后,继续安装已下载的更新包
+    // #ifdef APP-PLUS
+    if (this.apkPath) {
+      this.installApk(this.apkPath, true);
+    }
+    // #endif
     this.getEquipList();
     this.getEquipList();
   },
   },
   methods: {
   methods: {
@@ -333,23 +340,7 @@ export default {
           console.log(res);
           console.log(res);
           if (res.statusCode === 200) {
           if (res.statusCode === 200) {
             console.log('安装包下载成功,即将安装:' + res.tempFilePath);
             console.log('安装包下载成功,即将安装:' + res.tempFilePath);
-            // 调起系统安装器安装 apk(需在系统设置中允许本应用“安装未知应用”)
-            plus.runtime.install(
-              res.tempFilePath,
-              { force: false },
-              () => {
-                console.log('已调起安装');
-              },
-              (e) => {
-                console.log('安装失败:', e);
-                uni.showModal({
-                  title: '安装失败',
-                  content:
-                    '请在系统设置中允许本应用“安装未知应用”权限后重试',
-                  showCancel: false,
-                });
-              },
-            );
+            this.installApk(res.tempFilePath);
             this.showA = false;
             this.showA = false;
             this.isShow = false;
             this.isShow = false;
           }
           }
@@ -368,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() {
     async formSubmit() {
       // 1. 必填校验:用户名
       // 1. 必填校验:用户名

+ 41 - 11
pages/my/about/about.vue

@@ -80,6 +80,7 @@
 				contentA: '',
 				contentA: '',
 				isShow: false, //进度条
 				isShow: false, //进度条
 				percentNum: 0, //在线下载进度,
 				percentNum: 0, //在线下载进度,
+				apkPath: "", //已下载待安装的更新包路径
 				value: "",
 				value: "",
 				updateTF:false
 				updateTF:false
 			}
 			}
@@ -159,17 +160,7 @@
 						console.log(res)
 						console.log(res)
 						if (res.statusCode === 200) {
 						if (res.statusCode === 200) {
 							console.log('安装包下载成功,即将安装:' + res.tempFilePath);
 							console.log('安装包下载成功,即将安装:' + res.tempFilePath);
-							// 调起系统安装器安装 apk(需在系统设置中允许本应用“安装未知应用”)
-							plus.runtime.install(res.tempFilePath, { force: false }, () => {
-								console.log('已调起安装')
-							}, (e) => {
-								console.log('安装失败:', e)
-								uni.showModal({
-									title: '安装失败',
-									content: '请在系统设置中允许本应用“安装未知应用”权限后重试',
-									showCancel: false
-								})
-							})
+							this.installApk(res.tempFilePath)
 							this.showA = false
 							this.showA = false
 							this.isShow = false
 							this.isShow = false
 						}
 						}
@@ -188,6 +179,41 @@
 					}
 					}
 				});
 				});
 			},
 			},
+			// 调起系统安装器安装更新包,失败时引导开启“安装未知应用”后自动重试一次
+			// 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() {
 			record() {
 				uni.navigateTo({
 				uni.navigateTo({
 					url: "../record/record"
 					url: "../record/record"
@@ -195,6 +221,10 @@
 			}
 			}
 		},
 		},
 		onShow() {
 		onShow() {
+			// 从“安装未知应用”设置页返回后,继续安装已下载的更新包
+			if (this.apkPath) {
+				this.installApk(this.apkPath, true)
+			}
 			this.id = plus.runtime.version
 			this.id = plus.runtime.version
 			this.getEquipList()
 			this.getEquipList()
 		},
 		},