impl #1112 (part 1)

This commit is contained in:
qhy040404
2023-12-16 22:28:39 +08:00
parent c5e0221a0b
commit 879b930ea6
5 changed files with 53 additions and 10 deletions

View File

@@ -14,7 +14,8 @@ internal interface IAnnouncementService
/// <summary> /// <summary>
/// 异步获取游戏公告与活动,通常会进行缓存 /// 异步获取游戏公告与活动,通常会进行缓存
/// </summary> /// </summary>
/// <param name="languageCode">语言代码</param>
/// <param name="cancellationToken">取消令牌</param> /// <param name="cancellationToken">取消令牌</param>
/// <returns>公告包装器</returns> /// <returns>公告包装器</returns>
ValueTask<AnnouncementWrapper> GetAnnouncementWrapperAsync(CancellationToken cancellationToken = default); ValueTask<AnnouncementWrapper> GetAnnouncementWrapperAsync(string languageCode, CancellationToken cancellationToken = default);
} }

View File

@@ -25,7 +25,7 @@ internal sealed partial class AnnouncementService : IAnnouncementService
private readonly IMemoryCache memoryCache; private readonly IMemoryCache memoryCache;
/// <inheritdoc/> /// <inheritdoc/>
public async ValueTask<AnnouncementWrapper> GetAnnouncementWrapperAsync(CancellationToken cancellationToken = default) public async ValueTask<AnnouncementWrapper> GetAnnouncementWrapperAsync(string languageCode, CancellationToken cancellationToken = default)
{ {
// 缓存中存在记录,直接返回 // 缓存中存在记录,直接返回
if (memoryCache.TryGetRequiredValue(CacheKey, out AnnouncementWrapper? cache)) if (memoryCache.TryGetRequiredValue(CacheKey, out AnnouncementWrapper? cache))
@@ -35,7 +35,7 @@ internal sealed partial class AnnouncementService : IAnnouncementService
await taskContext.SwitchToBackgroundAsync(); await taskContext.SwitchToBackgroundAsync();
Response<AnnouncementWrapper> announcementWrapperResponse = await announcementClient Response<AnnouncementWrapper> announcementWrapperResponse = await announcementClient
.GetAnnouncementsAsync(cancellationToken) .GetAnnouncementsAsync(languageCode, cancellationToken)
.ConfigureAwait(false); .ConfigureAwait(false);
if (!announcementWrapperResponse.IsOk()) if (!announcementWrapperResponse.IsOk())
@@ -45,7 +45,7 @@ internal sealed partial class AnnouncementService : IAnnouncementService
AnnouncementWrapper wrapper = announcementWrapperResponse.Data; AnnouncementWrapper wrapper = announcementWrapperResponse.Data;
Response<ListWrapper<AnnouncementContent>> announcementContentResponse = await announcementClient Response<ListWrapper<AnnouncementContent>> announcementContentResponse = await announcementClient
.GetAnnouncementContentsAsync(cancellationToken) .GetAnnouncementContentsAsync(languageCode, cancellationToken)
.ConfigureAwait(false); .ConfigureAwait(false);
if (!announcementContentResponse.IsOk()) if (!announcementContentResponse.IsOk())

View File

@@ -4,6 +4,7 @@
using Snap.Hutao.Core.Setting; using Snap.Hutao.Core.Setting;
using Snap.Hutao.Service.Abstraction; using Snap.Hutao.Service.Abstraction;
using Snap.Hutao.Service.Hutao; using Snap.Hutao.Service.Hutao;
using Snap.Hutao.Service.Metadata;
using Snap.Hutao.View.Card; using Snap.Hutao.View.Card;
using Snap.Hutao.View.Card.Primitive; using Snap.Hutao.View.Card.Primitive;
using Snap.Hutao.Web.Hoyolab.Hk4e.Common.Announcement; using Snap.Hutao.Web.Hoyolab.Hk4e.Common.Announcement;
@@ -23,6 +24,7 @@ internal sealed partial class AnnouncementViewModel : Abstraction.ViewModel
private readonly IAnnouncementService announcementService; private readonly IAnnouncementService announcementService;
private readonly HutaoUserOptions hutaoUserOptions; private readonly HutaoUserOptions hutaoUserOptions;
private readonly ITaskContext taskContext; private readonly ITaskContext taskContext;
private readonly MetadataOptions metadataOptions;
private AnnouncementWrapper? announcement; private AnnouncementWrapper? announcement;
private string greetingText = SH.ViewPageHomeGreetingTextDefault; private string greetingText = SH.ViewPageHomeGreetingTextDefault;
@@ -61,7 +63,7 @@ internal sealed partial class AnnouncementViewModel : Abstraction.ViewModel
{ {
try try
{ {
AnnouncementWrapper announcementWrapper = await announcementService.GetAnnouncementWrapperAsync(CancellationToken).ConfigureAwait(false); AnnouncementWrapper announcementWrapper = await announcementService.GetAnnouncementWrapperAsync(metadataOptions.LanguageCode, CancellationToken).ConfigureAwait(false);
await taskContext.SwitchToMainThreadAsync(); await taskContext.SwitchToMainThreadAsync();
Announcement = announcementWrapper; Announcement = announcementWrapper;
} }
@@ -138,4 +140,4 @@ internal sealed partial class AnnouncementViewModel : Abstraction.ViewModel
Cards = result; Cards = result;
} }
} }

