Browse Source

feat: 无感更新

liming 2 weeks ago
parent
commit
32f67d6dc4
8 changed files with 99 additions and 43 deletions
  1. 8 0
      README.md
  2. 1 0
      android/app/capacitor.build.gradle
  3. 7 2
      capacitor.config.ts
  4. 2 0
      package.json
  5. 8 4
      src/App.vue
  6. 0 37
      src/hooks/updataApp.js
  7. 62 0
      src/updater/index.js
  8. 11 0
      src/updater/update.json

+ 8 - 0
README.md

@@ -86,4 +86,12 @@ ext {
     androidxEspressoCoreVersion = '3.6.1'
     cordovaAndroidVersion = '10.1.1'
 }
+```
+
+```
+{
+  "version": "1.0.2",               // 新 JS bundle 的版本号
+  "url": "https://yourdomain.com/updates/v1.0.2.zip", // 真实 ZIP 包地址
+  "minBinaryVersion": "1.0.0"       // 要求原生壳最低版本
+}
 ```

+ 1 - 0
android/app/capacitor.build.gradle

@@ -18,6 +18,7 @@ dependencies {
     implementation project(':capacitor-push-notifications')
     implementation project(':capacitor-status-bar')
     implementation project(':capacitor-toast')
+    implementation project(':capgo-capacitor-updater')
 
 }
 

+ 7 - 2
capacitor.config.ts

@@ -1,7 +1,7 @@
 import { CapacitorConfig } from '@capacitor/cli';
-// import { updateVersion } from './scripts/sync-version';
+import { updateVersion } from './scripts/sync-version';
 
-// updateVersion()
+updateVersion()
 
 
 
@@ -21,6 +21,11 @@ const config: CapacitorConfig = {
     // 注意:原JSON中嵌套了重复的"plugins"键,已修正
     PushNotifications: {
       presentationOptions: ['badge', 'sound', 'alert']
+    },
+
+    CapacitorUpdater: {
+      autoUpdate: false, // 自行控制 
+      statsUrl: '',   // 统计上报: POST /api/stats 接收 JSON(内容是插件的事件日志),返回 200 即可。
     }
   },
   // iOS专属配置

+ 2 - 0
package.json

@@ -31,6 +31,7 @@
     "@capacitor/push-notifications": "^7.0.1",
     "@capacitor/status-bar": "^7.0.1",
     "@capacitor/toast": "^7.0.1",
+    "@capgo/capacitor-updater": "^7.8.7",
     "axios": "^1.10.0",
     "clipboard": "^2.0.11",
     "crypto-js": "^4.2.0",
@@ -41,6 +42,7 @@
     "pinia": "^3.0.1",
     "pinia-plugin-persistedstate": "^4.4.1",
     "qrcode.vue": "^3.6.0",
+    "semver": "^7.7.2",
     "sharp": "^0.34.2",
     "typescript": "^5.8.3",
     "vant": "^4.9.20",

+ 8 - 4
src/App.vue

@@ -12,15 +12,15 @@
   </div>
 </template>
 
-<script setup> 
-import { appStart } from "@/hooks/updataApp";
+<script setup>  
+import { checkAndUpdate } from "@/updater/index";
+
 import { getNotchHeight } from "@/utils/statusBar";
 const route = useRoute();
 const router = useRouter();
 
 const height = ref(0);
-const notchStyle = ref({});
-// const appMainViewStyle = ref({});
+const notchStyle = ref({}); 
 
 const appMainViewStyle = computed(() => {
   let mainHeight = 0
@@ -54,6 +54,10 @@ onBeforeMount(async () => {
   notchStyle.value = {
     paddingTop: `${height.value}px`,
   };
+
+
+  // 更新
+  checkAndUpdate()
 });
 </script>
 

+ 0 - 37
src/hooks/updataApp.js

@@ -1,37 +0,0 @@
-import { App } from "@capacitor/app";
-import { Capacitor } from "@capacitor/core";
-
- 
-
-// 检测是否有更新包
-export const checkUpdate = async () => {
-  // 判断是否是web端
-  if (Capacitor.getPlatform() === "web") {
-    return;
-  }
-  // 获取当前应用版本
-  const { version } = await App.getInfo();
-  console.log("当前版本:", info);
-
-  // 从服务器获取最新版本信息
-  const response = await fetch('https://your-server.com/api/check-update');
-  const serverData = await response.json();
-
-   // 对比版本号(建议使用语义化版本比较库,如 compare-versions)
-  if (compareVersions(serverData.latestVersion, version) > 0) {
-    console.log('发现新版本:', serverData.latestVersion);
-    return serverData; // 返回更新信息
-  }
-  return null; // 无更新
-};
-
-// 示例:在应用启动时检查
-export const appStart = async () => {
-  // const updateInfo = await checkUpdate();
-  // if (updateInfo) {
-  // showUpdateDialog(updateInfo); // 提示用户更新
-  // }
-
-  const dd = await checkUpdate();
-  console.log("当前版本:", dd);
-};

+ 62 - 0
src/updater/index.js

@@ -0,0 +1,62 @@
+ import { CapacitorUpdater } from "@capgo/capacitor-updater";
+import { App } from "@capacitor/app";
+import semver from "semver";
+import { showDialog } from "vant";
+
+const HOST = "https://nim.angeltokens.io/updates/down/";
+const UPDATE_URL = HOST + "update.json";
+
+// 检查并更新
+export async function checkAndUpdate() {
+  // 判断是web 还是 ios
+  if (Capacitor.getPlatform() === "web") {
+    console.log("web 端不支持更新");
+    return;
+  }
+
+  // 1. 拉取元数据
+  const meta = await fetch(UPDATE_URL).then((r) => r.json());
+  console.log("2. 基本校验", meta);
+
+  //  通知
+  if (meta.mandatory) {
+    showDialog({
+      title: meta.notificationTitle,
+      message: meta.notificationBody,
+    }).then(async () => {});
+  }
+
+  // 2. 基本校验
+  const current = await CapacitorUpdater.current();
+  const { version } = await App.getInfo();
+
+  if (
+    current.bundle.version != "builtin" &&
+    semver.lt(current.bundle.version, meta.version)
+  ) {
+    console.log("无新版本");
+    return;
+  }
+  if (version && semver.lt(version, meta.minBinaryVersion)) {
+    console.log("壳子太旧");
+    return;
+  }
+  // 4. 下载(带进度)
+  const update = await CapacitorUpdater.download({
+    url: `${HOST}v${meta.version}.zip`,
+    version: meta.version,
+    checksum: meta.checksum,
+  });
+
+  // 通知
+  showDialog({
+    title: `v ${meta.version} 已發布`,
+    confirmButtonText: "立即體驗",
+    message: meta.upDataDescription,
+  }).then(async () => {
+    // 重启
+    await CapacitorUpdater.set(update);
+    console.log("✅ 已切换到新版本,准备重启");
+    await App.exitApp(); // 冷启动加载新 bundle
+  });
+}

+ 11 - 0
src/updater/update.json

@@ -0,0 +1,11 @@
+{
+  "version": "1.0.1",
+  "releaseDate": "2025-07-29", 
+  "checksum": "4a232ff37f282fa507bf3e92292577e40097a56501b9a3fbeab1c55aa5221dba",
+  "minBinaryVersion": "1.0.0",
+  "mandatory": true,
+  "upDataDescription":"✨修正一些錯誤!",
+  
+  "notificationTitle": "公告",
+  "notificationBody": "更新了!"
+}