Winform開發(fā)框架之系統(tǒng)登錄實(shí)現(xiàn)
在業(yè)務(wù)系統(tǒng)的操作過程中,有時候,用戶需要切換用戶進(jìn)行重新登錄,這種情況有時候是因?yàn)橐粋€人管理多個用戶賬號,希望通過不同的賬號登錄進(jìn)行管理不同的資料,另一種情況是酒店的換班操作,另一個人接替前面的人進(jìn)行系統(tǒng)維護(hù)管理。這種重新登錄其實(shí)也是一種友好的操作之一,試想一下,換個賬號登錄,就需要推出系統(tǒng),重新尋找運(yùn)行程序才可以,而且如果系統(tǒng)啟動較慢一點(diǎn)的,還需要等待,所以實(shí)現(xiàn)重新登錄,有時候也是必要的。因此實(shí)現(xiàn)這個功能,也是體現(xiàn)我們開發(fā)的系統(tǒng)注重細(xì)節(jié)的表現(xiàn)。
另外,自動登錄(其實(shí)是接受通過命令行參數(shù)進(jìn)行登錄)也是很常見的,有時候,讓客戶端記住用戶的賬號密碼,我們在后臺通過調(diào)動命令行方式進(jìn)行登錄,讓系統(tǒng)程序接收到相關(guān)的參數(shù)值即可進(jìn)行登錄了。
1、系統(tǒng)重新登錄實(shí)現(xiàn)
大致的思路,就是登錄系統(tǒng)后,在系統(tǒng)菜單中有一項(xiàng)重新登錄的功能入口,單擊可以要求客戶重新輸入密碼進(jìn)行登錄,如下所示。
代碼實(shí)現(xiàn)就是通過把初始化的時候,用戶相關(guān)的操作放到一個函數(shù)里面,保證重新執(zhí)行這個函數(shù)操作就能重新刷新登錄用戶信息即可。如下所示。
在InitUserRelated函數(shù)里面,我們把用戶相關(guān)的初始化操作放在里面,其中包括顯示登錄用戶信息、用戶可操作按鈕或者菜單、首頁信息等相關(guān)項(xiàng)目,代碼如下所示。
- /// <summary>
- /// 初始化用戶相關(guān)的系統(tǒng)信息
- /// </summary>
- private void InitUserRelated()
- {
- ChildWinManagement.LoadMdiForm(this, typeof(FirstPage));//歡迎頁面
- #region 初始化系統(tǒng)名稱
- try
- {
- string Manufacturer = config.AppConfigGet("Manufacturer");
- string ApplicationName = config.AppConfigGet("ApplicationName");
- string AppWholeName = string.Format("{0}-{1} ", Manufacturer, ApplicationName);
- Portal.gc.gAppUnit = Manufacturer;
- Portal.gc.gAppMsgboxTitle = AppWholeName;
- Portal.gc.gAppWholeName = AppWholeName;
- this.Text = AppWholeName + " ";
- this.notifyIcon1.BalloonTipText = AppWholeName;
- this.notifyIcon1.BalloonTipTitle = AppWholeName;
- this.notifyIcon1.Text = AppWholeName;
- string userName = Portal.gc.LoginInfo.RealName;
- if (string.IsNullOrEmpty(userName))
- {
- userName = Portal.gc.LoginInfo.Name;
- }
- UserStatus = string.Format("當(dāng)前用戶:{0}({1})", userName, Portal.gc.RoleInfo.RoleName);
- CommandStatus = string.Format("歡迎使用 {0}", Portal.gc.gAppWholeName);
- }
- catch { }
- #endregion
- InitAuthorizedUI();//根據(jù)權(quán)限屏蔽
- InitSkinGallery();
- UserLookAndFeel.Default.SetSkinStyle("Office 2010 Blue");
- }
其中InitAuthorizedUI就是判斷用戶有哪些權(quán)限的函數(shù),根據(jù)權(quán)限系統(tǒng)獲取到的功能點(diǎn),在這里對界面元素進(jìn)行重新刷新,有權(quán)限的就顯示,沒有的就隱藏即可,如下所示。
- /// <summary>
- /// 根據(jù)權(quán)限屏蔽功能
- /// </summary>
- private void InitAuthorizedUI()
- {
- this.tool_Report.Enabled = Portal.gc.HasFunction("Report");
- this.tool_Dict.Enabled = Portal.gc.HasFunction("Dictionary");
- this.tool_ItemDetail.Enabled = Portal.gc.HasFunction("ItemDetail");
- this.tool_Purchase.Enabled = Portal.gc.HasFunction("Purchase");
- this.tool_StockSearch.Enabled = Portal.gc.HasFunction("StockSearch");
- this.tool_TakeOut.Enabled = Portal.gc.HasFunction("TakeOut");
- this.tool_WareHouse.Enabled = Portal.gc.HasFunction("WareHouse");
- //this.menu_run_systemLog.Enabled = Portal.gc.HasFunction("LoginLog");
- this.tool_Settings.Enabled = Portal.gc.HasFunction("Parameters");
- this.tool_MonthlyStatistic.Enabled = Portal.gc.HasFunction("MonthlyStatistic");
- this.tool_AnnualStatistic.Enabled = Portal.gc.HasFunction("AnnualStatistic");
- this.tool_ClearAll.Enabled = Portal.gc.HasFunction("ClearAllData");
- this.tool_ImportItemDetail.Enabled = Portal.gc.HasFunction("ImportItemDetail");
- }
這樣封裝好后,我們需要重新登錄就方便了,我們在重新登錄的菜單操作里面,實(shí)現(xiàn)代碼如下所示。
- private void btnRelogin_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
- {
- if (MessageDxUtil.ShowYesNoAndWarning("您確定需要重新登錄嗎?") != DialogResult.Yes)
- return;
- Portal.gc.MainDialog.Hide();
- Login dlg = new Login();
- dlg.StartPosition = FormStartPosition.CenterScreen;
- if (DialogResult.OK == dlg.ShowDialog())
- {
- if (dlg.bLogin)
- {
- CloseAllDocuments();
- InitUserRelated();
- }
- }
- dlg.Dispose();
- Portal.gc.MainDialog.Show();
- }
實(shí)現(xiàn)上面的操作過程,基本上就完成了重新登錄的操作了。
2、系統(tǒng)自動登錄實(shí)現(xiàn)
系統(tǒng)自動登錄有時候很必要,在用戶自己絕對信任的電腦上,自動登錄對用戶來說,很方便友好的,君不見,QQ如此、旺旺如此等等。其實(shí)實(shí)現(xiàn)思路就是通過給exe執(zhí)行文件傳遞登錄參數(shù)即可,必要時登錄的參數(shù)值還可以進(jìn)行加密,給第三方進(jìn)行運(yùn)行調(diào)用,以前就做過一個在Web上自動啟動桌面程序Visio應(yīng)用軟件的操作,其實(shí)原理就是一樣,通過傳遞參數(shù)給執(zhí)行文件實(shí)現(xiàn)的。
- [STAThread]
- static void Main(string[] args)
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- if (args.Length > 0)
- {
- LoginByArgs(args);
- }
- else
- {
- LoginNormal(args);
- }
- }
- /// <summary>
- /// 使用參數(shù)化登錄
- /// </summary>
- /// <param name="args"></param>
- private static void LoginByArgs(string[] args)
- {
- CommandArgs commandArgs = CommandLine.Parse(args);
- if (commandArgs.ArgPairs.Count > 0)
- {
- #region 獲取用戶參數(shù)
- string userName = string.Empty;
- string identity = string.Empty;
- foreach (KeyValuePair<string, string> pair in commandArgs.ArgPairs)
- {
- if ("U".Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
- {
- userName = pair.Value;
- }
- if ("P".Equals(pair.Key, StringComparison.OrdinalIgnoreCase))
- {
- identity = pair.Value;
- }
- }
- #endregion
- if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(identity))
- {
- bool bLogin = Portal.gc.LoginByIdentity(userName.Trim(), identity);
- if (bLogin)
- {
- ShowMainDialog();
- }
- else
- {
- LoginNormal(args);
- }
- }
- }
- }
有時候,即使覺得用戶不需要通過命令行登錄,那么我們自己為了避免開發(fā)過程中,啟動程序時候,總是需要輸入用戶賬號密碼的問題,也可以使用模擬自動登錄的方式解決。
我們只需要在項(xiàng)目的屬性里面輸入內(nèi)置的用戶名密碼,這樣我們測試起來就不用登錄那么麻煩了。
以上就是Winform開發(fā)框架中對于系統(tǒng)重新登錄以及系統(tǒng)自動登錄(命令行登錄)的思想思路及方式,歡迎大家提供更好的思路及技巧,或者進(jìn)行探討,謝謝支持。
原文鏈接:http://www.cnblogs.com/wuhuacong/archive/2012/08/21/2648339.html


2011-04-22 10:45:14




