浏览代码

feat: 更新

liming 2 周之前
父节点
当前提交
616e7e9112
共有 4 个文件被更改,包括 63 次插入53 次删除
  1. 9 14
      src/App.vue
  2. 39 26
      src/updater/index.js
  3. 4 4
      src/updater/update.json
  4. 11 9
      updataSetVersion.js

+ 9 - 14
src/App.vue

@@ -8,11 +8,11 @@
     :style="notchStyle"
   />
   <div class="main" :style="appMainViewStyle">
-    <RouterView  />
+    <RouterView />
   </div>
 </template>
 
-<script setup>  
+<script setup>
 import { checkAndUpdate } from "@/updater/index";
 
 import { getNotchHeight } from "@/utils/statusBar";
@@ -20,14 +20,14 @@ const route = useRoute();
 const router = useRouter();
 
 const height = ref(0);
-const notchStyle = ref({}); 
+const notchStyle = ref({});
 
 const appMainViewStyle = computed(() => {
-  let mainHeight = 0
-  if (route.meta.navbar)  mainHeight = height.value + 46;
-  if (route.meta.tabbar) mainHeight = mainHeight + 60
-  return { 
-    height: `calc(100vh - ${mainHeight}px)`
+  let mainHeight = 0;
+  if (route.meta.navbar) mainHeight = height.value + 46;
+  if (route.meta.tabbar) mainHeight = mainHeight + 60;
+  return {
+    height: `calc(100vh - ${mainHeight}px)`,
   };
 });
 
@@ -55,13 +55,8 @@ onBeforeMount(async () => {
     paddingTop: `${height.value}px`,
   };
 
-
- 
+  checkAndUpdate();
 });
-
-onMounted(() => { 
-  checkAndUpdate()
-})
 </script>
 
 <style lang="less">

+ 39 - 26
src/updater/index.js

@@ -1,3 +1,4 @@
+import { Capacitor } from "@capacitor/core";
 import { CapacitorUpdater } from "@capgo/capacitor-updater";
 import { App } from "@capacitor/app";
 import semver from "semver";
@@ -8,32 +9,34 @@ const UPDATE_URL = HOST + "update.json";
 
 // 检查并更新: shasum -a 256
 export async function checkAndUpdate() {
+  console.log("检测更新");
   // 判断是web 还是 ios
   if (Capacitor.getPlatform() === "web") {
     console.log("web 端不支持更新");
     return;
   }
+  CapacitorUpdater.notifyAppReady();
+  checkForUpgrade();
+}
 
