Files
petwash/PetWashControl/Services/ApiService.cs
GukSang.Jin 0a884fa6cb 更新
2026-03-18 13:53:44 +08:00

128 lines
4.0 KiB
C#

using System.Net.Http;
using System.Net.Http.Json;
using PetWashControl.Models;
namespace PetWashControl.Services;
public class ApiService
{
private readonly HttpClient _httpClient;
private readonly ConfigurationService _config;
public ApiService(ConfigurationService? config = null)
{
_config = config ?? new ConfigurationService();
_httpClient = new HttpClient
{
BaseAddress = new Uri(_config.ApiBaseUrl),
Timeout = TimeSpan.FromSeconds(30)
};
}
public async Task<List<Package>> GetPackagesAsync()
{
try
{
return await _httpClient.GetFromJsonAsync<List<Package>>("api/packages")
?? new List<Package>();
}
catch (HttpRequestException ex)
{
throw new Exception($"无法连接到服务器: {ex.Message}", ex);
}
}
public async Task<CreateOrderResponse?> CreateOrderAsync(int packageId)
{
try
{
var response = await _httpClient.PostAsJsonAsync("api/orders", new { packageId });
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<CreateOrderResponse>();
}
catch (HttpRequestException ex)
{
throw new Exception($"创建订单失败: {ex.Message}", ex);
}
}
public async Task<Order?> ConfirmPaymentAsync(int orderId)
{
try
{
var response = await _httpClient.PostAsync($"api/orders/{orderId}/payment", null);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<Order>();
}
catch (HttpRequestException ex)
{
throw new Exception($"确认支付失败: {ex.Message}", ex);
}
}
public async Task<PaymentStatusResponse?> GetPaymentStatusAsync(int orderId, string outTradeNo)
{
try
{
return await _httpClient.GetFromJsonAsync<PaymentStatusResponse>(
$"api/orders/{orderId}/payment-status?outTradeNo={Uri.EscapeDataString(outTradeNo)}");
}
catch (HttpRequestException ex)
{
throw new Exception($"获取支付状态失败: {ex.Message}", ex);
}
}
public async Task<Order?> GetOrderAsync(int orderId)
{
try
{
return await _httpClient.GetFromJsonAsync<Order>($"api/orders/{orderId}");
}
catch (HttpRequestException ex)
{
throw new Exception($"获取订单失败: {ex.Message}", ex);
}
}
public async Task<Order?> UpdateOrderStatusAsync(int orderId, OrderStatus status)
{
try
{
var response = await _httpClient.PutAsJsonAsync($"api/orders/{orderId}/status", new { status });
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<Order>();
}
catch (HttpRequestException ex)
{
throw new Exception($"更新订单状态失败: {ex.Message}", ex);
}
}
public async Task<Package?> UpdatePackageAsync(Package package)
{
try
{
var response = await _httpClient.PutAsJsonAsync($"api/packages/{package.Id}", new
{
package.Price,
package.FirstSprayWaterTime,
package.AfterShampoo1SprayTime,
package.AfterShampoo2SprayTime,
package.AfterShampoo3SprayTime,
package.SprayShampoo1Time,
package.SprayShampoo2Time,
package.SprayShampoo3Time,
package.HotAirTime,
package.ColdAirTime,
package.UvSterilizationTime
});
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<Package>();
}
catch (HttpRequestException ex)
{
throw new Exception($"保存套餐配置失败: {ex.Message}", ex);
}
}
}