using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using HaloPixelToolBox.Backend.Utilities; using HaloPixelToolBox.Core.Utilities; using System.Collections.ObjectModel; using System.Net; using XFEExtension.NetCore.ServerInteractive.Models; namespace HaloPixelToolBox.Backend.ViewModels; public partial class IPBanManagePageViewModel : ViewModelBase { [ObservableProperty] public partial ObservableCollection Items { get; set; } = []; [ObservableProperty] public partial IPAddressInfo? SelectedItem { get; set; } [ObservableProperty] public partial string IPAddressText { get; set; } = string.Empty; [ObservableProperty] public partial string Notes { get; set; } = string.Empty; [ObservableProperty] public partial string StatusText { get; set; } = "正在确认管理员登录状态..."; [ObservableProperty] public partial bool IsBusy { get; set; } [RelayCommand] public async Task RefreshAsync() { if (!await EnsureLoggedInAsync()) return; IsBusy = true; try { var response = await DataManager.ClientRequester.Request>("get_bannedIPList"); if (response.StatusCode != HttpStatusCode.OK || response.Result is null) { StatusText = "读取 IP 黑名单失败,请确认登录状态和管理员权限"; return; } Items = new(response.Result.OrderByDescending(item => item.BannedTime)); StatusText = $"当前共封禁 {Items.Count} 个 IP"; } catch (Exception ex) { StatusText = $"读取 IP 黑名单失败:{ex.Message}"; } finally { IsBusy = false; } } [RelayCommand] private async Task AddAsync() { if (!await EnsureLoggedInAsync()) return; var input = IPAddressText.Trim(); if (!System.Net.IPAddress.TryParse(input, out var parsedAddress)) { StatusText = "请输入有效的 IPv4 或 IPv6 地址"; return; } var normalizedAddress = parsedAddress.ToString(); if (Items.Any(item => string.Equals(item.IPAddress, normalizedAddress, StringComparison.OrdinalIgnoreCase))) { StatusText = $"IP {normalizedAddress} 已在黑名单中"; return; } IsBusy = true; try { var response = await DataManager.ClientRequester.Request("add_bannedIP", normalizedAddress, Notes.Trim()); if (response.StatusCode != HttpStatusCode.OK) { StatusText = "封禁失败,请确认登录状态和管理员权限"; return; } IPAddressText = string.Empty; Notes = string.Empty; StatusText = $"IP {normalizedAddress} 已封禁"; await RefreshAsync(); } catch (Exception ex) { StatusText = $"封禁失败:{ex.Message}"; } finally { IsBusy = false; } } [RelayCommand] private async Task RemoveAsync() { if (!await EnsureLoggedInAsync()) return; if (SelectedItem is null) { StatusText = "请先选择要解封的 IP"; return; } IsBusy = true; try { var address = SelectedItem.IPAddress; var response = await DataManager.ClientRequester.Request("remove_bannedIP", address); if (response.StatusCode != HttpStatusCode.OK || !response.Result) { StatusText = "解封失败,请确认登录状态和管理员权限"; return; } SelectedItem = null; StatusText = $"IP {address} 已解封"; await RefreshAsync(); } catch (Exception ex) { StatusText = $"解封失败:{ex.Message}"; } finally { IsBusy = false; } } private async Task EnsureLoggedInAsync() { if (await AdministratorSessionManager.InitializeAsync()) return true; StatusText = $"{AdministratorSessionManager.StatusText};请前往设置完成管理员登录"; return false; } }