maliang 1 mēnesi atpakaļ
vecāks
revīzija
3006ba87b9
4 mainītis faili ar 80 papildinājumiem un 10 dzēšanām
  1. 3 1
      App.vue
  2. 7 1
      common/store/modules/update.js
  3. 57 0
      common/utils/app-update.js
  4. 13 8
      pages/index/index.vue

+ 3 - 1
App.vue

@@ -3,7 +3,9 @@
 	var _codeQueryTag = false;
 export default {
 	onLaunch(options) {
-		
+		// 重置app更新信息
+		uni.setStorageSync('update', false);
+		uni.setStorageSync('downloadApkParams', null);
 		console.log('onLaunch')
 		// 检测客户端更新
 		this.$store.dispatch('update/update', 0)

+ 7 - 1
common/store/modules/update.js

@@ -12,6 +12,7 @@ export default {
 	// 储存数据
 	state: {
 		update: uni.getStorageSync("update") || false,
+		downloadApkParams: uni.getStorageSync("downloadApkParams") || {},
 		data: {},
 		link: {},
 		download: {
@@ -34,6 +35,10 @@ export default {
 		setUpdate(state, data) {
 			state.update = data;
 			uni.setStorageSync('update', data);
+		},	
+		setDownloadApkParams(state, data) {
+			state.downloadApkParams = data;
+			uni.setStorageSync('downloadApkParams', data);
 		},
 	},
 	actions: {
@@ -74,7 +79,8 @@ export default {
 			.then(res=>{
 				if(system_info.appVersion != res.data.version) {
 					commit('setUpdate',true)
-					console.log(state.update,'res')
+					commit('setDownloadApkParams',res.data)
+					// console.log(state.update,'res')
 				}else {
 					commit('setUpdate',false)
 				}

+ 57 - 0
common/utils/app-update.js

@@ -0,0 +1,57 @@
+export function downloadApkWithJsonPost(apiUrl, requestData) {
+  fetch(apiUrl, {
+    method: "POST",
+    headers: {
+      "Content-Type": "application/json",
+    },
+    body: JSON.stringify(requestData),
+  })
+    .then((response) => {
+      if (!response.ok) throw new Error("下载失败");
+      return response.blob(); // 获取二进制 APK 文件
+    })
+    .then((blob) => {
+      saveBlobToFile(blob); // 保存到本地
+    })
+    .catch((err) => {
+      plus.nativeUI.alert("更新失败:" + err.message);
+    });
+}
+
+function saveBlobToFile(blob) {
+  const apkPath = "_doc/update/app.apk";
+
+  plus.io.resolveLocalFileSystemURL("_doc/", function (dirEntry) {
+    dirEntry.getDirectory("update", { create: true }, function (updateDir) {
+      updateDir.getFile("app.apk", { create: true }, function (fileEntry) {
+        fileEntry.createWriter(function (writer) {
+          writer.onwrite = function () {
+            console.log("文件保存成功");
+            installApk(fileEntry.toLocalURL());
+          };
+          writer.onerror = function (e) {
+            plus.nativeUI.alert("保存文件失败:" + e.message);
+          };
+          writer.write(blob);
+        });
+      });
+    });
+  });
+}
+
+function installApk(filepath) {
+  plus.runtime.install(
+    filepath,
+    {
+      force: true,
+    },
+    function () {
+      plus.nativeUI.alert("更新完成,应用即将重启", function () {
+        plus.runtime.restart();
+      });
+    },
+    function (e) {
+      plus.nativeUI.alert("安装失败:" + JSON.stringify(e));
+    }
+  );
+}

+ 13 - 8
pages/index/index.vue

@@ -27,7 +27,7 @@
 		<use-pop :userInfo="userInfo" v-model="meunShow" @close="meunShow = false"></use-pop>
 	
 		<!-- 今日任务有未读 -->
-		<u-popup :show="updateShow" :closeOnClickOverlay="false" :safeAreaInsetBottom="false" mode="center" :round="16"  @close="state.taskNumShow = false">
+		<u-popup :show="showUpdatePop" :closeOnClickOverlay="false" :safeAreaInsetBottom="false" mode="center" :round="16"  @close="state.taskNumShow = false">
 			<view class="reason-box">
 				<image class="icon-img" src="https://qiuyu-shuzhi.oss-cn-beijing.aliyuncs.com/image/task-pop-img.png" mode=""></image>
 				<view class="reason-box-title">象力WMS邀请你升级至新版本</view>
@@ -42,7 +42,7 @@
 
 <script>
 	import usePop from './components/use-pop.vue'
-	import { downloadApk } from '@/common/request/apis/index'
+	import { downloadApkWithJsonPost } from '@/common/utils/app-update.js' 
 	// const Base64 = require('js-base64').Base64
 	import {
 		mapGetters
@@ -88,10 +88,10 @@
 			// JSON.parse(Base64.decode(that.$Route.query.classData))
 		},
 		onShow() {
-			if(this.$store.state.update.update) {
-				console.log('需要更新')
-				this.updateShow = true
-			}
+			// if(this.$store.state.update.update) {
+			// 	console.log('需要更新')
+			// 	this.updateShow = true
+			// }
 			if(!this.isLogin) {
 				uni.$u.toast("请先登录!");
 				setTimeout(()=>{
@@ -102,13 +102,18 @@
 			}
 		},
 		computed: {
-			...mapGetters(['isLogin','userInfo'])
+			...mapGetters(['isLogin','userInfo']),
+			showUpdatePop(){
+				return this.$store.state.update.update&&this.updateShow
+			}
 		},
 		watch: {
 		},
 		methods: {
 			toUpdate() {
-				
+				const params = this.$store.state.downloadApkParams
+				const url = this.$BASE_URL + '/pda/downloadApk'
+				downloadApkWithJsonPost(url, params)
 			},
 			meunClick() {
 				if(!this.isLogin) {