WdPassword.xaml.cs 6.63 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
using System.ComponentModel;
using FLY.Thick.Base.IService;
潘栩锋's avatar
潘栩锋 committed
15
using Unity;
潘栩锋's avatar
潘栩锋 committed
16

潘栩锋's avatar
潘栩锋 committed
17
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
18 19 20 21
{
    /// <summary>
    /// Window_Password.xaml 的交互逻辑
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
22
    public partial class WdPassword : FLY.ControlLibrary.WindowBigClose,INotifyPropertyChanged
潘栩锋's avatar
潘栩锋 committed
23
    {
潘栩锋's avatar
潘栩锋 committed
24
        PasswordAuthorize passwordAuthorize;
潘栩锋's avatar
潘栩锋 committed
25 26
        static PasswordKeep mPwKeep = new PasswordKeep();

潘栩锋's avatar
潘栩锋 committed
27
        public WdPassword()
潘栩锋's avatar
潘栩锋 committed
28 29 30
        {
            InitializeComponent();
        }
潘栩锋's avatar
潘栩锋 committed
31 32 33 34 35 36 37 38 39 40
        public void Init(PasswordAuthorize passwordAuthorize, int level) 
        {
            this.passwordAuthorize = passwordAuthorize;
            this.Level = level;
        }
        /// <summary>
        /// 密码级别
        /// </summary>
        int Level = 0;

41 42 43 44
        /// <summary>
        /// 输入密码,查询到的级别
        /// </summary>
        public int PwLv { get; private set; }
潘栩锋's avatar
潘栩锋 committed
45 46 47 48 49 50
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uiName"></param>
        /// <returns></returns>
        public static bool Authorize(string uiName) 
潘栩锋's avatar
潘栩锋 committed
51
        {
52 53 54 55 56 57 58 59 60 61 62 63
            return Authorize(uiName, out int pwLv);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="uiName">界面</param>
        /// <param name="pwLv">当密码正确时,密码的级别</param>
        /// <returns></returns>
        public static bool Authorize(string uiName, out int pwLv)
        {
            pwLv = 0;
潘栩锋's avatar
潘栩锋 committed
64 65 66 67 68 69 70
            //从容器获取
            var container = Application.Current.Properties["container"] as IUnityContainer;
            if (container == null)
                return true;

            var passwordAuthorize = container.Resolve<PasswordAuthorize>();
            int level = passwordAuthorize.GetLv(uiName);
71
            if (level <= 0)
潘栩锋's avatar
潘栩锋 committed
72
                return true;
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

            WdPassword w = new WdPassword();
            w.Init(passwordAuthorize, level);
            w.Owner = Application.Current.MainWindow;
            bool ret = (bool)w.ShowDialog();
            pwLv = w.PwLv;
            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="level">要求的级别</param>
        /// <param name="pwLv">当密码正确时,密码的级别</param>
        /// <returns></returns>
        public static bool Authorize(int level, out int pwLv)
        {
            pwLv = 0;
            //从容器获取
            var container = Application.Current.Properties["container"] as IUnityContainer;
            if (container == null)
                return true;

            var passwordAuthorize = container.Resolve<PasswordAuthorize>();
            if (level <= 0)
                return true;

潘栩锋's avatar
潘栩锋 committed
100
            WdPassword w = new WdPassword();
101
            w.Init(passwordAuthorize, level);
潘栩锋's avatar
潘栩锋 committed
102
            w.Owner = Application.Current.MainWindow;
103 104 105
            bool ret = (bool)w.ShowDialog();
            pwLv = w.PwLv;
            return ret;
潘栩锋's avatar
潘栩锋 committed
106 107 108
        }
        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
109
            if(string.IsNullOrEmpty(passwordbox.Password))
潘栩锋's avatar
潘栩锋 committed
110 111 112 113 114
            {
                FLY.ControlLibrary.Window_WarningTip.Show("错误", "密码为空",
                    TimeSpan.FromSeconds(2));
                return;
            }
潘栩锋's avatar
潘栩锋 committed
115 116
            

117 118
            AUTHORIZE_RESULT r = passwordAuthorize.Authorize(passwordbox.Password, Level, out int pwLv);
            PwLv = pwLv;
潘栩锋's avatar
潘栩锋 committed
119 120 121
            switch (r) 
            {
                case AUTHORIZE_RESULT.OK:
潘栩锋's avatar
潘栩锋 committed
122
                    {
潘栩锋's avatar
潘栩锋 committed
123
                        mPwKeep.SetPSK(passwordbox.Password);
潘栩锋's avatar
潘栩锋 committed
124 125 126 127
                        this.DialogResult = true;
                        this.Close();
                        return;
                    }
潘栩锋's avatar
潘栩锋 committed
128
                case AUTHORIZE_RESULT.ERR_LEVEL:
潘栩锋's avatar
潘栩锋 committed
129 130 131 132 133
                    {
                        FLY.ControlLibrary.Window_WarningTip.Show("错误", $"权限不足 需要Lv{Level}以上密码",
                            TimeSpan.FromSeconds(2));
                        return;
                    }
潘栩锋's avatar
潘栩锋 committed
134
                case AUTHORIZE_RESULT.ERR_PW:
潘栩锋's avatar
潘栩锋 committed
135 136 137 138 139
                    {
                        FLY.ControlLibrary.Window_WarningTip.Show("错误", "密码错误",
                            TimeSpan.FromSeconds(2));
                        return;
                    }
潘栩锋's avatar
潘栩锋 committed
140 141 142 143
            }
            return;
        }

潘栩锋's avatar
潘栩锋 committed
144 145 146 147 148 149 150 151 152 153 154 155
        private void ButtonOSK_Click(object sender, RoutedEventArgs e)
        {
            TextBox tbPw = new TextBox
            {
                Text = passwordbox.Password,
                Tag = "full password"
            };
            tbPw.TextChanged += (s, _e) =>
            {
                var textbox = s as TextBox;
                passwordbox.Password = textbox.Text;
            };
156
            FLY.ControlLibrary.UI.OSK.KeyboardBehavior.PopPad(tbPw, this);
潘栩锋's avatar
潘栩锋 committed
157
        }
潘栩锋's avatar
潘栩锋 committed
158 159
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
160 161
            //从容器获取
            //this.DataContext = this;
潘栩锋's avatar
潘栩锋 committed
162
            togglebutton_keep5min.DataContext = mPwKeep;
潘栩锋's avatar
潘栩锋 committed
163
            if (mPwKeep.GetPSK(out string psk))
潘栩锋's avatar
潘栩锋 committed
164
            {
潘栩锋's avatar
潘栩锋 committed
165
                passwordbox.Password = psk;
潘栩锋's avatar
潘栩锋 committed
166 167 168 169 170 171 172 173 174 175 176 177
            }
        }
        #region INotifyPropertyChanged 成员
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }

    /// <summary>
    /// 上一次的密码保持5分钟
    /// </summary>
    public class PasswordKeep:INotifyPropertyChanged
    {
潘栩锋's avatar
潘栩锋 committed
178 179 180 181 182 183

        /// <summary>
        /// 密码保存5分钟
        /// </summary>
        public bool Keep5Min { get; set; } = true;
        
潘栩锋's avatar
潘栩锋 committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        string PSK;
        /// <summary>
        /// 密码输入时间
        /// </summary>
        DateTime Time = DateTime.MinValue;
        /// <summary>
        /// 复位
        /// </summary>
        public void Reset()
        {
            Time = DateTime.MinValue;
        }
        public bool GetPSK(out string psk) 
        {
            psk = null;
            if (Keep5Min == false)
                return false;

            if ((DateTime.Now - Time) > TimeSpan.FromMinutes(5)) //5分钟内
                return false;
            
            psk = PSK;
            return true;
        }
        public void SetPSK(string psk)
        {
            PSK = psk;
            Time = DateTime.Now;
        }

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
}