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
72
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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;
using Unity;
namespace FLY.Thick.Base.UI
{
/// <summary>
/// Window_Password.xaml 的交互逻辑
/// </summary>
public partial class WdPassword : FLY.ControlLibrary.WindowBigClose,INotifyPropertyChanged
{
PasswordAuthorize passwordAuthorize;
static PasswordKeep mPwKeep = new PasswordKeep();
public WdPassword()
{
InitializeComponent();
}
public void Init(PasswordAuthorize passwordAuthorize, int level, string msg=null)
{
this.passwordAuthorize = passwordAuthorize;
this.Level = level;
this.Msg = msg;
}
string Msg = null;
/// <summary>
/// 密码级别
/// </summary>
int Level = 0;
/// <summary>
/// 输入密码,查询到的级别
/// </summary>
public int PwLv { get; private set; }
/// <summary>
///
/// </summary>
/// <param name="uiName"></param>
/// <returns></returns>
public static bool Authorize(string uiName)
{
return Authorize(uiName, out int pwLv);
}
/// <summary>
///
/// </summary>
/// <param name="uiName"></param>
/// <returns></returns>
public static bool Authorize(string uiName, string msg)
{
return Authorize(uiName, out int pwLv, msg);
}
/// <summary>
///
/// </summary>
/// <param name="uiName">界面</param>
/// <param name="pwLv">当密码正确时,密码的级别</param>
/// <returns></returns>
public static bool Authorize(string uiName, out int pwLv, string msg=null)
{
pwLv = 0;
//从容器获取
var container = Application.Current.Properties["container"] as IUnityContainer;
if (container == null)
return true;
var passwordAuthorize = container.Resolve<PasswordAuthorize>();
int level = passwordAuthorize.GetLv(uiName);
if (level <= 0)
return true;
WdPassword w = new WdPassword();
w.Init(passwordAuthorize, level, msg);
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;
WdPassword w = new WdPassword();
w.Init(passwordAuthorize, level);
w.Owner = Application.Current.MainWindow;
bool ret = (bool)w.ShowDialog();
pwLv = w.PwLv;
return ret;
}
private void button_apply_Click(object sender, RoutedEventArgs e)
{
if(string.IsNullOrEmpty(passwordbox.Password))
{
FLY.ControlLibrary.Window_WarningTip.Show("错误", "密码为空",
TimeSpan.FromSeconds(2));
return;
}
AUTHORIZE_RESULT r = passwordAuthorize.Authorize(passwordbox.Password, Level, out int pwLv, out string pwDesp);
PwLv = pwLv;
switch (r)
{
case AUTHORIZE_RESULT.OK:
{
mPwKeep.SetPSK(passwordbox.Password);
this.DialogResult = true;
this.Close();
return;
}
case AUTHORIZE_RESULT.ERR_LEVEL:
{
FLY.ControlLibrary.Window_WarningTip.Show("错误", $"{pwDesp}权限不足 需要Lv{Level}以上密码",
TimeSpan.FromSeconds(2));
return;
}
case AUTHORIZE_RESULT.ERR_PW:
{
FLY.ControlLibrary.Window_WarningTip.Show("错误", "密码错误",
TimeSpan.FromSeconds(2));
return;
}
}
return;
}
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;
};
FLY.ControlLibrary.UI.OSK.KeyboardBehavior.PopPad(tbPw, this);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//从容器获取
//this.DataContext = this;
togglebutton_keep5min.DataContext = mPwKeep;
if (string.IsNullOrEmpty(Msg))
{
tbMsg.Visibility = Visibility.Collapsed;
}
else {
tbMsg.Text = Msg;
}
if (mPwKeep.GetPSK(out string psk))
{
passwordbox.Password = psk;
}
}
#region INotifyPropertyChanged 成员
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
/// <summary>
/// 上一次的密码保持5分钟
/// </summary>
public class PasswordKeep:INotifyPropertyChanged
{
/// <summary>
/// 密码保存5分钟
/// </summary>
public bool Keep5Min { get; set; } = true;
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
}
}