From 09a8547dedfa80863e4b668e86e2cd54c66094ea Mon Sep 17 00:00:00 2001 From: Timi Date: Tue, 17 Nov 2026 23:37:31 +0800 Subject: [PATCH 1/2] feat(app): dual pointer for app channel --- .../api/modules/app/entity/AppChannel.java | 10 +- .../modules/app/mapper/AppChannelMapper.java | 7 ++ .../app/service/AppVersionService.java | 119 +++++++----------- .../app/vo/AppUpdateCheckResponse.java | 14 ++- .../V92__split_app_channel_pointers.sql | 9 ++ 5 files changed, 82 insertions(+), 77 deletions(-) create mode 100644 src/main/resources/db/migration/timiserver/V92__split_app_channel_pointers.sql diff --git a/src/main/java/com/imyeyu/api/modules/app/entity/AppChannel.java b/src/main/java/com/imyeyu/api/modules/app/entity/AppChannel.java index 3a18c72..646e39b 100644 --- a/src/main/java/com/imyeyu/api/modules/app/entity/AppChannel.java +++ b/src/main/java/com/imyeyu/api/modules/app/entity/AppChannel.java @@ -7,7 +7,7 @@ import lombok.EqualsAndHashCode; /// /// 应用发布通道 /// -/// 通道只持有一个指针,回滚就是改指向,发布记录本身不可变 +/// 通道分别持有热更包和原生包指针 /// /// @author Codex /// @since 2026-11-06 @@ -24,7 +24,13 @@ public class AppChannel extends UUIDEntity { /// 通道名称 protected String name; - /// 当前指向的发布 ID,为空表示该通道无发布 + /// 当前热更包发布 ID + protected String bundleReleaseId; + + /// 当前原生包发布 ID + protected String nativeReleaseId; + + /// 过渡期旧指针 protected String releaseId; /// 最后操作人用户 ID diff --git a/src/main/java/com/imyeyu/api/modules/app/mapper/AppChannelMapper.java b/src/main/java/com/imyeyu/api/modules/app/mapper/AppChannelMapper.java index 2419baa..5ba092b 100644 --- a/src/main/java/com/imyeyu/api/modules/app/mapper/AppChannelMapper.java +++ b/src/main/java/com/imyeyu/api/modules/app/mapper/AppChannelMapper.java @@ -44,4 +44,11 @@ public interface AppChannelMapper extends BaseMapper { @Param("updatedBy") String updatedBy, @Param("updatedAt") Long updatedAt ); + /// 按类型切换指针并同步旧指针 + @Update("UPDATE app_channel SET bundle_release_id = CASE WHEN #{nativeRelease} = false THEN #{releaseId} ELSE bundle_release_id END, native_release_id = CASE WHEN #{nativeRelease} = true THEN #{releaseId} ELSE native_release_id END, release_id = #{releaseId}, updated_by = #{updatedBy}, updated_at = #{updatedAt} WHERE id = #{id}") + int updatePointers(@Param("id") String id, @Param("releaseId") String releaseId, @Param("nativeRelease") boolean nativeRelease, @Param("updatedBy") String updatedBy, @Param("updatedAt") Long updatedAt); + + /// 清除所有通道中指向撤回记录的指针 + @Update("UPDATE app_channel SET bundle_release_id = CASE WHEN bundle_release_id = #{releaseId} THEN NULL ELSE bundle_release_id END, native_release_id = CASE WHEN native_release_id = #{releaseId} THEN NULL ELSE native_release_id END, release_id = CASE WHEN release_id = #{releaseId} THEN NULL ELSE release_id END, updated_by = #{updatedBy}, updated_at = #{updatedAt} WHERE product_id = #{productId} AND deleted_at IS NULL AND (bundle_release_id = #{releaseId} OR native_release_id = #{releaseId} OR release_id = #{releaseId})") + int clearRelease(@Param("productId") String productId, @Param("releaseId") String releaseId, @Param("updatedBy") String updatedBy, @Param("updatedAt") Long updatedAt); } diff --git a/src/main/java/com/imyeyu/api/modules/app/service/AppVersionService.java b/src/main/java/com/imyeyu/api/modules/app/service/AppVersionService.java index 008c131..8bb7bc2 100644 --- a/src/main/java/com/imyeyu/api/modules/app/service/AppVersionService.java +++ b/src/main/java/com/imyeyu/api/modules/app/service/AppVersionService.java @@ -16,6 +16,7 @@ import com.imyeyu.java.TimiJava; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import java.nio.charset.StandardCharsets; @@ -48,6 +49,12 @@ public class AppVersionService { /// 通道指向原生包时,低于这个版本的设备改为下发热更包,先把它升到能自升级为止 public static final String SHELL_AWARE_VERSION = "1.3.2"; + /// 首个按独立字段接收外壳更新的 Web 包版本 + /// + /// 从这版起响应把安装包放到 shellUrl,主字段 url 永远给热更包; + /// 更早的包只认「主字段 + shell 标记」,所以两套语义要并存到 1.5.0 铺开为止 + public static final String SHELL_JSON_AWARE_VERSION = "1.5.0"; + /// 更新日志最多返回的版本条数 public static final int MAX_NOTES = 20; @@ -72,28 +79,26 @@ public class AppVersionService { if (product == null) { return AppUpdateCheckResponse.none(currentVersion); } - AppRelease release = resolveChannelRelease(product, req); - if (release == null) { - return AppUpdateCheckResponse.none(currentVersion); - } int currentCode = parseInt(req.getVersionCode(), 0); - if (release.getKind() == AppRelease.Kind.NATIVE) { - // 低于外壳升级能力的包拿不动安装包,先补热更,否则它会把 APK 当 zip 解 - boolean shellAware = 0 <= compareVersion(currentVersion, SHELL_AWARE_VERSION); - AppRelease nativeTarget = shellAware ? resolveNative(release, currentCode) : null; - if (nativeTarget != null) { - return buildResponse(nativeTarget, Boolean.TRUE.equals(nativeTarget.getMandatory()), true); - } - // 装不了安装包,或壳已经是最新:热更不能因为通道被原生包占着就断流 - AppRelease bundle = resolveLatestBundle(product, currentCode, currentVersion, req.getDeviceId()); - return bundle == null - ? AppUpdateCheckResponse.none(currentVersion) - : buildResponse(bundle, false, false); + AppRelease bundlePointer = resolvePointer(product, req, AppRelease.Kind.BUNDLE); + AppRelease nativePointer = resolvePointer(product, req, AppRelease.Kind.NATIVE); + AppRelease bundle = bundlePointer == null ? null : resolveBundle(bundlePointer, product, currentCode, currentVersion, req.getDeviceId()); + AppRelease nativeTarget = nativePointer == null ? null : resolveNative(nativePointer, currentCode); + boolean shellAware = 0 <= compareVersion(currentVersion, SHELL_AWARE_VERSION); + boolean jsonAware = 0 <= compareVersion(currentVersion, SHELL_JSON_AWARE_VERSION); + // 认识 shell 标记但还不认识独立字段的包,主字段继续给安装包 + if (nativeTarget != null && shellAware && !jsonAware) { + return buildResponse(nativeTarget, Boolean.TRUE.equals(nativeTarget.getMandatory()), true); } - AppRelease target = resolveBundle(release, product, currentCode, currentVersion, req.getDeviceId()); - return target == null - ? AppUpdateCheckResponse.none(currentVersion) - : buildResponse(target, false, false); + AppUpdateCheckResponse response = bundle == null ? AppUpdateCheckResponse.none(currentVersion) : buildResponse(bundle, false, false); + if (nativeTarget != null && jsonAware) { + AppUpdateCheckResponse shell = buildResponse(nativeTarget, false, true); + response.setShellUrl(shell.getUrl()); + response.setShellVersion(shell.getVersion()); + response.setShellChecksum(shell.getChecksum()); + response.setShellMandatory(Boolean.TRUE.equals(nativeTarget.getMandatory())); + } + return response; } /// 查询更新日志 @@ -113,9 +118,9 @@ public class AppVersionService { if (product == null) { return new AppUpdateNotesResponse(null, Collections.emptyList()); } - AppRelease target = resolveChannelRelease(product, req); - // 安装包不属于更新日志:通道指向原生包时,改用最新发布的热更包做上界 - if (target == null || target.getKind() == AppRelease.Kind.NATIVE) { + AppRelease target = resolvePointer(product, req, AppRelease.Kind.BUNDLE); + // 指针为空(刚建通道还没发过热更)时退回最近一次发布,日志不该跟着空 + if (target == null) { target = latestPublishedBundle(product); } if (target == null) { @@ -172,17 +177,22 @@ public class AppVersionService { /// @param product 产品 /// @param req 插件上报信息 /// @return 发布记录,通道为空或指向的记录不可用时为 null - private AppRelease resolveChannelRelease(AppProduct product, AppUpdateCheckRequest req) { + private AppRelease resolvePointer(AppProduct product, AppUpdateCheckRequest req, AppRelease.Kind kind) { String channelCode = TimiJava.isEmpty(req.getChannel()) ? DEFAULT_CHANNEL : req.getChannel(); AppChannel channel = channelMapper.selectByProductAndCode(product.getId(), channelCode); - if (channel == null || TimiJava.isEmpty(channel.getReleaseId())) { + if (channel == null) { return null; } - AppRelease release = releaseMapper.select(channel.getReleaseId()); - if (release == null || release.getStatus() != AppRelease.Status.PUBLISHED) { + String id = kind == AppRelease.Kind.BUNDLE ? channel.getBundleReleaseId() : channel.getNativeReleaseId(); + // 兼容旧代码在回滚期间仅更新旧指针的情况 + if (TimiJava.isEmpty(id)) { + id = channel.getReleaseId(); + } + if (TimiJava.isEmpty(id)) { return null; } - return release; + AppRelease release = releaseMapper.select(id); + return release != null && release.getKind() == kind && release.getStatus() == AppRelease.Status.PUBLISHED ? release : null; } /// 原生包是否需要更新 @@ -233,45 +243,6 @@ public class AppVersionService { return release; } - /// 查询设备此刻可用的最新热更包 - /// - /// 通道指向原生包时用它兜底:通道是单指针,原生包一上线热更就停发, - /// 而低于 SHELL_AWARE_VERSION 的设备又拿不动安装包,只能靠这里把它先升上去。 - /// 只认 PUBLISHED 且版本高于当前包的记录,不会降级,最坏情况是多升一次。 - /// - /// @param product 产品 - /// @param currentCode 当前原生 versionCode - /// @param currentVersion 当前 Web 包版本 - /// @param deviceId 设备标识 - /// @return 可用热更包,没有则返回 null - private AppRelease resolveLatestBundle( - AppProduct product, - int currentCode, - String currentVersion, - String deviceId - ) { - AppRelease latest = null; - for (AppRelease item : releaseMapper.selectPublished(product.getId(), AppRelease.Kind.BUNDLE)) { - if (item.getMinVersionCode() != null && currentCode < item.getMinVersionCode()) { - continue; - } - if (item.getMaxVersionCode() != null && item.getMaxVersionCode() < currentCode) { - continue; - } - // 同级或更低不下发,避免把设备降回去 - if (0 >= compareVersion(item.getVersionName(), currentVersion)) { - continue; - } - if (!hitGray(item.getGrayPermille(), deviceId, product.getAppKey())) { - continue; - } - if (latest == null || 0 < compareVersion(item.getVersionName(), latest.getVersionName())) { - latest = item; - } - } - return latest; - } - /// 取最近一次发布的热更包 /// /// 通道指向原生包时,更新日志没有「通道版本」可用,改用这里兜底。 @@ -320,7 +291,7 @@ public class AppVersionService { .path("/attach/download/") .path(release.getAttachmentId()) .toUriString(); - return new AppUpdateCheckResponse(release.getVersionName(), url, release.getSha256(), mandatory, shell, release.getNote()); + return new AppUpdateCheckResponse(release.getVersionName(), url, release.getSha256(), mandatory, shell, release.getNote(), null, null, null, null); } /// 比较版本号 @@ -388,6 +359,7 @@ public class AppVersionService { /// @param grayPermille 灰度比例,为空时保持原值 /// @param operatorId 操作人用户 ID /// @param note 变更说明 + @Transactional public void publish(String releaseId, String channelCode, Integer grayPermille, String operatorId, String note) { AppRelease release = releaseMapper.select(releaseId); if (release == null) { @@ -408,7 +380,7 @@ public class AppVersionService { release.setNote(TimiJava.isEmpty(note) ? release.getNote() : note); release.setUpdatedAt(now); releaseMapper.updateSelective(release); - channelMapper.updateReleaseId(channel.getId(), releaseId, operatorId, now); + channelMapper.updatePointers(channel.getId(), releaseId, release.getKind() == AppRelease.Kind.NATIVE, operatorId, now); log("APP_RELEASE_PUBLISH", releaseId); } @@ -420,6 +392,7 @@ public class AppVersionService { /// @param channelCode 通道代码 /// @param targetReleaseId 目标发布 ID /// @param operatorId 操作人用户 ID + @Transactional public void rollback(String productId, String channelCode, String targetReleaseId, String operatorId) { AppChannel channel = channelMapper.selectByProductAndCode(productId, channelCode); if (channel == null) { @@ -434,7 +407,7 @@ public class AppVersionService { target.setStatus(AppRelease.Status.PUBLISHED); target.setUpdatedAt(now); releaseMapper.updateSelective(target); - channelMapper.updateReleaseId(channel.getId(), targetReleaseId, operatorId, now); + channelMapper.updatePointers(channel.getId(), targetReleaseId, target.getKind() == AppRelease.Kind.NATIVE, operatorId, now); log("APP_RELEASE_ROLLBACK", targetReleaseId); } @@ -442,6 +415,7 @@ public class AppVersionService { /// /// @param releaseId 发布 ID /// @param operatorId 操作人用户 ID + @Transactional public void revoke(String releaseId, String operatorId) { AppRelease release = releaseMapper.select(releaseId); if (release == null) { @@ -451,10 +425,7 @@ public class AppVersionService { release.setStatus(AppRelease.Status.REVOKED); release.setUpdatedAt(now); releaseMapper.updateSelective(release); - AppChannel channel = channelMapper.selectByProductAndCode(release.getProductId(), DEFAULT_CHANNEL); - if (channel != null && releaseId.equals(channel.getReleaseId())) { - channelMapper.updateReleaseId(channel.getId(), null, operatorId, now); - } + channelMapper.clearRelease(release.getProductId(), releaseId, operatorId, now); log("APP_RELEASE_REVOKE", releaseId); } diff --git a/src/main/java/com/imyeyu/api/modules/app/vo/AppUpdateCheckResponse.java b/src/main/java/com/imyeyu/api/modules/app/vo/AppUpdateCheckResponse.java index a2d0d44..cb3a83c 100644 --- a/src/main/java/com/imyeyu/api/modules/app/vo/AppUpdateCheckResponse.java +++ b/src/main/java/com/imyeyu/api/modules/app/vo/AppUpdateCheckResponse.java @@ -39,11 +39,23 @@ public class AppUpdateCheckResponse { /// 字段名不能叫 message:Capgo 插件把 message 当作错误信息,收到会直接判为检查失败 protected String note; + /// 外壳下载地址 + protected String shellUrl; + + /// 外壳版本 + protected String shellVersion; + + /// 外壳 SHA-256 + protected String shellChecksum; + + /// true 为强制更新 + protected Boolean shellMandatory; + /// 构建无更新响应,version 与当前一致时插件不会下载 /// /// @param currentVersion 当前运行的 Web 包版本 /// @return 无更新响应 public static AppUpdateCheckResponse none(String currentVersion) { - return new AppUpdateCheckResponse(currentVersion, "", null, false, null, null); + return new AppUpdateCheckResponse(currentVersion, "", null, false, null, null, null, null, null, null); } } diff --git a/src/main/resources/db/migration/timiserver/V92__split_app_channel_pointers.sql b/src/main/resources/db/migration/timiserver/V92__split_app_channel_pointers.sql new file mode 100644 index 0000000..6144e88 --- /dev/null +++ b/src/main/resources/db/migration/timiserver/V92__split_app_channel_pointers.sql @@ -0,0 +1,9 @@ +ALTER TABLE `app_channel` + ADD COLUMN `bundle_release_id` VARCHAR(36) NULL COMMENT '当前热更包发布 ID' AFTER `name`, + ADD COLUMN `native_release_id` VARCHAR(36) NULL COMMENT '当前原生包发布 ID' AFTER `bundle_release_id`; + +UPDATE `app_channel` c JOIN `app_release` r ON r.`id` = c.`release_id` +SET c.`native_release_id` = IF(r.`kind` = 'NATIVE', c.`release_id`, NULL), + c.`bundle_release_id` = IF(r.`kind` = 'BUNDLE', c.`release_id`, + (SELECT b.`id` FROM `app_release` b WHERE b.`product_id` = c.`product_id` AND b.`kind` = 'BUNDLE' + AND b.`status` = 'PUBLISHED' AND b.`deleted_at` IS NULL ORDER BY b.`published_at` DESC LIMIT 1)); From 8954902994f24bdbca01a77a7e317a66749bb4c2 Mon Sep 17 00:00:00 2001 From: Timi Date: Tue, 17 Nov 2026 23:37:38 +0800 Subject: [PATCH 2/2] v1.0.77 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f0a8ca9..d6f04d2 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ com.imyeyu.timiserverapi TimiServerAPI - 1.0.76 + 1.0.77 jar TimiServerAPI imyeyu.com API