using System; using System.Data; using System.Windows.Forms; using SuperMap.RealEstate.FrameWork.Business; using SuperMap.RealEstate.Windows.Forms; using SuperMap.RealEstate.Utility; using SuperMap.RealEstate.Launcher_WinForm; namespace SuperMap.RealEstate.SecurityDevice.Manager { public partial class frmMain : FormModule { public frmMain(string ModuleGuid, string UserName = "", string Password = "") : base(ModuleGuid, UserName, Password) { InitializeComponent(); _ISecurityDevice.MessageChanged += new ClientSecurityDeviceMessageHandler(SecurityDeviceControl1_MessageChanged); } public frmMain(string ModuleGuid, string[] args) : base(ModuleGuid, args) { InitializeComponent(); _ISecurityDevice.MessageChanged += new ClientSecurityDeviceMessageHandler(SecurityDeviceControl1_MessageChanged); } bool MultiLine_Enanled = false; string MultiLine_Url = string.Empty; IClientSecurityDevice _ISecurityDevice = new UsbKeyLicenseDevice(); private void ButtonReset_Click(object sender, EventArgs e) { if (MessageBox.Show("您确认将数字证书恢复到出厂设置?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Cancel) { return; } richTextBox1.Clear(); if (_ISecurityDevice.Open()) { _ISecurityDevice.Reset(); _ISecurityDevice.Close(); } } private void frmMain_Load(object sender, EventArgs e) { LauncherHelper.SetMessageValue("加载 用户..."); BuildUserTreeView(); SecurityDeviceControl1.Device_Connect(); LauncherHelper.Close(1); } private void BuildUserTreeNode(string pid,TreeNodeCollection nodes, DataTable type, DataTable user) { DataRow[] _TypeDataRowCollection = type.Select("UserType_PID='" + pid + "'"); DataRow[] _UserDataRowCollection; TreeNode _TypeNode, _UserNode; foreach (DataRow _TypeDataRow in _TypeDataRowCollection) { _TypeNode = new TreeNode(); _TypeNode.Text = _TypeDataRow["UserType_Name"].ToString(); _TypeNode.ImageIndex = 0; _TypeNode.SelectedImageIndex = 0; nodes.Add(_TypeNode); _UserDataRowCollection = user.Select("UserType_ID='" + _TypeDataRow["UserType_ID"].ToString() + "'"); foreach (DataRow _UserDataRow in _UserDataRowCollection) { _UserNode = new TreeNode(); _UserNode.Text = _UserDataRow["User_Name"].ToString(); _UserNode.Tag = _UserDataRow["User_ID"].ToString(); _UserNode.ToolTipText = _UserDataRow["User_Passport"].ToString(); _UserNode.ImageIndex = 1; _UserNode.SelectedImageIndex = 1; _TypeNode.Nodes.Add(_UserNode); } BuildUserTreeNode(_TypeDataRow["UserType_ID"].ToString(), _TypeNode.Nodes, type, user); } } private void BuildUserTreeView() { UserType _UserType = new UserType(); DataTable _UserTypeDataTable = _UserType.ExecuteDataTable("select UserType_PID,UserType_ID,UserType_Name from T_UserType order by UserType_PID,UserType_Index,UserType_ID"); DataTable _UserDataTable = _UserType.ExecuteDataTable("select UserType_ID,User_ID,User_Name,User_Passport from T_User order by UserType_ID,User_Index,User_ID"); treeView1.Nodes.Clear(); BuildUserTreeNode("-1", treeView1.Nodes, _UserTypeDataTable, _UserDataTable); treeView1.ExpandAll(); } private string _SecurityDeviceWritePassWord = "SuperMap.RealEstate.SecurityDevices"; private void ButtonWrite_Click(object sender, EventArgs e) { if (MessageBox.Show("您确认将信息写入数字证书?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Cancel) { return; } richTextBox1.Clear(); if (_ISecurityDevice.Open()) { _ISecurityDevice.Reset(); ClientSecurityDeviceObject _SecurityDeviceObject = GetSecurityDeviceObject(); _ISecurityDevice.Write(_SecurityDeviceObject, _SecurityDeviceWritePassWord); _ISecurityDevice.Close(); SecurityDeviceControl1.SetValue(_SecurityDeviceObject); } } private void ButtonClose_Click(object sender, EventArgs e) { this.Close(); } private void SetSecurityDeviceObject(ClientSecurityDeviceObject value) { textBoxName.Text = (value == null) ? "" : value.Name; textBoxUser.Text = (value == null) ? "" : value.User; textBoxKey.Text = (value == null) ? "" : value.Key; textBoxDate.Text = (value == null) ? "" : value.Date; textBoxUrl.Text = (value == null) ? "" : value.Url; } private ClientSecurityDeviceObject GetSecurityDeviceObject() { ClientSecurityDeviceObject _SecurityDeviceInfo = new ClientSecurityDeviceObject(); _SecurityDeviceInfo.Name = textBoxName.Text; _SecurityDeviceInfo.User = textBoxUser.Text; //_SecurityDeviceInfo.Key = textBoxKey.Text; _SecurityDeviceInfo.Key = textBoxKey.Text.ToEncrypt(); _SecurityDeviceInfo.Date = textBoxDate.Text; _SecurityDeviceInfo.Url = textBoxUrl.Text; return _SecurityDeviceInfo; } private bool FindUser(string userPassport, TreeNodeCollection nodes) { foreach (TreeNode _Node in nodes) { if (_Node.ImageIndex == 0) { if (FindUser(userPassport, _Node.Nodes)) return true; else continue; } else if (_Node.ImageIndex == 1) { if (_Node.ToolTipText == userPassport) { treeView1.SelectedNode = _Node; return true; } } } return false; } private void SecurityDeviceControl1_DeviceChanged(object sender, ClientSecurityDeviceEventArgs e) { if (e.DeviceEnum == ClientSecurityDeviceState.Insert) { SetUI(true); if (MultiLine_Enanled) { } else { if (FindUser(e.KeyInfo.User, treeView1.Nodes)) { textBoxUrl.Text = e.KeyInfo.Url; } else { SetSecurityDeviceObject(null); ClientSecurityDeviceMessageEventArgs _ClientSecurityDeviceMessageEventArgs = new ClientSecurityDeviceMessageEventArgs(); _ClientSecurityDeviceMessageEventArgs.Message = "无法通过数字证书定位到用户。"; SecurityDeviceControl1_MessageChanged(SecurityDeviceControl1, _ClientSecurityDeviceMessageEventArgs); } } } else { SetUI(false); if (MultiLine_Enanled) { //_SecurityDeviceInfo = null; } } } private void SetUI(bool isFind) { ButtonReset.Enabled = isFind; buttonWrite.Enabled = isFind; } private void buttonWriteX_Click(object sender, EventArgs e) { MultiLine_Enanled = false; ClientSecurityDeviceObject _SecurityDeviceObject =GetSecurityDeviceObject(); MultiLine_Url = _SecurityDeviceObject.Url; if (String.IsNullOrEmpty(MultiLine_Url)) { if (MessageBox.Show("系统网址为空,插入数字证书将不自动弹出浏览器。", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Cancel) { return; } } //检查勾选的用户集合 MessageBox.Show("结合框架定义用户再考虑。", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); MultiLine_Enanled = true; } private void SecurityDeviceControl1_MessageChanged(object sender, ClientSecurityDeviceMessageEventArgs e) { if (richTextBox1.Lines.Length > 0) richTextBox1.AppendText("\r\n"); richTextBox1.AppendText("【" + DateTime.Now.ToLongTimeString() + "】" + e.Message); richTextBox1.ScrollToCaret(); Application.DoEvents(); } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { using (User _User = new User()) { TreeNodeSelected(e.Node, _User); } } private bool TreeNodeSelected(TreeNode node, User user) { if (node.ImageIndex == 0) return false; bool _result = false; if (user.Select(int.Parse(node.Tag.ToString()))) { //尚未启动数字证书 if (user.User_EnabledLicense.Value == 0) { if (ShowQuestion("用户" + user.User_Name + "未启用数字证书,是否立即启用?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { user.User_EnabledLicense = 1; if (user.User_Indefinit.Value == 1) { user.User_Expiry = DateTime.MaxValue; } else { //授权时间 if ((!user.User_Expiry.HasValue) || (user.User_Expiry.Value < DateTime.Now.Date)) { if (ShowQuestion("用户" + user.User_Name + "授权时间已到,是否为其授权一年?", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) { user.User_Expiry = DateTime.Now.Date.AddYears(1); } else { user.User_Expiry = DateTime.Parse(SecurityDeviceControl1.User_Expiry); } } } if (string.IsNullOrEmpty(user.User_License)) user.User_License = Guid.NewGuid().ToString().Replace("-", ""); _result = user.Update(); } } //已启动数字证书但key为空 else if (user.User_EnabledLicense.Value == 1 && string.IsNullOrEmpty(user.User_License)) { ShowQuestion("用户" + user.User_Name + "数字证书秘钥为空,请重新写入加密狗!", MessageBoxButtons.OK); user.User_License = Guid.NewGuid().ToString().Replace("-", ""); _result = user.Update(); } textBoxName.Text = user.User_Name; textBoxUser.Text = user.User_Passport; textBoxKey.Text = user.User_License; //用户类型 if (user.User_Indefinit.Value == 1) textBoxDate.Text = DateTime.MaxValue.ToShortDateString().Replace("/", "-"); else textBoxDate.Text = user.User_Expiry.Value.ToShortDateString().Replace("/", "-"); } else ShowQuestion("无法找到对应的用户信息!", MessageBoxButtons.OK); return _result; } } }