// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目必须遵守相关法律法规和许可证的要求。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using System.Globalization;
namespace Admin.NET.Core.Service;
///
/// APP 版本检查服务 🧩
///
[ApiDescriptionSettings(Order = 505)]
public class SysAppVersionService : IDynamicApiController, ITransient
{
private readonly SysConfigService _sysConfigService;
private readonly SysAppReleaseService _releaseService;
private readonly UserManager _userManager;
public SysAppVersionService(
SysConfigService sysConfigService,
SysAppReleaseService releaseService,
UserManager userManager)
{
_sysConfigService = sysConfigService;
_releaseService = releaseService;
_userManager = userManager;
}
///
/// 获取 APP 最新版本 🔖
///
/// 平台:android / ios
/// 匿名请求只返回版本元数据;登录请求在存在新发布配置时返回一次性短时下载地址。旧 app_android_apk_url 仅对登录请求兼容返回。
[AllowAnonymous]
[DisplayName("获取APP最新版本")]
public async Task GetLatest([FromQuery] string platform = "android")
{
platform = string.IsNullOrWhiteSpace(platform) ? "android" : platform.Trim().ToLowerInvariant();
if (platform is not ("android" or "ios")) throw Oops.Oh("不支持的 APP 平台");
var release = await AppReleaseSnapshot.Load(_sysConfigService, platform);
var output = new AppVersionOutput
{
Platform = platform,
PackageName = platform == "android" ? AppReleaseConfig.AndroidPackageName : null,
VersionCode = release.VersionCode,
VersionName = release.VersionName,
ForceUpdate = release.ForceUpdate,
ReleaseNotes = release.ReleaseNotes,
PublishedAt = release.PublishedAt,
SizeBytes = release.SizeBytes,
Sha256 = release.Sha256,
};
if (_userManager.UserId <= 0) return output;
if (release.IsComplete)
{
var ticket = await _releaseService.CreateTicketForCurrentUser(release);
output.ApkUrl = ticket.DownloadUrl;
output.DownloadExpiresAt = ticket.ExpiresAt;
}
else
{
// 兼容历史客户端:仅登录用户可获得旧永久地址。该地址无法撤销、无法防重放,
// 管理员完成一次新方式发布后应清空旧配置。
output.ApkUrl = await _sysConfigService.GetConfigValue($"app_{platform}_apk_url");
output.UsesLegacyPermanentUrl = !string.IsNullOrWhiteSpace(output.ApkUrl);
}
return output;
}
}
///
/// APP 版本信息输出
///
public class AppVersionOutput
{
///
/// 平台(android / ios)
///
public string Platform { get; set; }
///
/// Android applicationId。
///
public string? PackageName { get; set; }
///
/// 版本号(递增整数,对应 manifest.json versionCode)
///
public int VersionCode { get; set; }
///
/// 版本名(对应 manifest.json versionName)
///
public string VersionName { get; set; }
///
/// 安装包下载地址
///
public string ApkUrl { get; set; }
///
/// 短时下载地址过期时间。
///
public DateTimeOffset? DownloadExpiresAt { get; set; }
///
/// 是否使用旧永久地址兼容模式。
///
public bool UsesLegacyPermanentUrl { get; set; }
///
/// 是否强制更新
///
public bool ForceUpdate { get; set; }
///
/// 更新说明
///
public string ReleaseNotes { get; set; }
public DateTimeOffset? PublishedAt { get; set; }
public long SizeBytes { get; set; }
public string Sha256 { get; set; }
}
public static class AppReleaseConfig
{
public const string AndroidPackageName = "com.mdiop.app";
public const string GroupCode = "app_release";
public const int DefaultTicketTtlMinutes = 15;
public const int DefaultSignedUrlTtlMinutes = 5;
}
public sealed class AppReleaseSnapshot
{
public string Platform { get; init; } = "android";
public long FileId { get; init; }
public int VersionCode { get; init; }
public string VersionName { get; init; }
public string ReleaseNotes { get; init; }
public bool ForceUpdate { get; init; }
public DateTimeOffset? PublishedAt { get; init; }
public long SizeBytes { get; init; }
public string Sha256 { get; init; }
public bool IsComplete =>
FileId > 0 &&
VersionCode > 0 &&
!string.IsNullOrWhiteSpace(VersionName) &&
PublishedAt.HasValue &&
SizeBytes > 0 &&
Sha256?.Length == 64;
public static async Task Load(SysConfigService config, string platform = "android")
{
var prefix = $"app_{platform}_";
var publishedAtRaw = await config.GetConfigValue(prefix + "published_at");
_ = DateTimeOffset.TryParse(
publishedAtRaw,
CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind,
out var publishedAt);
return new AppReleaseSnapshot
{
Platform = platform,
FileId = await config.GetConfigValue(prefix + "file_id"),
VersionCode = await config.GetConfigValue(prefix + "version_code"),
VersionName = await config.GetConfigValue(prefix + "version_name"),
ReleaseNotes = await config.GetConfigValue(prefix + "release_notes"),
ForceUpdate = await config.GetConfigValue(prefix + "force_update"),
PublishedAt = publishedAt == default ? null : publishedAt,
SizeBytes = await config.GetConfigValue(prefix + "size_bytes"),
Sha256 = await config.GetConfigValue(prefix + "sha256"),
};
}
}