兼容win7版应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.1 KiB

using System.Threading;
using AIK.Common.Enum;
using AIK.Common.IconString;
using AIK.Common.Interface;
using AIK.Service.IService;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AIK.Views.UIViewModels
{
public partial class QRCodeMessageViewModel : ObservableObject
{
private readonly CancellationTokenSource _cts = new();
public CancellationToken CancellationToken => _cts.Token;
[ObservableProperty]
private string _title = "请稍候";
[ObservableProperty]
private string _message = "处理中...";
[ObservableProperty]
private MessageTypeEnum _messageType;
[ObservableProperty]
private string _iconPath = "\ue601";
[ObservableProperty]
private string _iconForeground = "white";
[ObservableProperty]
private bool _isCancelButtonVisible = true;
private readonly IConfigurationService _configurationService;
private readonly IQRCodeService _qrCodeService;
[ObservableProperty]
private byte[]? _qrCodeBytes; // 👈 只暴露字节数组
//国内版
private string defaultLogoPath = "AIK.Assets.Images.AIKChina.png";
public QRCodeMessageViewModel(IConfigurationService configurationService, IQRCodeService qRCodeService)
{
_configurationService = configurationService;
_qrCodeService = qRCodeService;
}
public static QRCodeMessageViewModel Create(
IServiceProvider serviceProvider,
string title = "请稍候",
string message = "处理中...",
MessageTypeEnum typeEnum = MessageTypeEnum.Info,
bool showCancel = true)
{
var viewModel = ActivatorUtilities.CreateInstance<QRCodeMessageViewModel>(serviceProvider);
// 2. 设置业务参数
viewModel.Title = title;
viewModel.Message = message;
viewModel.IsCancelButtonVisible = showCancel;
viewModel.MessageType = typeEnum;
// 3. 根据类型设置图标和颜色
var (icon, color) = Common.IconString.MessageStyle.GetByType(typeEnum);
viewModel.IconPath = icon;
viewModel.IconForeground = color;
// 4. 根据编译指令设置 Logo 路径
#if CN
viewModel.defaultLogoPath = "AIK.Assets.Images.AIKChina.png";
#elif TEST
viewModel.defaultLogoPath = "AIK.Assets.Images.AIKChina.png";
#else
viewModel.defaultLogoPath = "AIK.Assets.Images.AIKInternationl.png";
#endif
// 5. 加载二维码
viewModel.LoadQrCodeImage();
return viewModel;
}
private void LoadQrCodeImage()
{
QrCodeBytes = _qrCodeService.GenerateQRCodeWithLogo(_configurationService.ApiSettings.QRCodeUrl, defaultLogoPath, 12);
}
[RelayCommand]
private void Cancel()
{
_cts.Cancel();
}
}
}