From d216edd0e9931d71664f33e625cff6d8131a0fad Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 13 三月 2026 16:00:40 +0800
Subject: [PATCH] 重构: 实现前后端分离架构

---
 Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Web/Pages/Edit.cshtml.cs |   77 +++++++++++++++++---------------------
 1 files changed, 35 insertions(+), 42 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Web/Pages/Edit.cshtml.cs b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Web/Pages/Edit.cshtml.cs
index 9310d00..4a9184d 100644
--- a/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Web/Pages/Edit.cshtml.cs
+++ b/Code/WCS/WIDESEAWCS_S7Simulator/WIDESEAWCS_S7Simulator.Web/Pages/Edit.cshtml.cs
@@ -3,21 +3,23 @@
 using System.ComponentModel.DataAnnotations;
 using WIDESEAWCS_S7Simulator.Core.Entities;
 using WIDESEAWCS_S7Simulator.Core.Enums;
+using WIDESEAWCS_S7Simulator.Web.Services;
 
 namespace WIDESEAWCS_S7Simulator.Web.Pages;
 
 /// <summary>
 /// 缂栬緫瀹炰緥椤�
 /// </summary>
-public class EditModel : PageModel
+public class EditModel : BasePageModel
 {
     private readonly ILogger<EditModel> _logger;
-    private readonly IHttpClientFactory _httpClientFactory;
+    private readonly ApiHttpClient _apiClient;
 
-    public EditModel(ILogger<EditModel> logger, IHttpClientFactory httpClientFactory)
+    public EditModel(ILogger<EditModel> logger, IConfiguration configuration, ApiHttpClient apiClient)
+        : base(configuration)
     {
         _logger = logger;
-        _httpClientFactory = httpClientFactory;
+        _apiClient = apiClient;
     }
 
     [BindProperty]
@@ -36,30 +38,35 @@
 
         try
         {
-            var httpClient = _httpClientFactory.CreateClient();
-            httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}");
+            // 鑾峰彇瀹炰緥鐘舵��
+            CurrentInstance = await _apiClient.GetInstanceAsync(id);
 
-            var response = await httpClient.GetAsync($"/api/SimulatorInstances/{Uri.EscapeDataString(id)}");
-
-            if (response.IsSuccessStatusCode)
+            if (CurrentInstance != null)
             {
-                CurrentInstance = await response.Content.ReadFromJsonAsync<InstanceState>();
-                IsRunning = CurrentInstance?.Status == InstanceStatus.Running;
+                IsRunning = CurrentInstance.Status == InstanceStatus.Running;
 
-                // Load existing config
-                var configResponse = await httpClient.GetAsync($"/api/SimulatorInstances");
-                if (configResponse.IsSuccessStatusCode)
+                // 鑾峰彇瀹炰緥閰嶇疆
+                var config = await _apiClient.GetInstanceConfigAsync(id);
+                if (config != null)
                 {
-                    var allInstances = await configResponse.Content.ReadFromJsonAsync<List<InstanceState>>();
-                    // For now, we'll initialize with defaults. In production, you'd have a separate endpoint to get instance config
-                }
+                    Input.Id = config.Id;
+                    Input.Name = config.Name;
+                    Input.PLCType = config.PLCType;
+                    Input.Port = config.Port;
+                    Input.ActivationKey = config.ActivationKey;
+                    Input.AutoStart = config.AutoStart;
 
-                // Initialize input with current values (default for now)
-                Input.Id = id;
-                Input.Name = CurrentInstance?.InstanceId ?? id;
-                Input.Port = 102; // Default
+                    // 鍐呭瓨閰嶇疆
+                    Input.MRegionSize = config.MemoryConfig.MRegionSize;
+                    Input.DBBlockCount = config.MemoryConfig.DBBlockCount;
+                    Input.DBBlockSize = config.MemoryConfig.DBBlockSize;
+                    Input.IRegionSize = config.MemoryConfig.IRegionSize;
+                    Input.QRegionSize = config.MemoryConfig.QRegionSize;
+                    Input.TRegionCount = config.MemoryConfig.TRegionCount;
+                    Input.CRegionCount = config.MemoryConfig.CRegionCount;
+                }
             }
-            else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
+            else
             {
                 return NotFound($"瀹炰緥 \"{id}\" 涓嶅瓨鍦�");
             }
@@ -77,17 +84,12 @@
     {
         if (!ModelState.IsValid)
         {
-            // Reload instance state
             await LoadInstanceStateAsync(Input.Id);
             return Page();
         }
 
         try
         {
-            // 鍒涘缓HttpClient骞惰缃瓸aseAddress
-            var httpClient = _httpClientFactory.CreateClient();
-            httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}");
-
             var config = new InstanceConfig
             {
                 Id = Input.Id,
@@ -108,22 +110,17 @@
                 }
             };
 
-            var response = await httpClient.PutAsJsonAsync($"/api/SimulatorInstances/{Uri.EscapeDataString(Input.Id)}", config);
+            var result = await _apiClient.UpdateInstanceAsync(Input.Id, config);
 
-            if (response.IsSuccessStatusCode)
+            if (result != null)
             {
                 _logger.LogInformation("Instance {InstanceId} updated successfully", Input.Id);
                 TempData["SuccessMessage"] = $"瀹炰緥 \"{Input.Id}\" 鏇存柊鎴愬姛!";
                 return RedirectToPage("/Index");
             }
-            else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
-            {
-                ModelState.AddModelError(string.Empty, "瀹炰緥涓嶅瓨鍦�");
-            }
             else
             {
-                var error = await response.Content.ReadFromJsonAsync<object>();
-                ModelState.AddModelError(string.Empty, error?.ToString() ?? "鏇存柊瀹炰緥澶辫触");
+                ModelState.AddModelError(string.Empty, "鏇存柊瀹炰緥澶辫触");
             }
         }
         catch (HttpRequestException ex)
@@ -140,14 +137,10 @@
     {
         try
         {
-            var httpClient = _httpClientFactory.CreateClient();
-            httpClient.BaseAddress = new Uri($"{Request.Scheme}://{Request.Host}");
-
-            var response = await httpClient.GetAsync($"/api/SimulatorInstances/{Uri.EscapeDataString(id)}");
-            if (response.IsSuccessStatusCode)
+            CurrentInstance = await _apiClient.GetInstanceAsync(id);
+            if (CurrentInstance != null)
             {
-                CurrentInstance = await response.Content.ReadFromJsonAsync<InstanceState>();
-                IsRunning = CurrentInstance?.Status == InstanceStatus.Running;
+                IsRunning = CurrentInstance.Status == InstanceStatus.Running;
             }
         }
         catch

--
Gitblit v1.9.3