2026-02-25 15:43:47 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:38:08 +08:00
|
|
|
public async Task<CreateOrderResponse?> CreateOrderAsync(int packageId)
|
2026-02-25 15:43:47 +08:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var response = await _httpClient.PostAsJsonAsync("api/orders", new { packageId });
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
2026-03-16 15:38:08 +08:00
|
|
|
return await response.Content.ReadFromJsonAsync<CreateOrderResponse>();
|
2026-02-25 15:43:47 +08:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 15:38:08 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-25 15:43:47 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|