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

app新版本更新流程代码优化

@dongkboy 6 дней назад
Родитель
Сommit
c1adff5a02
4 измененных файлов с 100 добавлено и 52 удалено
  1. 15 9
      components/uni-Cell/uni-Cell.vue
  2. 58 37
      components/yzhua006-update/app-update.vue
  3. 23 0
      pages/lottery/index.vue
  4. 4 6
      pages/my/my.vue

+ 15 - 9
components/uni-Cell/uni-Cell.vue

@@ -20,8 +20,8 @@
 				</view>
 			</view>
 		</view>
-		<!-- 更新组件 force 是否强制更新   tabbar:页面是否有原生tabbar组件-->
-		<app-update ref="app_update" @status="updateStatus"></app-update>
+		<!-- 更新组件:结果通过 update() 的返回值拿回,不再依赖事件桥接 -->
+		<app-update ref="app_update"></app-update>
 	</view>
 </template>
 
@@ -58,13 +58,9 @@
 		data() {
 			return {
 				hasUpdate: false,
+				latestVersion: '', // 检测到的最新版本号
 			};
 		},
-		watch: {
-			hasUpdate(newVal, oldVal) {
-				this.hasUpdate = newVal
-			},
-		},
 		computed: {
 			...mapState(['userInfo']),
 		},
@@ -91,7 +87,13 @@
 						break;
 					case 'checkVersion': //检查版本
 						//#ifdef APP-PLUS
-						this.$refs.app_update.update(); //调用子组件检查更新方法
+						// 直接通过 update() 的返回值拿检查结果,不走事件桥接
+						// 返回值结构:{ hasUpdate, latestVersion }
+						this.$refs.app_update.update().then((res) => {
+							this.updateStatus(res);
+						}).catch((e) => {
+							console.error('[checkVersion] 检查更新失败:', e);
+						});
 						//#endif
 						// 非app端不检查更新
 						//#ifdef H5
@@ -136,8 +138,12 @@
 					default:
 				}
 			},
