| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Text.Json;
- namespace XafPet.Desktop.PetEngine;
- /// <summary>CFC 后端 API 客户端。所有接口统一 POST + JWT Bearer。</summary>
- public sealed class ApiClient
- {
- private readonly HttpClient _http;
- private readonly string _baseUrl;
- private string _token;
- private static readonly JsonSerializerOptions JsonOpts = new(JsonSerializerDefaults.Web);
- public ApiClient(HttpClient http, string baseUrl, string token)
- {
- _http = http;
- _baseUrl = baseUrl.TrimEnd('/');
- _token = token;
- }
- public void UpdateToken(string token) => _token = token;
- private async Task<T?> PostAsync<T>(string path, object? body = null)
- {
- using var req = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}{path}");
- req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token);
- if (body != null)
- {
- req.Content = new StringContent(JsonSerializer.Serialize(body, JsonOpts),
- Encoding.UTF8, "application/json");
- }
- var resp = await _http.SendAsync(req);
- var json = await resp.Content.ReadAsStringAsync();
- var result = JsonSerializer.Deserialize<ApiResult<T>>(json, JsonOpts)
- ?? throw new ApiException(-1, "响应解析失败");
- if (!result.IsSuccess)
- throw new ApiException(result.Code, result.Message ?? "未知错误");
- return result.Data;
- }
- /// <summary>POST /api/tasks/today — 今日任务</summary>
- public Task<List<CfcTask>?> GetTodayTasksAsync()
- => PostAsync<List<CfcTask>>("/api/tasks/today", new Dictionary<string, object>());
- /// <summary>POST /api/energy/overview — 五维能量概览</summary>
- public Task<EnergyOverview?> GetEnergyOverviewAsync()
- => PostAsync<EnergyOverview>("/api/energy/overview");
- /// <summary>POST /api/daily/checkin — 今日打卡状态</summary>
- public Task<DailyCheckin?> GetDailyCheckinAsync()
- => PostAsync<DailyCheckin>("/api/daily/checkin");
- /// <summary>POST /api/daily/checkin — 完成喝水打卡(body: {"type":"water"})</summary>
- public async Task<ApiResult<object>?> CompleteDrinkWaterAsync()
- {
- using var req = new HttpRequestMessage(HttpMethod.Post, $"{_baseUrl}/api/daily/checkin");
- req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token);
- req.Content = new StringContent("""{"type":"water"}""", Encoding.UTF8, "application/json");
- var resp = await _http.SendAsync(req);
- var json = await resp.Content.ReadAsStringAsync();
- var result = JsonSerializer.Deserialize<ApiResult<object>>(json, JsonOpts)
- ?? throw new ApiException(-1, "响应解析失败");
- if (!result.IsSuccess)
- throw new ApiException(result.Code, result.Message ?? "未知错误");
- return result;
- }
- /// <summary>POST /api/user/mascot/get — 获取用户已选吉祥物</summary>
- public Task<MascotInfo?> GetMascotAsync()
- => PostAsync<MascotInfo>("/api/user/mascot/get");
- /// <summary>POST /api/user/mascot/set — 设置吉祥物</summary>
- public Task<object?> SetMascotAsync(string mascotId)
- => PostAsync<object>("/api/user/mascot/set", new { mascotId });
- }
|