+// 更新
+const checkForUpgrade = async () => {
   // 1. 拉取元数据
+  console.log("拉取元数据");
   const meta = await fetch(UPDATE_URL).then((r) => r.json());
   console.log("更新", meta);
 
-  // 2. 基本校验
-  // 判断是ios 
-  if (Capacitor.getPlatform() === "ios") {
-    await CapacitorUpdater.ready(); // 等插件完全就绪
-  }else{
-    await new Promise(r => setTimeout(r, 3000));
-  }
   const current = await CapacitorUpdater.current();
   const { version } = await App.getInfo();
-
   console.log("版本:", current.bundle.version, meta.version);
-  if (!current.bundle.version ||  current.bundle.version == "builtin") {
+  if (!current.bundle.version) {
     console.log("无新版本!");
     return;
   }
-  if (semver.gte(current.bundle.version, meta.version)) {
+  if (
+    current.bundle.version != "builtin" &&
+    semver.gte(current.bundle.version, meta.version)
+  ) {
     console.log("无新版本");
     return;
   }
@@ -45,23 +48,33 @@ export async function checkAndUpdate() {
   const update = await CapacitorUpdater.download({
     url: `${HOST}v${meta.version}.zip`,
     version: meta.version,
-    checksum: meta.checksum,
+    // checksum: meta.checksum,
   });
 
-  // 通知
-  if (meta.mandatory) {
-    await showDialog({
-      title: `v ${meta.version} 已發布`,
-      confirmButtonText: "立即體驗",
-      message: meta.upDataDescription,
-    }).then(async () => {
-      await CapacitorUpdater.set(update); // 设置新版本
-      console.log("✅ 已切换到新版本,准备重启");
-      await App.exitApp(); // 冷启动加载新 bundle
+  try {
+    console.log("下载地址:", `${HOST}v${meta.version}.zip`);
+    const update = await CapacitorUpdater.download({
+      url: `${HOST}v${meta.version}.zip`,
+      version: meta.version,
+      checksum: meta.checksum,
     });
-  } else {
-    await CapacitorUpdater.set(update); // 设置新版本
-    console.log("✅ 已切换到新版本,准备重启");
-    await App.exitApp(); // 冷启动加载新 bundle
+
+    // 成功
+    console.log("✅ 下载成功", update);
+
+    // 通知
+    if (meta.mandatory) {
+      showDialog({
+        title: `v ${meta.version} 已發布`,
+        confirmButtonText: "立即體驗",
+        message: meta.upDataDescription,
+      }).then(async () => {
+        await CapacitorUpdater.set(update); // 设置新版本
+      });
+    }else{
+      await CapacitorUpdater.set(update); // 设置新版本
+    }
+  } catch (err) {
+    console.error("❌ 下载失败", err.message || err);
   }
-}
+};

+ 4 - 4
src/updater/update.json

@@ -1,8 +1,8 @@
 {
-  "version": "1.0.9",
-  "releaseDate": "2025-07-29 12:00:00", 
-  "checksum": "dbe314c14114dfce518bbc876ef71cf0827871250d88e5a20e54800f79097eca",
+  "version": "1.0.26",
+  "releaseDate": "2025-07-29 12:00:00",
+  "checksum": "2658f657ef5dfeabf6b4fbd97d1da6b80c3752284fd889c4fcc17718f66f1fbb",
   "minBinaryVersion": "1.0.0",
   "mandatory": true,
-  "upDataDescription":"✨修正一些錯誤。。。。!!!"
+  "upDataDescription": "✨修正一些錯誤。。。。!!!"
 }

+ 11 - 9
updataSetVersion.js

@@ -1,6 +1,8 @@
 import fs from "fs";
 import AdmZip from "adm-zip";
 import crypto from "crypto";
+import semver from "semver";
+
 
 // 读取 update.json
 const updateJsonPath = "bin/update.json"
@@ -8,7 +10,9 @@ const upDataJson = fs.readFileSync("src/updater/update.json", "utf8");
 let updata = JSON.parse(upDataJson);
 
 console.log(updata.version);
-
+// updata.version 加1 
+updata.version = semver.inc(updata.version, "patch");
+ 
 // 判断有没有bin 文件夹没有,就创建一个,有的话就删除bin中的所有内容
 if (!fs.existsSync("bin")) {
   fs.mkdirSync("bin");
@@ -24,14 +28,9 @@ if (fs.existsSync("dist")) {
   const files = fs.readdirSync("dist");
   const zip = new AdmZip();
   const zipName = `v${updata.version}.zip`;
-  // 添加所有文件到 ZIP
-  files.forEach((file) => {
-    const filePath = `dist/${file}`;
-    if (fs.statSync(filePath).isFile()) {
-      // 确保是文件(非子目录)
-      zip.addLocalFile(filePath);
-    }
-  });
+
+  zip.addLocalFolder('dist', '');   // 第二个参数 '' 表示把 dist 本身作为根目录
+ 
 
   // 保存 ZIP 文件
   const zipPath = `bin/${zipName}`;
@@ -46,7 +45,10 @@ if (fs.existsSync("dist")) {
   // 4. 修改 update.json 的 checksum
   updata.checksum = hash; // 假设 update.json 有 checksum 字段
   fs.writeFileSync(updateJsonPath, JSON.stringify(updata, null, 2)); // 2 空格缩进
+  fs.writeFileSync("src/updater/update.json", JSON.stringify(updata, null, 2)); // 2 空格缩进
   console.log(`已更新 ${updateJsonPath} 的 checksum: ${hash}`);
+  
+ 
 } else {
   console.log("dist 文件夹不存在,跳过打包");
 }