|
|
|
@@ -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,12 @@ 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) {
|
|
|
|
|
return new AppUpdateNotesResponse(null, Collections.emptyList());
|
|
|
|
|
}
|
|
|
|
|
// 当前版本不低于通道版本:已是最新,或回滚后反而超前
|
|
|
|
@@ -169,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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 原生包是否需要更新
|
|
|
|
@@ -230,38 +243,16 @@ 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
|
|
|
|
|
) {
|
|
|
|
|
/// @return 版本最高的已发布热更包,没有则返回 null
|
|
|
|
|
private AppRelease latestPublishedBundle(AppProduct product) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
@@ -300,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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 比较版本号
|
|
|
|
@@ -368,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) {
|
|
|
|
@@ -388,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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -400,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) {
|
|
|
|
@@ -414,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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -422,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) {
|
|
|
|
@@ -431,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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|