WdPassword.xaml.cs 7.76 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();
        }
31
        public void Init(PasswordAuthorize passwordAuthorize, int level, string msg=null) 
潘栩锋's avatar
潘栩锋 committed
32 33 34
        {
            this.passwordAuthorize = passwordAuthorize;
            this.Level = level;
35
            this.Msg = msg;
潘栩锋's avatar
潘栩锋 committed
36
        }
37
        string Msg = null;
潘栩锋's avatar
潘栩锋 committed
38 39 40 41 42
        /// <summary>
        /// 密码级别
        /// </summary>
        int Level = 0;

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

57 58 59 60 61 62 63 64 65 66 67
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uiName"></param>
        /// <returns></returns>
        public static bool Authorize(string uiName, string msg)
        {
            return Authorize(uiName, out int pwLv, msg);
        }


68 69 70 71 72 73
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uiName">界面</param>
        /// <param name="pwLv">当密码正确时,密码的级别</param>
        /// <returns></returns>
74
        public static bool Authorize(string uiName, out int pwLv, string msg=null)
75 76
        {
            pwLv = 0;
潘栩锋's avatar
潘栩锋 committed
77 78 79 80 81 82 83
            //从容器获取
            var container = Application.Current.Properties["container"] as IUnityContainer;
            if (container == null)
                return true;

            var passwordAuthorize = container.Resolve<PasswordAuthorize>();
            int level = passwordAuthorize.GetLv(uiName);
84
            if (level <= 0)
潘栩锋's avatar
潘栩锋 committed
85
                return true;
86 87

            WdPassword w = new WdPassword();
88
            w.Init(passwordAuthorize, level, msg);
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
            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
113
            WdPassword w = new WdPassword();
114
            w.Init(passwordAuthorize, level);
潘栩锋's avatar
潘栩锋 committed
115
            w.Owner = Application.Current.MainWindow;
116 117 118
            bool ret = (bool)w.ShowDialog();
            pwLv = w.PwLv;
            return ret;
潘栩锋's avatar
潘栩锋 committed
119 120 121
        }
        private void button_apply_Click(object sender, RoutedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
122
            if(string.IsNullOrEmpty(passwordbox.Password))
潘栩锋's avatar
潘栩锋 committed
123
            {
124 125 126
                string tit = (string)Application.Current.TryFindResource("str.WdPassword.OperateFailed");
                string msg = (string)Application.Current.TryFindResource("str.WdPassword.PasswordNull");
                FLY.ControlLibrary.Window_Tip.Show(tit, msg, TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
127 128
                return;
            }
潘栩锋's avatar
潘栩锋 committed
129 130
            

131
            AUTHORIZE_RESULT r = passwordAuthorize.Authorize(passwordbox.Password, Level, out int pwLv, out string pwDesp);
132
            PwLv = pwLv;
潘栩锋's avatar
潘栩锋 committed
133 134 135
            switch (r) 
            {
                case AUTHORIZE_RESULT.OK:
潘栩锋's avatar
潘栩锋 committed
136
                    {
潘栩锋's avatar
潘栩锋 committed
137
                        mPwKeep.SetPSK(passwordbox.Password);
潘栩锋's avatar
潘栩锋 committed
138 139 140 141
                        this.DialogResult = true;
                        this.Close();
                        return;
                    }
潘栩锋's avatar
潘栩锋 committed
142
                case AUTHORIZE_RESULT.ERR_LEVEL:
潘栩锋's avatar
潘栩锋 committed
143
                    {
144 145 146 147 148
                        string tit = (string)Application.Current.TryFindResource("str.WdPassword.OperateFailed");
                        string format = (string)Application.Current.TryFindResource("str.WdPassword.PermissionDenied");
                        string msg = string.Format(format, pwDesp, Level);

                        FLY.ControlLibrary.Window_Tip.Show(tit, msg, TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
149 150
                        return;
                    }
潘栩锋's avatar
潘栩锋 committed
151
                case AUTHORIZE_RESULT.ERR_PW:
潘栩锋's avatar
潘栩锋 committed
152
                    {
153 154 155
                        string tit = (string)Application.Current.TryFindResource("str.WdPassword.OperateFailed");
                        string msg = (string)Application.Current.TryFindResource("str.WdPassword.PasswordWrong");
                        FLY.ControlLibrary.Window_Tip.Show(tit, msg, TimeSpan.FromSeconds(2));
潘栩锋's avatar
潘栩锋 committed
156 157
                        return;
                    }
潘栩锋's avatar
潘栩锋 committed
158 159 160 161
            }
            return;
        }

潘栩锋's avatar
潘栩锋 committed
162 163 164 165 166 167 168 169 170 171 172 173
        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;
            };
174
            FLY.ControlLibrary.UI.OSK.KeyboardBehavior.PopPad(tbPw, this);
潘栩锋's avatar
潘栩锋 committed
175
        }
潘栩锋's avatar
潘栩锋 committed
176 177
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
潘栩锋's avatar
潘栩锋 committed
178 179
            //从容器获取
            //this.DataContext = this;
潘栩锋's avatar
潘栩锋 committed
180
            togglebutton_keep5min.DataContext = mPwKeep;
181 182 183 184 185 186 187 188 189

            if (string.IsNullOrEmpty(Msg))
            {
                tbMsg.Visibility = Visibility.Collapsed;
            }
            else {
                tbMsg.Text = Msg;
            }

潘栩锋's avatar
潘栩锋 committed
190
            if (mPwKeep.GetPSK(out string psk))
潘栩锋's avatar
潘栩锋 committed
191
            {
潘栩锋's avatar
潘栩锋 committed
192
                passwordbox.Password = psk;
潘栩锋's avatar
潘栩锋 committed
193 194 195 196 197 198 199 200 201 202 203 204
            }
        }
        #region INotifyPropertyChanged 成员
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }

    /// <summary>
    /// 上一次的密码保持5分钟
    /// </summary>
    public class PasswordKeep:INotifyPropertyChanged
    {
潘栩锋's avatar
潘栩锋 committed
205 206 207 208 209 210

        /// <summary>
        /// 密码保存5分钟
        /// </summary>
        public bool Keep5Min { get; set; } = true;
        
潘栩锋's avatar
潘栩锋 committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        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
    }
}