using System; using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using EveryPublish.Client.Models; using EveryPublish.Client.Services; namespace EveryPublish.Client.Pages; public sealed partial class ChallengesPage : Page { private ClientContext _ctx = null!; private DispatcherTimer? _timer; private ChallengeItem? _selected; public ChallengesPage() { InitializeComponent(); _timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) }; _timer.Tick += async (s, e) => await RefreshAsync(); } protected override void OnNavigatedTo(NavigationEventArgs e) { _ctx = (ClientContext)e.Parameter; _ctx.Core.ReloadLocalJson(); _timer?.Start(); _ = RefreshAsync(); } protected override void OnNavigatedFrom(NavigationEventArgs e) { _timer?.Stop(); } private async Task RefreshAsync() { var list = await _ctx.Core.GetChallengesAsync(); ChallengeList.ItemsSource = list; EmptyText.Visibility = list.Count == 0 ? Visibility.Visible : Visibility.Collapsed; if (_selected == null) { SolvePanel.Visibility = Visibility.Collapsed; } } private void ChallengeList_SelectionChanged(object sender, SelectionChangedEventArgs e) { _selected = ChallengeList.SelectedItem as ChallengeItem; if (_selected == null) { SolvePanel.Visibility = Visibility.Collapsed; return; } SolvePanel.Visibility = Visibility.Visible; SolveHint.Text = _selected.Kind switch { "qr" => "扫码挑战(请在对应平台 App 扫码;完成后点击「我已确认」提交)", _ => "输入收到的验证码 / 确认值", }; // 二维码展示:qrUrl 为 http URL 或 file:// 本地路径 var qr = !string.IsNullOrEmpty(_selected.QrUrl) ? _selected.QrUrl : _selected.QrToken; if (!string.IsNullOrEmpty(qr) && (qr.StartsWith("http") || qr.StartsWith("file://"))) { QrImage.Source = new Microsoft.UI.Xaml.Media.Imaging.BitmapImage(new Uri(qr)); QrImage.Visibility = Visibility.Visible; } else { QrImage.Visibility = Visibility.Collapsed; } ValueBox.Text = ""; SolveResult.Text = ""; } private async void Solve_Click(object sender, RoutedEventArgs e) { if (_selected == null) return; var value = ValueBox.Text.Trim(); if (_selected.Kind == "qr" && string.IsNullOrEmpty(value)) { value = "confirmed"; } if (string.IsNullOrEmpty(value)) { SolveResult.Text = "请输入验证码"; return; } SolveButton.IsEnabled = false; var (ok, message) = await _ctx.Core.SolveAsync(_selected.ChallengeId, value); SolveButton.IsEnabled = true; SolveResult.Text = ok ? "已提交,等待服务器确认" : message; if (ok) { _selected = null; await RefreshAsync(); } } }