PgPwManager.xaml.cs 4.46 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
using FLY.Thick.Base.Common;
using FLY.Thick.Base.IService;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

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

        }
        [Unity.InjectionMethod]
        public void Init(PasswordAuthorize passwordAuthorize) 
        {
            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;
        public PgPwManagerVm() 
        {
            ApplyCmd = new RelayCommand(Apply);
        }


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

        }
        void update_list() 
        {
            Pws.Clear();
            UiLvs.Clear();
            if (this.passwordAuthorize.Db.Pws != null)
            {
                
                foreach (var c in this.passwordAuthorize.Db.Pws)
                {
72
                    Pws.Add(new PasswordCell() { Password = c.Password, Level = c.Level, Description = c.Description });
潘栩锋's avatar
潘栩锋 committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
                }
            }
            if (this.passwordAuthorize.Db.UiLvs != null)
            {
                
                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)
        {
            if (e.PropertyName == "Db") 
            {
                update_list();
            }
        }

        private void Apply()
        {
            //检测
            foreach (var pw in Pws)
            {
                if (string.IsNullOrEmpty(pw.Password)) 
                {
                    FLY.ControlLibrary.Window_WarningTip.Show(
                        "格式错误",
                        $"不能有空密码");
                    return;
                }
                pw.Password = pw.Password.Trim();

                if (pw.Level < 0 || pw.Level > 10)
                {
                    FLY.ControlLibrary.Window_WarningTip.Show(
                        "格式错误",
                        $"{pw.Password} 级别 应该在 0-10 之间");
                    return;
                }

                if ((from _pw in Pws where _pw.Password == pw.Password select _pw).Count() > 1) 
                {
                    FLY.ControlLibrary.Window_WarningTip.Show(
                    "格式错误",
                    $"{pw.Password} 重复");
                    return;
                }
            }
122
            foreach(var c in UiLvs)
潘栩锋's avatar
潘栩锋 committed
123 124 125 126 127 128 129 130 131
            {
                if (c.Level < 0 || c.Level > 10)
                {
                    FLY.ControlLibrary.Window_WarningTip.Show(
                        "格式错误",
                        $"{c.UiName} 级别 应该在 0-10 之间");
                    return;
                }
            }
132 133 134 135 136 137

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

            this.passwordAuthorize.Apply(p);
潘栩锋's avatar
潘栩锋 committed
138 139 140 141 142 143 144 145 146 147 148

            FLY.ControlLibrary.Window_Tip.ShowShortTime(
                "修改成功",
                $"一共 {Pws.Count()}个密码 保存成功");
            return;

        }

    }

}