From 277d6652893fcc10fb6827b62ef16fe2910a525c Mon Sep 17 00:00:00 2001
From: zhouwenxuan <1175765986@qq.com>
Date: 星期五, 15 十二月 2023 16:58:25 +0800
Subject: [PATCH] 强制更新功能

---
 pages/index/index.vue |   89 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 85 insertions(+), 4 deletions(-)

diff --git a/pages/index/index.vue b/pages/index/index.vue
index 2b9226a..702cfa6 100644
--- a/pages/index/index.vue
+++ b/pages/index/index.vue
@@ -17,15 +17,16 @@
 				<u-form-item style="margin-top: 10px;width: 75%;">
 					<u-button :loading="isLogining" type="primary" style="width: 100%;" @click="Login">登录</u-button>
 					</u-form-item>
-				<view style="color: white;margin-top: 150px;">技术支持:中国科学院</view>
+				<view style="color: white;margin-top: 150px;">技术支持:技术保障部</view>
 			</u-form>
 		</view>
 	</view>
 </template>
 
 <script>
-	import {login} from '../../api/index.js';
+	import {login,getLastApp} from '../../api/index.js';
 	import store from '@/store/index.js'
+	import VUE_APP_BASE_URL from '../../common/constant.js'
 	const jpushModule = uni.requireNativePlugin('JG-JPush')
 	export default {
 		data() {
@@ -39,7 +40,7 @@
 				//极光推送
 				connectStatus: '未连接',
 				registrationID: '未获得',
-				isLogining: false
+				isLogining: false,
 			}
 		},
 		onShow() {
@@ -71,6 +72,7 @@
 		},
 		onLoad() {
 			this.connect()
+			this.getMaxVersion()
 		},
 		methods: {
 			Login(){
@@ -146,7 +148,86 @@
 					uni.setStorageSync('registrationID', registerID);
 					console.log("iddd",uni.getStorageSync('registrationID'))
 				})
-			}
+			},
+			    // 获取最新版本
+			    getMaxVersion() {
+			      uni.getSystemInfo({
+			        success: (res) => {
+					  getLastApp().then(res => {
+						  if (res.code === 100) {
+							  console.log(res,'res');
+							  this.updateVersion(res.data);
+						  }
+					  })
+			        },
+			      });
+			    },
+				
+			    // 版本对比
+			    updateVersion(data) {
+				  const lastVersion = data.version; //最新版本
+			      const curVersion = uni.getStorageSync("version"); // 当前版本
+				  console.log("lastVersion",lastVersion)
+				  console.log("curVersion",curVersion)
+			      if (curVersion && lastVersion) {
+					  const curNum =  parseInt(curVersion.replace(/[,|.]/g, ''))
+					  const lastNum =  parseInt(lastVersion.replace(/[,|.]/g, ''))
+			        if (lastNum > curNum) {
+			          this.showFly(data.attachmentInfo.fileUrl,lastVersion);
+			        }
+			      }
+			    },
+					// 版本下载 升级
+			    showFly(url,lastVersion) {
+					console.log("url",url)
+			      uni.showModal({
+			        title: "提示",
+			        content: `发现新版本${lastVersion},立即升级!`,
+			        showCancel: false, // 如果是强制更新就不显示取消按钮
+			        success: (e) => {
+			          if (e.confirm) {
+			            uni.showLoading({
+			              title: "更新中……",
+			            });
+			            const downloadTask = uni.downloadFile({
+			              url: VUE_APP_BASE_URL + url, // 这个是最新版本apk包的地址
+			              success: (res) => {
+			                uni.hideLoading();
+			                if (res.statusCode === 200) {
+								console.log("url",VUE_APP_BASE_URL + url)
+			                  plus.runtime.install(res.tempFilePath, { force: true }, _res => {
+			                      uni.showToast({
+			                        title: "更新成功,重启中",
+			                        duration: 1600,
+			                      });
+			                      plus.runtime.restart();
+			                      uni.hideToast();
+			                    }
+			                  );
+			                } else {
+			                  uni.showToast({
+			                    title: "下载失败!",
+			                    icon: "none",
+			                    duration: 800,
+			                  });
+			                }
+			              },
+			            });
+			            // downloadTask.onProgressUpdate((res) => {
+			            //   // _this.startDown = true;
+			            //   // _this.calcPro(res.progress);
+			            //   // console.log('下载进度' + res.progress);
+			            //   // console.log('已经下载的数据长度' + res.totalBytesWritten);
+			            //   // console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
+			            //   // 测试条件,取消下载任务。
+			            //   // if (res.progress > 50) {
+			            //   //     downloadTask.abort();
+			            //   // }
+			            // });
+			          }
+			        },
+			      });
+			    }
 		}
 	}
 </script>

--
Gitblit v1.9.2