浏览代码

app在线版本更新

lxh 1 天之前
父节点
当前提交
c4d242da82

+ 152 - 20
App.vue

@@ -6,6 +6,7 @@
 import Vue from "vue";
 import appUpdate from "common/util/appUpdate.js";
 import configService from "@/common/service/config.service.js";
+import api from "@/api/api";
 export default {
   data() {
     return {
@@ -13,25 +14,12 @@ export default {
     }
   },
   onLaunch: function () {
+    //app在线版本更新
+    this.checkVersion()
+    // this.getVersionFn()
     //消息推送
-    uni.getPushClientId({
-      //获取客户端推送标识
-      success: (res) => {
-        console.log(res.cid, 'cid---');
-        uni.setStorageSync('push_cid', res.cid); // 存储CID供后续使用
-      },
-      fail(err) {
-        console.log(err)
-      }
-    });
-  
-    // uni.onPushMessage((res) => {
-    //   //客户端监听推送消息
-    //   console.log("收到推送消息:", res) //监听推送消息
-    //   if (res.data) {
-    //     uni.createPushMessage(res.data)  //收到消息推送,创建系统状态栏及锁屏通 知
-    //   }
-    // })
+    this.getPushClientID()
+
 
     const savedIP = uni.getStorageSync("selectedIP");
     if (configService.apiUrl !== "") {
@@ -303,13 +291,157 @@ export default {
   },
   onShow: function () {
     console.log("App Show");
-  
+
   },
   onHide: function () {
     console.log("App Hide");
     clearInterval(this.$store.state.timer); // 假设定时器存储在 Vuex 的状态管理中
   },
-  methods: {}
+  methods: {
+    //消息推送
+    getPushClientID() {
+      uni.getPushClientId({
+        //获取客户端推送标识
+        success: (res) => {
+          console.log(res.cid, 'cid---');
+          uni.setStorageSync('push_cid', res.cid); // 存储CID供后续使用
+        },
+        fail(err) {
+          console.log(err)
+        }
+      });
+    },
+    // 检测版本
+    checkVersion() {
+      plus.runtime.getProperty(plus.runtime.appid, (appInfo) => {
+        // 获取版本号
+        const currentVersion = appInfo.version
+        // 这是用来看看版本是多少的
+        console.log(currentVersion, '版本号');
+        this.getVersionFn(currentVersion)
+      });
+    },
+    getVersionFn(currentVersion) {
+      let that = this
+      new Promise((resolve, reject) => {
+        api
+          .getAppLast({})
+          .then((response) => {
+            if (response.statusCode == 200) {
+              console.log(response, '响应数据')
+              const updateUrl = `${configService.apiUrlP}:9999${response.data.downloadUrl}`;  // 下载地址
+              let indexF = response.data.filename.lastIndexOf('-')
+              let indexL = response.data.filename.lastIndexOf('.')
+              const latestVersion = response.data.filename.substring(indexF + 1, indexL)// 版本号
+              console.log(updateUrl, '下载地址')
+              console.log(latestVersion, '版本号')
+              // 比较版本号,判断是否有新版本
+              if (that.compareVersion(currentVersion, latestVersion) < 0) {
+                // 提示用户更新
+                uni.showModal({
+                  title: '更新提示',
+                  content: '检测到新版本,是否立即更新?',
+                  success: (modalRes) => {
+                    if (modalRes.confirm) {
+                      // 用户确认更新,下载新版本
+                      that.downloadAndInstall(updateUrl);
+                    }
+                  }
+                });
+              } else {
+                console.log('当前已是最新版本');
+              }
+
+            } else {
+              reject(response);
+            }
+          })
+          .catch((error) => {
+            console.log("catch===>response", response);
+            reject(error);
+          });
+      });
+    },
+
+    // 比较版本号
+    compareVersion(v1, v2) {
+      const version1 = v1.split('.').map(Number);
+      const version2 = v2.split('.').map(Number);
+
+      for (let i = 0; i < version1.length; i++) {
+        if (version1[i] < version2[i]) {
+          return -1;
+        } else if (version1[i] > version2[i]) {
+          return 1;
+        }
+      }
+      return 0;
+    },
+
+    // 下载并安装新版本
+    downloadAndInstall(url) {
+      let dtask = plus.downloader.createDownload(url, {
+        filename: '_downloads/temporary.apk' //利用保存路径,实现下载文件的重命名
+      },
+        function (d, status) {
+          // 下载完成  
+          if (status == 200) {
+            plus.runtime.install(plus.io.convertLocalFileSystemURL(
+              d.filename), {}, {}, function (error) {
+                uni.showToast({
+                  title: '安装失败',
+                  duration: 1500
+                });
+              })
+          } else {
+            uni.showToast({
+              title: '更新失败',
+              duration: 1500
+            });
+          }
+        });
+      try {
+        console.log('开始下载');
+        dtask.start(); // 开启下载的任务
+        let prg = 0;
+        let showLoading = plus.nativeUI.showWaiting(
+          "正在下载"); //创建一个showWaiting对象 
+        dtask.addEventListener('statechanged', function (
+          task,
+          status
+        ) {
+          // 给下载任务设置一个监听 并根据状态  做操作
+          switch (task.state) {
+            case 1:
+              showLoading.setTitle("正在下载");
+              break;
+            case 2:
+              showLoading.setTitle("已连接到服务器");
+              break;
+            case 3:
+              prg = parseInt(
+                (parseFloat(task.downloadedSize) /
+                  parseFloat(task.totalSize)) *
+                100
+              );
+              showLoading.setTitle("  正在下载" + prg + "%  ");
+              break;
+            case 4:
+              plus.nativeUI.closeWaiting();
+              //下载完成
+              break;
+          }
+        });
+      } catch (err) {
+        plus.nativeUI.closeWaiting();
+        uni.showToast({
+          title: '更新失败',
+          mask: false,
+          duration: 1500
+        });
+      }
+    }
+  }
 };
 </script>
 

+ 6 - 0
api/api.js

@@ -25,6 +25,12 @@ const apiService = {
     return http.get(url);
   },
 
+  //获取App在线版本更新下载链接
+  getAppLast(params) {
+    const url = buildURL("/safety/app/last", params);
+    return http.get(url);
+  },
+
   /**
    * 单点登录
    */

+ 1 - 1
common/service/config.service.js

@@ -12,7 +12,7 @@ if (process.env.NODE_ENV == "development") {
 let staticDomainURL = BASE_URL + "/sys/common/static";
 const configService = {
   apiUrl: BASE_URL || uni.getStorageSync("apiUrl"),
-  apiUrlP: "http://10.246.167.205",
+  apiUrlP: "http://182.92.126.35",
   // apiUrlP: 'http://182.92.126.35:6006',
   staticDomainURL: staticDomainURL,
 };

+ 1 - 0
package.json

@@ -16,6 +16,7 @@
     ]
   },
   "dependencies": {
+    "crypto-js": "^4.2.0",
     "formdata-polyfill": "^4.0.10"
   }
 }

+ 1 - 1
pages/gasreport/components/gasImgIdentify.vue

@@ -291,7 +291,7 @@
 import api from "@/api/api";
 import moment from "moment";
 import configService from "@/common/service/config.service.js";
-import { RHFselcet } from "@/uni_modules/fz-media-selcet";
+// import { RHFselcet } from "@/uni_modules/fz-media-selcet";
 import helangCompress from "@/components/helang-compress/helang-compress";
 export default {
   name: "gasFill",

+ 18 - 0
uniCloud-aliyun/database/testPush/index.js

@@ -0,0 +1,18 @@
+'use strict';
+ 
+const { log } = require("console");
+ 
+const uniPush = uniCloud.getPushManager({appId:"__UNI__2000523"}) //注意这里需要传入你的应用appId,用于指定接收消息的客户端
+exports.main = async (event, context) => {
+	console.log('event',event);
+	console.log('context',context); 
+	const pushParam=JSON.parse(event.body)
+	return await uniPush.sendMessage({
+		...pushParam,
+		"options":{
+			"HW":{
+				"/message/android/category":"WORK"
+			}
+		}
+	})
+};

+ 8 - 0
uniCloud-aliyun/database/testPush/package.json

@@ -0,0 +1,8 @@
+{
+  "name": "testPush",
+  "dependencies": {},
+  "extensions": {
+    "uni-cloud-jql": {},
+    "uni-cloud-push": {}
+  }
+}