View File

@@ -189,6 +189,32 @@ internal static class ApiOsEndpoints
} }
#endregion #endregion
#region Hk4eApiOsAnnouncementApi
/// <summary>
/// 公告列表
/// </summary>
/// <param name="languageCode">语言代码</param>
/// <param name="region">服务器</param>
/// <returns>公告列表Url</returns>
public static string AnnList(string languageCode, string region)
{
return $"{Hk4eApiOsAnnouncementApi}/getAnnList?{AnnouncementQuery(languageCode, region)}";
}
/// <summary>
/// 公告内容
/// </summary>
/// <param name="languageCode">语言代码</param>
/// <param name="region">服务器</param>
/// <returns>公告内容Url</returns>
public static string AnnContent(string languageCode, string region)
{
return $"{Hk4eApiOsAnnouncementApi}/getAnnContent?{AnnouncementQuery(languageCode, region)}";
}
#endregion
#region SgPublicApi #region SgPublicApi
/// <summary> /// <summary>
@@ -307,6 +333,7 @@ internal static class ApiOsEndpoints
private const string BbsApiOsGameRecordAppApi = $"{BbsApiOs}/game_record/app/genshin/api"; private const string BbsApiOsGameRecordAppApi = $"{BbsApiOs}/game_record/app/genshin/api";
private const string Hk4eApiOs = "https://hk4e-api-os.hoyoverse.com"; private const string Hk4eApiOs = "https://hk4e-api-os.hoyoverse.com";
private const string Hk4eApiOsAnnouncementApi = $"{Hk4eApiOs}/common/hk4e_global/announcement/api";
private const string Hk4eApiOsGachaInfoApi = $"{Hk4eApiOs}/gacha_info/api"; private const string Hk4eApiOsGachaInfoApi = $"{Hk4eApiOs}/gacha_info/api";
private const string SdkOsStatic = "https://sdk-os-static.mihoyo.com"; private const string SdkOsStatic = "https://sdk-os-static.mihoyo.com";
@@ -333,5 +360,10 @@ internal static class ApiOsEndpoints
/// </summary> /// </summary>
public const string AppHoyolabReferer = "https://app.hoyolab.com/"; public const string AppHoyolabReferer = "https://app.hoyolab.com/";
private static string AnnouncementQuery(string languageCode, string region)
{
return $"game=hk4e&game_biz=hk4e_global&lang={languageCode}&bundle_id=hk4e_global&platform=pc&region={region}&level=55&uid=100000000";
}
#endregion #endregion
} }

View File

@@ -23,12 +23,16 @@ internal sealed partial class AnnouncementClient
/// <summary> /// <summary>
/// 异步获取公告列表 /// 异步获取公告列表
/// </summary> /// </summary>
/// <param name="languageCode">语言代码</param>
/// <param name="token">取消令牌</param> /// <param name="token">取消令牌</param>
/// <returns>公告列表</returns> /// <returns>公告列表</returns>
public async ValueTask<Response<AnnouncementWrapper>> GetAnnouncementsAsync(CancellationToken token = default) public async ValueTask<Response<AnnouncementWrapper>> GetAnnouncementsAsync(string languageCode, CancellationToken token = default)
{ {
// ApiOsEndpoints.AnnList(languageCode, region)
string annListUrl = ApiEndpoints.AnnList;
HttpRequestMessageBuilder builder = httpRequestMessageBuilderFactory.Create() HttpRequestMessageBuilder builder = httpRequestMessageBuilderFactory.Create()
.SetRequestUri(ApiEndpoints.AnnList) .SetRequestUri(annListUrl)
.Get(); .Get();
Response<AnnouncementWrapper>? resp = await builder Response<AnnouncementWrapper>? resp = await builder
@@ -41,12 +45,16 @@ internal sealed partial class AnnouncementClient
/// <summary> /// <summary>
/// 异步获取公告内容列表 /// 异步获取公告内容列表
/// </summary> /// </summary>
/// <param name="languageCode">语言代码</param>
/// <param name="token">取消令牌</param> /// <param name="token">取消令牌</param>
/// <returns>公告内容列表</returns> /// <returns>公告内容列表</returns>
public async ValueTask<Response<ListWrapper<AnnouncementContent>>> GetAnnouncementContentsAsync(CancellationToken token = default) public async ValueTask<Response<ListWrapper<AnnouncementContent>>> GetAnnouncementContentsAsync(string languageCode, CancellationToken token = default)
{ {
// ApiOsEndpoints.AnnContent(languageCode, region)
string annContentUrl = ApiEndpoints.AnnContent;
HttpRequestMessageBuilder builder = httpRequestMessageBuilderFactory.Create() HttpRequestMessageBuilder builder = httpRequestMessageBuilderFactory.Create()
.SetRequestUri(ApiEndpoints.AnnContent) .SetRequestUri(annContentUrl)
.Get(); .Get();
Response<ListWrapper<AnnouncementContent>>? resp = await builder Response<ListWrapper<AnnouncementContent>>? resp = await builder