+			// 检查结果回调:item = { hasUpdate, latestVersion }
 			updateStatus(item) {
-				this.hasUpdate = item;
+				this.hasUpdate = item.hasUpdate;
+				if (item.latestVersion) {
+					this.latestVersion = item.latestVersion;
+				}
 				if (!this.hasUpdate) {
 					uni.showToast({
 						title: '已经是最新版本',

+ 58 - 37
components/yzhua006-update/app-update.vue

@@ -125,53 +125,74 @@
 				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
 					vm.version = inf.version
 				});
-				vm.getUpdateInfo(); //获取最新版本信息
 				// #endif
-				vm.getUpdateInfo(); //获取最新版本信息
+				// 返回 Promise,结果 = { hasUpdate, latestVersion }
+				return vm.getUpdateInfo();
 			},
 			// 获取线上版本信息
 			async getUpdateInfo() {
-				//向后台发起请求,获取最新版本号
-				let apkquote = await this.$http.get('/version/getMaxVersion', {
-					appid: "__UNI__05D695B",
-					packageType: "apk",
-				});
-				if (apkquote.code == 200 && apkquote.data) {
-					vm.apkInfo = apkquote.data;
-					vm.apkLatestVersion = apkquote.data.version;
-				}
-				let wgtquote = await this.$http.get('/version/getMaxVersion', {
-					packageType: "wgt",
-					appid: "__UNI__05D695B",
-				});
-				if (wgtquote.code == 200 && wgtquote.data) {
-					vm.wgtInfo = wgtquote.data;
-					vm.wgtLatestVersion = wgtquote.data.version;
+				try {
+					//向后台发起请求,获取最新版本号
+					let apkquote = await this.$http.get('/version/getMaxVersion', {
+						appid: "__UNI__05D695B",
+						packageType: "apk",
+					});
+					if (apkquote.code == 200 && apkquote.data) {
+						vm.apkInfo = apkquote.data;
+						vm.apkLatestVersion = apkquote.data.version;
+					}
+					let wgtquote = await this.$http.get('/version/getMaxVersion', {
+						packageType: "wgt",
+						appid: "__UNI__05D695B",
+					});
+					if (wgtquote.code == 200 && wgtquote.data) {
+						vm.wgtInfo = wgtquote.data;
+						vm.wgtLatestVersion = wgtquote.data.version;
+					}
+				} catch (e) {
+					// 接口异常不能静默断链:记录错误并继续按“无更新”往下走
+					console.error('[app-update] 版本接口请求异常:', e);
 				}
-				vm.checkUpdate(); //检查是否更新
-
+				return vm.checkUpdate(); //检查是否更新并返回结果
 			},
-			// 检查是否更新
+			// 检查是否更新,结果统一为 { hasUpdate, latestVersion }
 			checkUpdate() {
-				if (vm.version) {
-					let apkneed = vm.compareVersion(vm.version, vm.apkLatestVersion); // 检查包版本
+				if (!vm.version) {
+					// 版本号尚未获取到,按无更新处理
+					this.$emit('status', {
+						hasUpdate: false,
+						latestVersion: ''
+					})
+					return {
+						hasUpdate: false,
+						latestVersion: ''
+					};
+				}
+				let has = false; // 是否有可更新的新版本
+				let apkneed = vm.compareVersion(vm.version, vm.apkLatestVersion); // 检查包版本
+				if (apkneed && vm.apkInfo.appType != 0) {
+					vm.update_info = vm.apkInfo;
+					has = true;
+				} else {
 					let wgtneed = vm.compareVersion(vm.version, vm.wgtLatestVersion); // 检查资源版本
-					if (apkneed) {
-						vm.update_info = vm.apkInfo;
-						if (vm.update_info.appType != 0) {
-							vm.popup_show = true; //线上版本号大于当前安装的版本号  显示升级框
-							this.$emit('status', vm.popup_show)
-						}
-					} else {
-						if (wgtneed) {
-							vm.update_info = vm.wgtInfo;
-							if (vm.update_info.appType != 0) {
-								vm.popup_show = true; //线上版本号大于当前安装的版本号  显示升级框
-								this.$emit('status', vm.popup_show)
-							}
-						}
+					if (wgtneed && vm.wgtInfo.appType != 0) {
+						vm.update_info = vm.wgtInfo;
+						has = true;
 					}
 				}
+				let latestVersion = '';
+				if (has) {
+					vm.popup_show = true; //线上版本号大于当前安装的版本号  显示升级框
+					// 把可更新的最新版本号一起带回去
+					latestVersion = (vm.update_info && vm.update_info.version) || '';
+				}
+				const result = {
+					hasUpdate: has,
+					latestVersion: latestVersion
+				};
+				// 事件与返回值双通道:事件供页面级实例使用,返回值供组件内直接拿结果
+				this.$emit('status', result)
+				return result;
 			},
 
 			// 取消更新

+ 23 - 0
pages/lottery/index.vue

@@ -21,6 +21,29 @@
 		computed: {
 			...mapState(['userInfo', 'system_code']),
 		},
+		onShow() {
+			// #ifdef APP-PLUS
+			// 设置webview显示时顶部丢失问题
+			var height = 0; //定义动态的高度变量,如高度为定值,可以直接写
+			uni.getSystemInfo({
+				//成功获取的回调函数,返回值为系统信息
+				success: (sysinfo) => {
+					height = sysinfo.windowHeight -
+						40; //自行修改,自己需要的高度 此处如底部有其他内容,可以直接---(-50)这种,如果不减,页面会下沉,看不到底部导航栏
+				},
+				complete: () => {}
+			});
+			var currentWebview = this.$scope.$getAppWebview(); //获取当前web-view
+			setTimeout(function() {
+				var wv = currentWebview.children()[0];
+				wv.setStyle({ //设置web-view距离顶部的距离以及自己的高度,单位为px
+					top: 40, //此处是距离顶部的高度,应该是你页面的头部
+					height: height, //webview的高度
+					scalable: false, //webview的页面是否可以缩放,双指放大缩小,
+				})
+			}, 500); //如页面初始化调用需要写延迟
+			// #endif
+		},
 		onLoad(options) {
 			// jzg-7kP9xL2mN5vQ8zR1jW  ,晋掌柜
 			// gyy-aB4cD6eF8gH0iJ2kL4  ,广誉源

+ 4 - 6
pages/my/my.vue

@@ -86,7 +86,7 @@
 								<view class="newtag dis a-c j-c">
 									New
 								</view>
-								<text style="font-size: 28rpx;color: #666;">{{ version || '1.0.0' }}</text>
+								<text style="font-size: 28rpx;color: #666;">{{ latestVersion || '1.0.0' }}</text>
 							</template>
 						</view>
 					</template>
@@ -112,7 +112,6 @@
 		},
 		data() {
 			return {
-				version: "", //版本
 				dateShow: false,
 				hasUpdate: false, //有无更新
 				ToolsList: [{
@@ -185,9 +184,6 @@
 		async onLoad() {
 
 			// #ifdef APP-PLUS
-			plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
-				this.version = widgetInfo.version;
-			})
 			setTimeout(() => {
 				this.$refs.app_update.update(); //调用子组件检查更新方法
 			}, 500)
@@ -201,8 +197,10 @@
 			...mapState(['userInfo']),
 		},
 		methods: {
+			// 检查结果回调:item = { hasUpdate, latestVersion }
 			updateStatus(item) {
-				this.hasUpdate = item;
+				this.hasUpdate = item.hasUpdate;
+				this.latestVersion = item.latestVersion;
 			},
 			//我的业绩
 			async userPerformance() {