From a3bae175812a9606de70d6277c840b164e793ee7 Mon Sep 17 00:00:00 2001 From: Timi Date: Sat, 7 Nov 2026 16:24:29 +0800 Subject: [PATCH 1/3] feat: split update into check, download and apply steps --- src/composables/useAppUpdate.ts | 50 ++++++++++++------- src/pages/setting/AppSettings.vue | 80 ++++++++++++++++++++----------- 2 files changed, 87 insertions(+), 43 deletions(-) diff --git a/src/composables/useAppUpdate.ts b/src/composables/useAppUpdate.ts index 7564f95..cc8106f 100644 --- a/src/composables/useAppUpdate.ts +++ b/src/composables/useAppUpdate.ts @@ -64,7 +64,7 @@ export function useAppUpdate() { return Boolean(target?.url) && target?.version !== currentVersion.value; } - /** 下载最新版本并排队等待生效 */ + /** 下载最新版本,只落盘不激活,等用户确认后再应用 */ async function download(): Promise { const target = latest.value; if (!target?.url) { @@ -84,7 +84,6 @@ export function useAppUpdate() { checksum: target.checksum, sessionKey: target.sessionKey }); - await CapacitorUpdater.next({ id: bundle.id }); ready.value = bundle; return bundle; } catch (e) { @@ -96,19 +95,28 @@ export function useAppUpdate() { } } - /** 立即重启应用使更新生效 */ - async function reload(): Promise { - await CapacitorUpdater.reload(); + /** 应用已下载的版本并重启,只有用户明确点了才执行 */ + async function apply(bundle: BundleInfo): Promise { + await CapacitorUpdater.set({ id: bundle.id }); } - /** 检查并下载,供启动时自动更新调用 */ - async function checkAndDownload(): Promise { - await loadCurrent(); - const result = await check(); - if (!result?.url || result.version === currentVersion.value) { - return false; + /** 查找已下载但尚未应用的版本,用户不点就一直停在这里 */ + async function restorePending(): Promise { + if (!supported) { + return; + } + try { + const [current, list] = await Promise.all([CapacitorUpdater.current(), CapacitorUpdater.list()]); + const currentId = current.bundle?.id; + const pending = list.bundles + .filter(bundle => bundle.id !== currentId) + .sort((a, b) => Date.parse(b.downloaded) - Date.parse(a.downloaded))[0]; + if (pending) { + ready.value = pending; + } + } catch { + ready.value = null; } - return (await download()) !== null; } return { @@ -125,8 +133,8 @@ export function useAppUpdate() { check, hasUpdate, download, - reload, - checkAndDownload + apply, + restorePending }; } @@ -138,11 +146,21 @@ export function isAutoUpdateEnabled(): boolean { /** * 启动时静默检查更新 * - * 只下载排队,不重启,下次打开 App 自动生效 + * 只下载不应用,是否切换版本始终由用户决定 */ export async function runAutoUpdate(): Promise { if (!Capacitor.isNativePlatform() || !isAutoUpdateEnabled()) { return; } - await useAppUpdate().checkAndDownload(); + const update = useAppUpdate(); + await update.loadCurrent(); + await update.restorePending(); + if (update.ready.value) { + return; + } + const result = await update.check(); + if (!result?.url || result.version === update.currentVersion.value) { + return; + } + await update.download(); } diff --git a/src/pages/setting/AppSettings.vue b/src/pages/setting/AppSettings.vue index 5fc722c..5219dda 100644 --- a/src/pages/setting/AppSettings.vue +++ b/src/pages/setting/AppSettings.vue @@ -56,20 +56,14 @@
- @@ -155,25 +149,51 @@ const { check, hasUpdate, download, - reload + apply, + restorePending } = useAppUpdate(); const autoUpdate = ref(isAutoUpdateEnabled()); -const canCheckUpdate = computed(() => updateSupported && !checking.value && !downloading.value); +/** 当前可执行的动作,按检查、下载、应用三阶段推进 */ +const actionTitle = computed(() => { + if (!updateSupported) { + return "检查更新"; + } + if (checking.value) { + return "正在检查"; + } + if (downloading.value) { + return "正在下载"; + } + if (ready.value) { + return "现在更新"; + } + if (latest.value && hasUpdate()) { + return "开始下载"; + } + return "检查更新"; +}); + +const actionEnabled = computed(() => { + if (!updateSupported || checking.value || downloading.value) { + return false; + } + return true; +}); const updateNote = computed(() => { if (!updateSupported) { return "仅在 App 内可用"; } if (downloading.value) { - return `下载中 ${percent.value}%`; + return `${percent.value}%`; } if (ready.value) { - return "已下载,下次启动生效"; + return `v${ready.value.version} 已下载`; } if (checking.value) { - return "检查中"; + return "请稍候"; } if (latest.value) { return hasUpdate() ? `发现 v${latest.value.version}` : "已是最新"; @@ -183,6 +203,7 @@ const updateNote = computed(() => { onMounted(async () => { await loadCurrent(); + await restorePending(); }); function handleAutoUpdateChange(value: boolean | string | number): void { @@ -191,10 +212,26 @@ function handleAutoUpdateChange(value: boolean | string | number): void { localStorage.setItem(AUTO_UPDATE_KEY, enabled ? "1" : "0"); } -async function handleCheckUpdate(): Promise { - if (!canCheckUpdate.value) { +async function handleAction(): Promise { + if (!actionEnabled.value) { return; } + // 已下载,应用并重启 + if (ready.value) { + await apply(ready.value); + return; + } + // 已发现新版本,开始下载 + if (latest.value && hasUpdate()) { + const bundle = await download(); + if (bundle) { + Message.success(`v${bundle.version} 已下载,点「现在更新」生效`); + return; + } + Message.error(updateError.value || "下载失败"); + return; + } + // 检查更新 const result = await check(); if (!result) { Message.error(updateError.value || "检查更新失败"); @@ -202,18 +239,7 @@ async function handleCheckUpdate(): Promise { } if (!hasUpdate()) { Message.info("已是最新版本"); - return; } - const bundle = await download(); - if (bundle) { - Message.success(`v${bundle.version} 已下载,下次启动生效`); - return; - } - Message.error(updateError.value || "下载失败"); -} - -async function handleReload(): Promise { - await reload(); } From 163ef12265dd386fe66658b4f43c91818f675bd5 Mon Sep 17 00:00:00 2001 From: Timi Date: Sat, 7 Nov 2026 16:49:35 +0800 Subject: [PATCH 2/3] feat: show update entry only in native app --- src/pages/setting/AppSettings.vue | 140 +++++++++++++++++------------- 1 file changed, 80 insertions(+), 60 deletions(-) diff --git a/src/pages/setting/AppSettings.vue b/src/pages/setting/AppSettings.vue index 5219dda..fcc41e1 100644 --- a/src/pages/setting/AppSettings.vue +++ b/src/pages/setting/AppSettings.vue @@ -47,7 +47,7 @@ - + @@ -55,16 +55,43 @@ - -
- -
+
+ + + 检查更新 + + + 下载更新 + + + + 现在更新 + +
@@ -155,48 +182,33 @@ const { const autoUpdate = ref(isAutoUpdateEnabled()); -/** 当前可执行的动作,按检查、下载、应用三阶段推进 */ -const actionTitle = computed(() => { - if (!updateSupported) { - return "检查更新"; - } - if (checking.value) { - return "正在检查"; - } +/** 当前所处的更新阶段 */ +const updateStage = computed<"check" | "download" | "downloading" | "apply">(() => { if (downloading.value) { - return "正在下载"; + return "downloading"; } if (ready.value) { - return "现在更新"; + return "apply"; } if (latest.value && hasUpdate()) { - return "开始下载"; + return "download"; } - return "检查更新"; + return "check"; }); -const actionEnabled = computed(() => { - if (!updateSupported || checking.value || downloading.value) { - return false; - } - return true; -}); - -const updateNote = computed(() => { +/** 阶段说明文案 */ +const updateHint = computed(() => { if (!updateSupported) { return "仅在 App 内可用"; } - if (downloading.value) { - return `${percent.value}%`; - } if (ready.value) { - return `v${ready.value.version} 已下载`; + return `v${ready.value.version} 已下载,点击下方按钮重启生效`; } - if (checking.value) { - return "请稍候"; + if (latest.value && hasUpdate()) { + return `发现新版本 v${latest.value.version}`; } - if (latest.value) { - return hasUpdate() ? `发现 v${latest.value.version}` : "已是最新"; + if (latest.value && !checking.value) { + return "当前已是最新版本"; } return ""; }); @@ -212,26 +224,7 @@ function handleAutoUpdateChange(value: boolean | string | number): void { localStorage.setItem(AUTO_UPDATE_KEY, enabled ? "1" : "0"); } -async function handleAction(): Promise { - if (!actionEnabled.value) { - return; - } - // 已下载,应用并重启 - if (ready.value) { - await apply(ready.value); - return; - } - // 已发现新版本,开始下载 - if (latest.value && hasUpdate()) { - const bundle = await download(); - if (bundle) { - Message.success(`v${bundle.version} 已下载,点「现在更新」生效`); - return; - } - Message.error(updateError.value || "下载失败"); - return; - } - // 检查更新 +async function handleCheck(): Promise { const result = await check(); if (!result) { Message.error(updateError.value || "检查更新失败"); @@ -241,6 +234,23 @@ async function handleAction(): Promise { Message.info("已是最新版本"); } } + +async function handleDownload(): Promise { + const bundle = await download(); + if (!bundle) { + Message.error(updateError.value || "下载失败"); + return; + } + Message.success(`v${bundle.version} 已下载`); +} + +async function handleApply(): Promise { + const bundle = ready.value; + if (!bundle) { + return; + } + await apply(bundle); +} From 6181e70b935a31dc260eb12dc6076e857e5698a7 Mon Sep 17 00:00:00 2001 From: Timi Date: Sat, 7 Nov 2026 16:50:22 +0800 Subject: [PATCH 3/3] ci: deploy web build to nginx on merge --- .gitea/workflows/deploy.yml | 97 +++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 .gitea/workflows/deploy.yml diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..1d165e5 --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,97 @@ +name: Deploy + +# 网页部署:构建后同步到 nginx 静态目录 +# 热更新包由 release.yml 单独处理,两者互不影响 +# 标题带 [skip deploy] 可跳过网页更新 +on: + pull_request: + branches: + - master + types: + - closed + workflow_dispatch: + +jobs: + deploy: + runs-on: act_runner_node + if: ${{ github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Check skip flag + id: guard + run: | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then + echo "skip=false" >> $GITHUB_OUTPUT + exit 0 + fi + title="${{ github.event.pull_request.title }}" + if echo "$title" | grep -q "\[skip deploy\]"; then + echo "标题含 [skip deploy],跳过网页部署" + echo "skip=true" >> $GITHUB_OUTPUT + else + echo "skip=false" >> $GITHUB_OUTPUT + fi + + - name: Configure nexus auth + if: steps.guard.outputs.skip != 'true' + env: + NEXUS_USERNAME: ${{ secrets.NEXUS_USERNAME }} + NEXUS_PASSWORD: ${{ secrets.NEXUS_PASSWORD }} + run: | + if [ -z "$NEXUS_USERNAME" ] || [ -z "$NEXUS_PASSWORD" ]; then + echo "FAIL: 缺少 secrets.NEXUS_USERNAME 或 secrets.NEXUS_PASSWORD" + exit 1 + fi + auth=$(printf '%s:%s' "$NEXUS_USERNAME" "$NEXUS_PASSWORD" | base64 | tr -d '\n') + { + echo "registry=https://nexus.imyeyu.com/repository/npm-group/" + echo "//nexus.imyeyu.com/repository/npm-group/:_auth=${auth}" + echo "//nexus.imyeyu.com/repository/npm-group/:always-auth=true" + echo "//nexus.imyeyu.com/repository/npm-timi/:_auth=${auth}" + echo "//nexus.imyeyu.com/repository/npm-timi/:always-auth=true" + } > ~/.npmrc + echo "OK: nexus 认证已配置" + + - name: Install dependencies + if: steps.guard.outputs.skip != 'true' + run: pnpm install --frozen-lockfile + + - name: Build + if: steps.guard.outputs.skip != 'true' + run: pnpm run build + + - name: Deploy to nginx + if: steps.guard.outputs.skip != 'true' + run: | + target=/mnt/nginx-html/gao + if [ ! -d "$target" ]; then + echo "FAIL: 目标目录 $target 不存在" + exit 1 + fi + if [ ! -f dist/index.html ]; then + echo "FAIL: dist/index.html 缺失" + exit 1 + fi + # 不能删除旧文件:nginx 对 /assets/ 设了一年 immutable 缓存,删掉会让在用的客户端白屏 + # 同名文件覆盖即可,历史 chunk 保留 + cp -a dist/. "$target/" + echo "部署完成" + echo "文件总数:$(find "$target" -type f | wc -l)" + echo "index.html:$(stat -c '%y %s 字节' "$target/index.html")" + echo "assets 数量:$(find "$target/assets" -type f 2>/dev/null | wc -l)" + + - name: Verify + if: steps.guard.outputs.skip != 'true' + run: | + echo "--- 产物一致性检查 ---" + local_index=$(node -p "require('fs').readFileSync('dist/index.html','utf8').match(/index-[A-Za-z0-9_-]+\\.js/)?.[0] || ''") + remote_index=$(grep -oE "index-[A-Za-z0-9_-]+\.js" /mnt/nginx-html/gao/index.html | head -1) + echo "本地入口:$local_index" + echo "线上入口:$remote_index" + if [ "$local_index" != "$remote_index" ]; then + echo "FAIL: 线上 index.html 与本次产物不一致" + exit 1 + fi + echo "OK: 线上已是最新构建"