PgPwManager.xaml.cs 4.85 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2
using CommunityToolkit.Mvvm.Input;
using FLY.Thick.Base.Common;
3
using System;
潘栩锋's avatar
潘栩锋 committed
4 5 6
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
7
using System.Windows;
潘栩锋's avatar
潘栩锋 committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
using System.Windows.Controls;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// PgPwManager.xaml 的交互逻辑
    /// </summary>
    public partial class PgPwManager : Page
    {
        PgPwManagerVm viewModel;
        public PgPwManager()
        {
            InitializeComponent();

        }
        [Unity.InjectionMethod]
潘栩锋's avatar
潘栩锋 committed
24
        public void Init(PasswordAuthorize passwordAuthorize)
潘栩锋's avatar
潘栩锋 committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38
        {
            viewModel = new PgPwManagerVm();
            viewModel.Init(passwordAuthorize);
            this.DataContext = viewModel;
        }
    }
    public class PgPwManagerVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<PasswordCell> Pws { get; } = new ObservableCollection<PasswordCell>();
        public ObservableCollection<UiLvCell> UiLvs { get; } = new ObservableCollection<UiLvCell>();
        public RelayCommand ApplyCmd { get; }

        PasswordAuthorize passwordAuthorize;
潘栩锋's avatar
潘栩锋 committed
39
        public PgPwManagerVm()
潘栩锋's avatar
潘栩锋 committed
40 41 42 43 44 45 46 47 48 49 50 51
        {
            ApplyCmd = new RelayCommand(Apply);
        }


        public void Init(PasswordAuthorize passwordAuthorize)
        {
            this.passwordAuthorize = passwordAuthorize;
            this.passwordAuthorize.PropertyChanged += PasswordAuthorize_PropertyChanged;
            update_list();

        }
潘栩锋's avatar
潘栩锋 committed
52
        void update_list()
潘栩锋's avatar
潘栩锋 committed
53 54 55 56 57
        {
            Pws.Clear();
            UiLvs.Clear();
            if (this.passwordAuthorize.Db.Pws != null)
            {
潘栩锋's avatar
潘栩锋 committed
58

潘栩锋's avatar
潘栩锋 committed
59 60
                foreach (var c in this.passwordAuthorize.Db.Pws)
                {
61
                    Pws.Add(new PasswordCell() { Password = c.Password, Level = c.Level, Description = c.Description });
潘栩锋's avatar
潘栩锋 committed
62 63 64 65
                }
            }
            if (this.passwordAuthorize.Db.UiLvs != null)
            {
潘栩锋's avatar
潘栩锋 committed
66

潘栩锋's avatar
潘栩锋 committed
67 68 69 70 71 72 73 74
                foreach (var c in this.passwordAuthorize.Db.UiLvs)
                {
                    UiLvs.Add(new UiLvCell() { UiName = c.UiName, Description = c.Description, Level = c.Level });
                }
            }
        }
        private void PasswordAuthorize_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
75
            if (e.PropertyName == "Db")
潘栩锋's avatar
潘栩锋 committed
76 77 78 79 80 81 82 83 84 85
            {
                update_list();
            }
        }

        private void Apply()
        {
            //检测
            foreach (var pw in Pws)
            {
潘栩锋's avatar
潘栩锋 committed
86
                if (string.IsNullOrEmpty(pw.Password))
潘栩锋's avatar
潘栩锋 committed
87
                {
88 89 90
                    string tit = (string)Application.Current.TryFindResource("str.PgPwManager.FormatErr");
                    string msg = (string)Application.Current.TryFindResource("str.PgPwManager.PwIsNull");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, msg);
潘栩锋's avatar
潘栩锋 committed
91 92 93 94 95 96
                    return;
                }
                pw.Password = pw.Password.Trim();

                if (pw.Level < 0 || pw.Level > 10)
                {
97 98 99
                    string tit = (string)Application.Current.TryFindResource("str.PgPwManager.FormatErr");
                    string msg = (string)Application.Current.TryFindResource("str.PgPwManager.LvIsOverrange");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, msg);
潘栩锋's avatar
潘栩锋 committed
100 101 102
                    return;
                }

潘栩锋's avatar
潘栩锋 committed
103
                if ((from _pw in Pws where _pw.Password == pw.Password select _pw).Count() > 1)
潘栩锋's avatar
潘栩锋 committed
104
                {
105 106 107
                    string tit = (string)Application.Current.TryFindResource("str.PgPwManager.FormatErr");
                    string msg = (string)Application.Current.TryFindResource("str.PgPwManager.PwIsDuplication");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, msg);
潘栩锋's avatar
潘栩锋 committed
108 109 110
                    return;
                }
            }
潘栩锋's avatar
潘栩锋 committed
111
            foreach (var c in UiLvs)
潘栩锋's avatar
潘栩锋 committed
112 113 114
            {
                if (c.Level < 0 || c.Level > 10)
                {
115 116 117
                    string tit = (string)Application.Current.TryFindResource("str.PgPwManager.FormatErr");
                    string msg = (string)Application.Current.TryFindResource("str.PgPwManager.LvIsOverrange");
                    FLY.ControlLibrary.Window_WarningTip.Show(tit, msg);
潘栩锋's avatar
潘栩锋 committed
118 119 120
                    return;
                }
            }
121 122 123 124 125 126

            var p = new PasswordJsonDb();
            p.Pws.AddRange(Pws);
            p.UiLvs.AddRange(UiLvs);

            this.passwordAuthorize.Apply(p);
127 128 129 130 131 132
            {
                string tit = (string)Application.Current.TryFindResource("str.PgPwManager.ApplySuccessfully");
                string format = (string)Application.Current.TryFindResource("str.PgPwManager.TotalNPw");
                string msg = string.Format(format, Pws.Count());
                FLY.ControlLibrary.Window_Tip.Show(tit, msg, TimeSpan.FromSeconds(2));
            }
潘栩锋's avatar
潘栩锋 committed
133 134 135 136 137 138 139
            return;

        }

    }

}