using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using System; using System.Collections.Generic; using System.Collections.ObjectModel; namespace AIK.ViewModels.IDCardCopy; public partial class AccessCardCopy : ObservableObject { [ObservableProperty] private string _deviceStatus = "设备已连接"; [ObservableProperty] private string _readerStatus = "待机"; [ObservableProperty] private string _currentMode = "IC/ID 自动识别"; [ObservableProperty] private string _actionHint = "点击“开始识别”读取原卡信息,或直接查看设备待机状态。"; [ObservableProperty] private bool _isCopyEnabled; [ObservableProperty] private bool _isBusy; [ObservableProperty] private CardInfoItem _sourceCard = CreateSourcePlaceholder(); [ObservableProperty] private CardInfoItem _targetCard = CreateTargetPlaceholder(); public ObservableCollection SectorBlockRows { get; } = new(); public ObservableCollection SourceSectorContents { get; } = new(); public ObservableCollection TargetSectorContents { get; } = new(); public ObservableCollection Logs { get; } = new(); public AccessCardCopy() { ResetSectorContents(); ResetSectorBlockRows(); AppendLog("页面已加载,设备待机中。"); } [RelayCommand] private void Identify() { IsBusy = true; ReaderStatus = "识别中"; ActionHint = "请将原卡放置在左侧识别区域附近。"; AppendLog("开始识别原卡。"); SourceCard = new CardInfoItem { Title = "原卡信息", CardType = "IC 卡", CardNumber = "04 A2 7F 19", Uid = "8F 23 91 C0", Protocol = "Mifare 1K", Frequency = "13.56 MHz", SectorCount = "16", WritableStatus = "可复制", CurrentStatus = "识别成功" }; ReplaceSectorContents(SourceSectorContents, CreateSourceSectorContents()); ReplaceSectorContents(TargetSectorContents, CreateTargetSectorPlaceholders()); ReaderStatus = "等待目标卡"; ActionHint = "原卡识别完成,当前可放入目标卡并点击“开始拷贝”。"; IsCopyEnabled = true; IsBusy = false; AppendLog("原卡识别成功,可进入拷贝流程。"); } [RelayCommand] private void Copy() { if (!IsCopyEnabled) { AppendLog("未识别原卡,拷贝操作已忽略。"); return; } IsBusy = true; ReaderStatus = "写入中"; ActionHint = "请保持目标卡稳定,系统正在模拟写入。"; AppendLog("检测到目标卡,开始写入。"); TargetCard = new CardInfoItem { Title = "目标卡信息", CardType = SourceCard.CardType, CardNumber = SourceCard.CardNumber, Uid = SourceCard.Uid, Protocol = SourceCard.Protocol, Frequency = SourceCard.Frequency, SectorCount = SourceCard.SectorCount, WritableStatus = "已写入", CurrentStatus = "写入成功" }; ReplaceSectorContents(TargetSectorContents, CreateTargetSectorContents()); ActionHint = "拷贝完成,可继续识别下一张卡或清空当前数据。"; ReaderStatus = "完成"; IsBusy = false; AppendLog("目标卡写入成功,拷贝完成。"); } [RelayCommand] private void Swap() { var source = SourceCard.Clone(); var sourceSectors = CloneSectorContents(SourceSectorContents); var targetSectors = CloneSectorContents(TargetSectorContents); SourceCard = TargetCard.Clone(); TargetCard = source; SourceCard.Title = "原卡信息"; TargetCard.Title = "目标卡信息"; ReplaceSectorContents(SourceSectorContents, targetSectors); ReplaceSectorContents(TargetSectorContents, sourceSectors); AppendLog("左右卡片信息已交换。"); } [RelayCommand] private void Clear() { DeviceStatus = "设备已连接"; ReaderStatus = "待机"; CurrentMode = "IC/ID 自动识别"; ActionHint = "点击“开始识别”读取原卡信息,或直接查看设备待机状态。"; IsCopyEnabled = false; IsBusy = false; SourceCard = CreateSourcePlaceholder(); TargetCard = CreateTargetPlaceholder(); ResetSectorContents(); ResetSectorBlockRows(); AppendLog("页面数据已清空,等待新的卡片操作。"); } private void ResetSectorContents() { ReplaceSectorContents(SourceSectorContents, CreateSourceSectorPlaceholders()); ReplaceSectorContents(TargetSectorContents, CreateTargetSectorPlaceholders()); } private void ResetSectorBlockRows() { SectorBlockRows.Clear(); foreach (var item in CreateSectorBlockRows()) { SectorBlockRows.Add(item); } } private void AppendLog(string message) { Logs.Insert(0, new OperationLogItem { Time = DateTime.Now.ToString("HH:mm:ss"), Message = message }); } private static void ReplaceSectorContents( ObservableCollection target, IEnumerable items) { target.Clear(); foreach (var item in items) { target.Add(item); } } private static List CloneSectorContents(IEnumerable items) { var result = new List(); foreach (var item in items) { result.Add(item.Clone()); } return result; } private static List CreateSourceSectorPlaceholders() { return new List { new SectorContentItem { SectorLabel = "--", BlockLabel = "--", DataPreview = "等待识别后显示原卡扇区内容。", Status = "待识别" } }; } private static List CreateTargetSectorPlaceholders() { return new List { new SectorContentItem { SectorLabel = "--", BlockLabel = "--", DataPreview = "等待拷贝后显示目标卡扇区内容。", Status = "待写入" } }; } private static List CreateSourceSectorContents() { return new List { new SectorContentItem { SectorLabel = "扇区 00", BlockLabel = "块 00", DataPreview = "04 A2 7F 19 88 04 00 00 12 34 56 78 9A BC DE F0", Status = "已读取" }, new SectorContentItem { SectorLabel = "扇区 01", BlockLabel = "块 04", DataPreview = "8F 23 91 C0 11 22 33 44 55 66 77 88 AA BB CC DD", Status = "已读取" }, new SectorContentItem { SectorLabel = "扇区 02", BlockLabel = "块 08", DataPreview = "13 56 4D 49 46 41 52 45 20 31 4B 00 00 00 00 00", Status = "已读取" }, new SectorContentItem { SectorLabel = "扇区 03", BlockLabel = "块 0C", DataPreview = "FF FF FF FF FF FF 07 80 69 C1 02 A4 B5 C6 D7 E8", Status = "校验完成" } }; } private static List CreateTargetSectorContents() { return new List { new SectorContentItem { SectorLabel = "扇区 00", BlockLabel = "块 00", DataPreview = "04 A2 7F 19 88 04 00 00 12 34 56 78 9A BC DE F0", Status = "已写入" }, new SectorContentItem { SectorLabel = "扇区 01", BlockLabel = "块 04", DataPreview = "8F 23 91 C0 11 22 33 44 55 66 77 88 AA BB CC DD", Status = "已写入" }, new SectorContentItem { SectorLabel = "扇区 02", BlockLabel = "块 08", DataPreview = "13 56 4D 49 46 41 52 45 20 31 4B 00 00 00 00 00", Status = "已写入" }, new SectorContentItem { SectorLabel = "扇区 03", BlockLabel = "块 0C", DataPreview = "FF FF FF FF FF FF 07 80 69 C1 02 A4 B5 C6 D7 E8", Status = "已写入" } }; } private static List CreateSectorBlockRows() { return new List { new SectorBlockRowItem { Sector = "扇区 00", Block = "块 00", Content = "11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00" }, new SectorBlockRowItem { Sector = "扇区 01", Block = "块 04", Content = "A1 B2 C3 D4 E5 F6 00 11 22 33 44 55 66 77 88 99" }, new SectorBlockRowItem { Sector = "扇区 02", Block = "块 08", Content = "01 03 05 07 09 0B 0D 0F 10 12 14 16 18 1A 1C 1E" }, new SectorBlockRowItem { Sector = "扇区 03", Block = "块 0C", Content = "FE DC BA 98 76 54 32 10 89 67 45 23 01 EF CD AB" } }; } private static CardInfoItem CreateSourcePlaceholder() { return new CardInfoItem { Title = "原卡信息", CardType = "--", CardNumber = "--", Uid = "--", Protocol = "--", Frequency = "--", SectorCount = "--", WritableStatus = "--", CurrentStatus = "待识别" }; } private static CardInfoItem CreateTargetPlaceholder() { return new CardInfoItem { Title = "目标卡信息", CardType = "--", CardNumber = "--", Uid = "--", Protocol = "--", Frequency = "--", SectorCount = "--", WritableStatus = "--", CurrentStatus = "待写入" }; } }