MainWindow.xaml.cs 3.06 KB
using HHECS.Application.Licenses;
using HHECS.Infrastructure.CommonHelper;
using Newtonsoft.Json;
using System;
using System.Windows;

namespace HHECS.LicenseTools
{
    public partial class MainWindow
    {
        public string MK { get; set; } = "areyouok";

        readonly LicenseModel licenseModel = new();

        public MainWindow()
        {
            InitializeComponent();
            Init();
            this.DataContext = licenseModel;
        }

        private void Init()
        {
            var dict = EnumHelper.EnumListDic<LicenseType>();
            cbx_Role.ItemsSource = dict;
            cbx_Role.DisplayMemberPath = "Key";
            cbx_Role.SelectedValuePath = "Value";
            cbx_Role.SelectedIndex = 0;

            dt_ExpireTime.SelectedDateTime = DateTime.Now.AddYears(1);
        }

        private void btn_ClearData_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            txt_MachineCode.Text = "";
            txt_Custom.Text = "";
            cbx_Role.SelectedIndex = 0;
            dt_ExpireTime.SelectedDateTime = DateTime.Now.AddYears(1);
        }

        private void btn_ClearLicense_Click(object sender, RoutedEventArgs e)
        {
            txt_License.Text = "";
        }

        private void BtnCreate_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            licenseModel.LastUseTime = DateTime.Now;
            licenseModel.ExpireTime = (DateTime)dt_ExpireTime.SelectedDateTime;
            licenseModel.CustomMachineCode = txt_MachineCode.Text;
            switch ((int)cbx_Role.SelectedValue)
            {
                case 0: licenseModel.CustomRole = LicenseType.Trial; break;
                case 1: licenseModel.CustomRole = LicenseType.Free; break;
                default: licenseModel.CustomRole = LicenseType.Trial; break;
            }
            licenseModel.CustomName = txt_Custom.Text;

            var result = EncodeHelper.AES(JsonConvert.SerializeObject(licenseModel), "areyouok");
            if (string.IsNullOrWhiteSpace(result))
            {
                MessageBox.Show($"生成失败");
                return;
            }
            txt_License.Text = result;
        }

        private void btn_Check_Click(object sender, RoutedEventArgs e)
        {
            var result = EncodeHelper.AESDecrypt(txt_License.Text, "areyouok");
            if (string.IsNullOrWhiteSpace(result))
            {
                MessageBox.Show($"验证失败:{result}");
                return;
            }
            LicenseModel model = JsonConvert.DeserializeObject<LicenseModel>(result);
            txt_MachineCode.Text = model.CustomMachineCode;
            txt_Custom.Text = model.CustomName;
            cbx_Role.SelectedValue = model.CustomRole.GetIndexInt();
            dt_ExpireTime.SelectedDateTime = model.ExpireTime;
        }

        private void btn_Get_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Clipboard.SetDataObject(txt_License.Text);
            }
            catch (Exception)
            {
            }
        }
    }
}