From c077fa998949d32bef48debce2644ab954bca74f Mon Sep 17 00:00:00 2001 From: Administrator Date: Thu, 5 Mar 2026 09:30:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=82=E9=85=8D=E4=BE=9B=E5=BA=94?= =?UTF-8?q?=E5=95=86=E5=8F=91=E8=B4=A7=E6=8E=A5=E5=8F=A3=E5=B9=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8D=95=E8=AE=A2=E5=8D=95=E5=8F=91=E8=B4=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PackagingMallShipper/App.config | 1 + PackagingMallShipper/Helpers/AppConfig.cs | 8 +++ PackagingMallShipper/Services/ShipService.cs | 38 +++++++++----- .../ViewModels/OrderListViewModel.cs | 52 +++++++++++++++++++ PackagingMallShipper/Views/OrderListView.xaml | 33 +++++++++++- .../Views/ShippingDialog.xaml | 2 +- 6 files changed, 117 insertions(+), 17 deletions(-) diff --git a/PackagingMallShipper/App.config b/PackagingMallShipper/App.config index 7ddabb6..de18015 100644 --- a/PackagingMallShipper/App.config +++ b/PackagingMallShipper/App.config @@ -6,6 +6,7 @@ + diff --git a/PackagingMallShipper/Helpers/AppConfig.cs b/PackagingMallShipper/Helpers/AppConfig.cs index 1162cd7..599a948 100644 --- a/PackagingMallShipper/Helpers/AppConfig.cs +++ b/PackagingMallShipper/Helpers/AppConfig.cs @@ -7,6 +7,9 @@ namespace PackagingMallShipper.Helpers public static string ApiBaseUrl => ConfigurationManager.AppSettings["ApiBaseUrl"] ?? "https://user.api.it120.cc"; + public static string CommonApiBaseUrl => + ConfigurationManager.AppSettings["CommonApiBaseUrl"] ?? "https://common.apifm.com"; + public static string SubDomain => ConfigurationManager.AppSettings["SubDomain"] ?? "vv125s"; @@ -23,5 +26,10 @@ namespace PackagingMallShipper.Helpers { return $"{ApiBaseUrl}/{SubDomain}{endpoint}"; } + + public static string GetCommonApiUrl(string endpoint) + { + return $"{CommonApiBaseUrl}{endpoint}"; + } } } diff --git a/PackagingMallShipper/Services/ShipService.cs b/PackagingMallShipper/Services/ShipService.cs index 278e15c..51d3f48 100644 --- a/PackagingMallShipper/Services/ShipService.cs +++ b/PackagingMallShipper/Services/ShipService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Data.SQLite; +using System.Diagnostics; using System.Linq; using System.Net.Http; using System.Threading; @@ -22,37 +23,38 @@ namespace PackagingMallShipper.Services public ShipService(IAuthService authService) { _authService = authService; - _httpClient = new HttpClient(); - _httpClient.Timeout = TimeSpan.FromSeconds(30); + _httpClient = new HttpClient + { + Timeout = TimeSpan.FromSeconds(30) + }; } public async Task ShipOrderAsync(ShipOrderRequest request) { var token = _authService.GetToken(); if (string.IsNullOrEmpty(token)) - throw new UnauthorizedAccessException("未登录"); + throw new UnauthorizedAccessException("未登录,请先登录"); UpdateLocalOrderStatus(request.OrderId, "shipping"); try { - var url = AppConfig.GetApiUrl($"/order/delivery") + - $"?orderId={request.OrderId}" + - $"&expressType={request.ExpressCompanyId}" + - $"&shipperCode={Uri.EscapeDataString(request.TrackingNumber)}"; + var query = $"?id={request.OrderId}" + + $"&expressCompanyId={request.ExpressCompanyId}" + + $"&number={Uri.EscapeDataString(request.TrackingNumber)}"; + + var url = AppConfig.GetCommonApiUrl("/apifmUser/fsmRepair/fahuo") + query; _httpClient.DefaultRequestHeaders.Clear(); _httpClient.DefaultRequestHeaders.Add("X-Token", token); - var response = await _httpClient.PostAsync(url, null); - var json = await response.Content.ReadAsStringAsync(); - var result = JsonConvert.DeserializeObject>(json); + Debug.WriteLine($"[发货请求] URL: {url}"); + var result = await PostShipRequestAsync(url); if (result?.Code != 0) throw new Exception(result?.Msg ?? "发货失败"); UpdateLocalOrderAfterShip(request); - return new ShipResult { Success = true }; } catch (Exception ex) @@ -73,7 +75,7 @@ namespace PackagingMallShipper.Services Results = new List() }; - int completed = 0; + var completed = 0; using (var semaphore = new SemaphoreSlim(concurrency)) { @@ -130,6 +132,15 @@ namespace PackagingMallShipper.Services return result; } + private async Task> PostShipRequestAsync(string url) + { + var response = await _httpClient.PostAsync(url, null); + var json = await response.Content.ReadAsStringAsync(); + Debug.WriteLine($"[发货响应] URL: {url}"); + Debug.WriteLine($"[发货响应] HTTP: {response.StatusCode}, body: {json}"); + return JsonConvert.DeserializeObject>(json); + } + private void UpdateLocalOrderStatus(int orderId, string status, string errorMsg = null) { using (var conn = SqliteHelper.GetConnection()) @@ -165,8 +176,7 @@ namespace PackagingMallShipper.Services using (var cmd = new SQLiteCommand(sql, conn)) { cmd.Parameters.AddWithValue("@expressId", request.ExpressCompanyId); - cmd.Parameters.AddWithValue("@expressName", - ExpressCompanies.GetName(request.ExpressCompanyId)); + cmd.Parameters.AddWithValue("@expressName", ExpressCompanies.GetName(request.ExpressCompanyId)); cmd.Parameters.AddWithValue("@trackingNumber", request.TrackingNumber); cmd.Parameters.AddWithValue("@dateShip", DateTime.Now); cmd.Parameters.AddWithValue("@now", DateTime.Now); diff --git a/PackagingMallShipper/ViewModels/OrderListViewModel.cs b/PackagingMallShipper/ViewModels/OrderListViewModel.cs index fafb0b0..08512a8 100644 --- a/PackagingMallShipper/ViewModels/OrderListViewModel.cs +++ b/PackagingMallShipper/ViewModels/OrderListViewModel.cs @@ -11,6 +11,7 @@ using Microsoft.Win32; using PackagingMallShipper.Helpers; using PackagingMallShipper.Models; using PackagingMallShipper.Services; +using PackagingMallShipper.Views; namespace PackagingMallShipper.ViewModels { @@ -633,6 +634,57 @@ namespace PackagingMallShipper.ViewModels "提示", MessageBoxButton.OK, MessageBoxImage.Information); } + [RelayCommand] + private async Task ShipSingleOrder(Order order) + { + if (order == null || order.Status != 1) return; + + var dialog = new ShippingDialog(order); + var activeWindow = Application.Current.Windows.OfType().FirstOrDefault(w => w.IsActive) + ?? Application.Current.Windows.OfType().FirstOrDefault(w => w.IsVisible); + if (activeWindow != null) + dialog.Owner = activeWindow; + + if (dialog.ShowDialog() == true) + { + IsBusy = true; + StatusMessage = $"正在发货 {order.OrderNumber}..."; + + try + { + var request = new ShipOrderRequest + { + OrderId = order.Id, + OrderNumber = order.OrderNumber, + ExpressCompanyId = dialog.SelectedExpressId, + TrackingNumber = dialog.TrackingNumber + }; + + var result = await _shipService.ShipOrderAsync(request); + if (result.Success) + { + StatusMessage = $"订单 {order.OrderNumber} 发货成功"; + await RefreshOrdersAsync(); + await UpdateCountsAsync(); + } + else + { + StatusMessage = $"发货失败: {result.Message}"; + MessageBox.Show($"发货失败: {result.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + catch (Exception ex) + { + StatusMessage = $"发货失败: {ex.Message}"; + MessageBox.Show($"发货失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + } + finally + { + IsBusy = false; + } + } + } + private async Task UpdateCountsAsync() { try diff --git a/PackagingMallShipper/Views/OrderListView.xaml b/PackagingMallShipper/Views/OrderListView.xaml index f998cfc..d94683d 100644 --- a/PackagingMallShipper/Views/OrderListView.xaml +++ b/PackagingMallShipper/Views/OrderListView.xaml @@ -100,12 +100,10 @@ Width="90" Height="30" Margin="0,0,5,0" IsEnabled="{Binding IsBusy, Converter={StaticResource InverseBool}}"/> - @@ -163,6 +161,37 @@ Width="70"/> + + + + + + + + diff --git a/PackagingMallShipper/Views/ShippingDialog.xaml b/PackagingMallShipper/Views/ShippingDialog.xaml index 915b010..398474a 100644 --- a/PackagingMallShipper/Views/ShippingDialog.xaml +++ b/PackagingMallShipper/Views/ShippingDialog.xaml @@ -2,7 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="填写发货信息" - Height="280" Width="400" + Height="320" Width="400" WindowStartupLocation="CenterOwner" ResizeMode="NoResize" ShowInTaskbar="False">