SysAppVersionService.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目必须遵守相关法律法规和许可证的要求。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  2. //
  3. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  4. using System.Globalization;
  5. namespace Admin.NET.Core.Service;
  6. /// <summary>
  7. /// APP 版本检查服务 🧩
  8. /// </summary>
  9. [ApiDescriptionSettings(Order = 505)]
  10. public class SysAppVersionService : IDynamicApiController, ITransient
  11. {
  12. private readonly SysConfigService _sysConfigService;
  13. private readonly SysAppReleaseService _releaseService;
  14. private readonly UserManager _userManager;
  15. public SysAppVersionService(
  16. SysConfigService sysConfigService,
  17. SysAppReleaseService releaseService,
  18. UserManager userManager)
  19. {
  20. _sysConfigService = sysConfigService;
  21. _releaseService = releaseService;
  22. _userManager = userManager;
  23. }
  24. /// <summary>
  25. /// 获取 APP 最新版本 🔖
  26. /// </summary>
  27. /// <param name="platform">平台:android / ios</param>
  28. /// <remarks>匿名请求只返回版本元数据;登录请求在存在新发布配置时返回一次性短时下载地址。旧 app_android_apk_url 仅对登录请求兼容返回。</remarks>
  29. [AllowAnonymous]
  30. [DisplayName("获取APP最新版本")]
  31. public async Task<AppVersionOutput> GetLatest([FromQuery] string platform = "android")
  32. {
  33. platform = string.IsNullOrWhiteSpace(platform) ? "android" : platform.Trim().ToLowerInvariant();
  34. if (platform is not ("android" or "ios")) throw Oops.Oh("不支持的 APP 平台");
  35. var release = await AppReleaseSnapshot.Load(_sysConfigService, platform);
  36. var output = new AppVersionOutput
  37. {
  38. Platform = platform,
  39. PackageName = platform == "android" ? AppReleaseConfig.AndroidPackageName : null,
  40. VersionCode = release.VersionCode,
  41. VersionName = release.VersionName,
  42. ForceUpdate = release.ForceUpdate,
  43. ReleaseNotes = release.ReleaseNotes,
  44. PublishedAt = release.PublishedAt,
  45. SizeBytes = release.SizeBytes,
  46. Sha256 = release.Sha256,
  47. };
  48. if (_userManager.UserId <= 0) return output;
  49. if (release.IsComplete)
  50. {
  51. var ticket = await _releaseService.CreateTicketForCurrentUser(release);
  52. output.ApkUrl = ticket.DownloadUrl;
  53. output.DownloadExpiresAt = ticket.ExpiresAt;
  54. }
  55. else
  56. {
  57. // 兼容历史客户端:仅登录用户可获得旧永久地址。该地址无法撤销、无法防重放,
  58. // 管理员完成一次新方式发布后应清空旧配置。
  59. output.ApkUrl = await _sysConfigService.GetConfigValue<string>($"app_{platform}_apk_url");
  60. output.UsesLegacyPermanentUrl = !string.IsNullOrWhiteSpace(output.ApkUrl);
  61. }
  62. return output;
  63. }
  64. }
  65. /// <summary>
  66. /// APP 版本信息输出
  67. /// </summary>
  68. public class AppVersionOutput
  69. {
  70. /// <summary>
  71. /// 平台(android / ios)
  72. /// </summary>
  73. public string Platform { get; set; }
  74. /// <summary>
  75. /// Android applicationId。
  76. /// </summary>
  77. public string? PackageName { get; set; }
  78. /// <summary>
  79. /// 版本号(递增整数,对应 manifest.json versionCode)
  80. /// </summary>
  81. public int VersionCode { get; set; }
  82. /// <summary>
  83. /// 版本名(对应 manifest.json versionName)
  84. /// </summary>
  85. public string VersionName { get; set; }
  86. /// <summary>
  87. /// 安装包下载地址
  88. /// </summary>
  89. public string ApkUrl { get; set; }
  90. /// <summary>
  91. /// 短时下载地址过期时间。
  92. /// </summary>
  93. public DateTimeOffset? DownloadExpiresAt { get; set; }
  94. /// <summary>
  95. /// 是否使用旧永久地址兼容模式。
  96. /// </summary>
  97. public bool UsesLegacyPermanentUrl { get; set; }
  98. /// <summary>
  99. /// 是否强制更新
  100. /// </summary>
  101. public bool ForceUpdate { get; set; }
  102. /// <summary>
  103. /// 更新说明
  104. /// </summary>
  105. public string ReleaseNotes { get; set; }
  106. public DateTimeOffset? PublishedAt { get; set; }
  107. public long SizeBytes { get; set; }
  108. public string Sha256 { get; set; }
  109. }
  110. public static class AppReleaseConfig
  111. {
  112. public const string AndroidPackageName = "com.mdiop.app";
  113. public const string GroupCode = "app_release";
  114. public const int DefaultTicketTtlMinutes = 15;
  115. public const int DefaultSignedUrlTtlMinutes = 5;
  116. }
  117. public sealed class AppReleaseSnapshot
  118. {
  119. public string Platform { get; init; } = "android";
  120. public long FileId { get; init; }
  121. public int VersionCode { get; init; }
  122. public string VersionName { get; init; }
  123. public string ReleaseNotes { get; init; }
  124. public bool ForceUpdate { get; init; }
  125. public DateTimeOffset? PublishedAt { get; init; }
  126. public long SizeBytes { get; init; }
  127. public string Sha256 { get; init; }
  128. public bool IsComplete =>
  129. FileId > 0 &&
  130. VersionCode > 0 &&
  131. !string.IsNullOrWhiteSpace(VersionName) &&
  132. PublishedAt.HasValue &&
  133. SizeBytes > 0 &&
  134. Sha256?.Length == 64;
  135. public static async Task<AppReleaseSnapshot> Load(SysConfigService config, string platform = "android")
  136. {
  137. var prefix = $"app_{platform}_";
  138. var publishedAtRaw = await config.GetConfigValue<string>(prefix + "published_at");
  139. _ = DateTimeOffset.TryParse(
  140. publishedAtRaw,
  141. CultureInfo.InvariantCulture,
  142. DateTimeStyles.RoundtripKind,
  143. out var publishedAt);
  144. return new AppReleaseSnapshot
  145. {
  146. Platform = platform,
  147. FileId = await config.GetConfigValue<long>(prefix + "file_id"),
  148. VersionCode = await config.GetConfigValue<int>(prefix + "version_code"),
  149. VersionName = await config.GetConfigValue<string>(prefix + "version_name"),
  150. ReleaseNotes = await config.GetConfigValue<string>(prefix + "release_notes"),
  151. ForceUpdate = await config.GetConfigValue<bool>(prefix + "force_update"),
  152. PublishedAt = publishedAt == default ? null : publishedAt,
  153. SizeBytes = await config.GetConfigValue<long>(prefix + "size_bytes"),
  154. Sha256 = await config.GetConfigValue<string>(prefix + "sha256"),
  155. };
  156. }
  157. }