| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目必须遵守相关法律法规和许可证的要求。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
- //
- // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
- using System.Globalization;
- namespace Admin.NET.Core.Service;
- /// <summary>
- /// APP 版本检查服务 🧩
- /// </summary>
- [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;
- }
- /// <summary>
- /// 获取 APP 最新版本 🔖
- /// </summary>
- /// <param name="platform">平台:android / ios</param>
- /// <remarks>匿名请求只返回版本元数据;登录请求在存在新发布配置时返回一次性短时下载地址。旧 app_android_apk_url 仅对登录请求兼容返回。</remarks>
- [AllowAnonymous]
- [DisplayName("获取APP最新版本")]
- public async Task<AppVersionOutput> 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<string>($"app_{platform}_apk_url");
- output.UsesLegacyPermanentUrl = !string.IsNullOrWhiteSpace(output.ApkUrl);
- }
- return output;
- }
- }
- /// <summary>
- /// APP 版本信息输出
- /// </summary>
- public class AppVersionOutput
- {
- /// <summary>
- /// 平台(android / ios)
- /// </summary>
- public string Platform { get; set; }
- /// <summary>
- /// Android applicationId。
- /// </summary>
- public string? PackageName { get; set; }
- /// <summary>
- /// 版本号(递增整数,对应 manifest.json versionCode)
- /// </summary>
- public int VersionCode { get; set; }
- /// <summary>
- /// 版本名(对应 manifest.json versionName)
- /// </summary>
- public string VersionName { get; set; }
- /// <summary>
- /// 安装包下载地址
- /// </summary>
- public string ApkUrl { get; set; }
- /// <summary>
- /// 短时下载地址过期时间。
- /// </summary>
- public DateTimeOffset? DownloadExpiresAt { get; set; }
- /// <summary>
- /// 是否使用旧永久地址兼容模式。
- /// </summary>
- public bool UsesLegacyPermanentUrl { get; set; }
- /// <summary>
- /// 是否强制更新
- /// </summary>
- public bool ForceUpdate { get; set; }
- /// <summary>
- /// 更新说明
- /// </summary>
- 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<AppReleaseSnapshot> Load(SysConfigService config, string platform = "android")
- {
- var prefix = $"app_{platform}_";
- var publishedAtRaw = await config.GetConfigValue<string>(prefix + "published_at");
- _ = DateTimeOffset.TryParse(
- publishedAtRaw,
- CultureInfo.InvariantCulture,
- DateTimeStyles.RoundtripKind,
- out var publishedAt);
- return new AppReleaseSnapshot
- {
- Platform = platform,
- FileId = await config.GetConfigValue<long>(prefix + "file_id"),
- VersionCode = await config.GetConfigValue<int>(prefix + "version_code"),
- VersionName = await config.GetConfigValue<string>(prefix + "version_name"),
- ReleaseNotes = await config.GetConfigValue<string>(prefix + "release_notes"),
- ForceUpdate = await config.GetConfigValue<bool>(prefix + "force_update"),
- PublishedAt = publishedAt == default ? null : publishedAt,
- SizeBytes = await config.GetConfigValue<long>(prefix + "size_bytes"),
- Sha256 = await config.GetConfigValue<string>(prefix + "sha256"),
- };
- }
- }
|