/****************************** * 说明:主程序启动相关管理类 * 创建人:龚宇超 * 创建日期:2017-08-09 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using Lskj.Control.Model; using Lskj.Core; using DevExpress.XtraEditors; using Lskj.Control; using Lskj.Data; using System.Threading; using Lskj.Business; using System.Data; using Lskj.Model; using DevExpress.XtraTab; using Lskj.Util; using DevExpress.Utils; using System.Diagnostics; using Lskj.Business.Impl; using System.IO; using System.Drawing; using System.Runtime.InteropServices; namespace Lskj.Main3.Model { /// /// 主程序启动相关管理类 /// public sealed class Manager { /// /// 主程序 /// private static FrmMain _frmMain = null; public static XtraTabControl TabMain = null; public static int MinusTheHeight = 0; public static int frmHeight = 0; public static string BsName; public static string BsUrl; /// /// 记录当前打开的窗口 /// public static Dictionary ModuleForms = new Dictionary(); /// /// 说明:程序启动入口 /// 创建人:龚宇超 /// 创建日期:2017-08-09 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// public static void StartForm() { bool isStart = true; // 加载Splash动画界面 SplashForm.ShowForm(); //CheckLocalConfig(); // C#数据库连接检查 if (!DBConfig.Instance.CreateConnection(DBConfig.Instance.Connection)) { MessageUtil.Show(ResourceKeys.UnConnectServer); SplashForm.HideForm(); DialogResult result = RunConfigForm(); isStart = result == DialogResult.OK; } if (isStart) { // Delphi数据库连接检查 if (DelphiHelper.Delphi_Init(new StringBuilder(DBConfig.Instance.GetDelphiConnection(DBConfig.Instance.dephiConnection))) == 0) { MessageUtil.Show(ResourceKeys.UnConnectServer + "[By Delphi]"); SplashForm.HideForm(); RunConfigForm(); return; } SplashForm.HideForm(); // 汉化dev控件 BaseResources.Localization(LocalizationType.CHS); // 启动登录页面 DialogResult result = RunLoginForm(); //登陆正确方可继续下一步 if (ERPInfo.Instance.LoginResult == false) return; if (result == DialogResult.OK) { DataTable table = MainImpl.EnableSubSystem(); ERPInfo.Instance.SubMenuCount = table.Rows.Count; if (table.Rows.Count == 1) { ERPInfo.Instance.SubSysId = table.Rows[0]["SubSysId"] + ""; ERPInfo.Instance.SubSysName = table.Rows[0]["SubSysName"] + ""; } ReStartMain(); } } else { SplashForm.HideForm(); } } /// /// 说明:重新启动 /// 创建人:龚宇超 /// 创建日期:2017-08-14 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 是否跳过子系统选择(适用于切换用户). public static void ReStartMain(bool skipMenu = false) { if (_frmMain != null) { _frmMain.Close(); _frmMain.Dispose(); _frmMain = null; } bool isStart = true; bool isOpenBs = false; DialogResult result; if (ERPInfo.Instance.SubMenuCount > 1 && !skipMenu) { // 进入子系统界面 result = RunSubSystemForm(); isStart = result == DialogResult.OK; isOpenBs = result == DialogResult.Yes; } if (isStart) { // 直接进入主界面 RunMainForm(); if (ERPInfo.Instance.SubMenuCount > 1) { ReStartMain(); } else { ExitSystem(); } } else if (isOpenBs) { string ReBsUrl = BsUrl.Replace("{userCode}", ERPInfo.Instance.LoginAccount); ReBsUrl = ReBsUrl.Replace("{password}", ERPInfo.Instance.Password); string url = ReplaceHelper.ReplaceUserInfo(ReBsUrl); // 固定传入参数(窗口标题、操作员ID、操作员名称、权限、模版编号) string[] defaultArgs = string.Format(ModuleArgs.DefaultArgs, BsName, ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, "", "").Split('~'); string[] menuArgs = new string[] { "", " ", url }; string[] args = defaultArgs.Concat(menuArgs).ToArray(); IForm form = FormHelper.LoadDllForm("Lskj.PubBrower2.dll", args); form.SubForm.WindowState = FormWindowState.Maximized; form.SubForm.ShowDialog(); if (ERPInfo.Instance.SubMenuCount > 1) { ReStartMain(); } else { ExitSystem(); } } else { ExitSystem(); } } /// /// 说明:打开模块 /// 创建人:龚宇超 /// 创建日期:2017-08-18 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The menu identifier. /// Name of the menu. /// The model flag. /// The URL parameters. public static void OpenModule(string menuId, string menuName, string dllName, string modelFlag, string urlParams) { try { // 验证参数 if (string.IsNullOrWhiteSpace(menuId) || string.IsNullOrWhiteSpace(dllName)) { MessageUtil.Show(ResourceKeys.NotFoundMenu); return; } // 验证权限 int purview = MainImpl.GetUserPurviewsByMenuId(menuId); if (purview == 0) { MessageUtil.Show(ResourceKeys.UnPurview); return; } //LogUtil.WriteDebug(menuId.ToString(), "操作模块", menuName, menuName); LogUtil.WriteDebug(modelFlag.ToString(), "操作模块", menuName, menuName, menuId.ToString()); DllModule module = new DllModule { DllName = ReplaceDllFileName(dllName), Id = menuId, Name = menuName }; string[] args = new string[]{ menuName, ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, purview + "", urlParams, menuId }; dllName = MainImpl.GetDefaultValue(dllName); if (SystemInfo.Instance.SoftOpenMode) { foreach (XtraTabPage tablepage in TabMain.TabPages) { if (tablepage.Text == menuName) { MessageUtil.Show("已设置不能打开多个相同模块!"); TabMain.SelectedTabPage = tablepage; return; } } } if (dllName.ToLower().Contains(".exe")) { WinHelper.WinExec(dllName); } else if (dllName.ToLower().IndexOf("http:", StringComparison.OrdinalIgnoreCase) != -1 || dllName.ToLower().IndexOf("www.", StringComparison.OrdinalIgnoreCase) != -1 || dllName.ToLower().IndexOf("ftp:", StringComparison.OrdinalIgnoreCase) != -1) { Process.Start(dllName); } else { if (dllName.ToLower().EndsWith(".lsp")) { //MessageBox.Show(PubUtil.AbsolutelyLibPath + dllName+" "+ menuName+" "+purview+" "+urlParams); // 打开delphi程序 DelphiHelper.LoadDelphiDll(PubUtil.AbsolutelyLibPath + dllName, "", menuName, "", "", purview, urlParams); } else { // 打开module程序 AddModuleToTab(module, args); } } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } /// /// 说明:添加模块到tab中 /// 创建人:龚宇超 /// 创建日期:2017-08-16 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The tab main. /// The module. /// The arguments. public static void AddModuleToTab(DllModule module, string[] args) { try { IForm iform = FormHelper.LoadDllForm(module.DllName, args); // 菜单打开的模块默认最大化,界面FormState设置为Normal模式,否则会出现界面不是全屏,会显示默认阴影界面 iform.SubForm.WindowState = _frmMain.WindowState; Form form = iform.SubForm; // 表示组成应用程序的用户界面的窗口或对话框。 string guid = Guid.NewGuid().ToString(); XtraTabPage tp = new XtraTabPage(); tp.Text = module.Name; tp.Tag = guid; // 这个必须有不然会提示:"不能向tabControl中添加顶级控件" form.TopLevel = false; form.Location = new Point(0, 0); //form.Dock = DockStyle.Fill; form.Width = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width; form.Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - MinusTheHeight; form.FormBorderStyle = FormBorderStyle.None; tp.AutoScroll = true; tp.Controls.Add(form); tp.ShowCloseButton = DefaultBoolean.True; tp.Dock = DockStyle.Fill; TabMain.TabPages.Add(tp); TabMain.SelectedTabPage = tp; module.Tag = "1"; module.ModuleForm = form; form.Show(); ModuleForms.Add(guid, module); //LastModuleForm.Add(module); //Lskj.Main3.Model.AutoSizeChange.ControllInitializeSize(form);//调整控件大小 } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(ex.InnerException != null ? ex.InnerException.Message : ex.Message); LogHelper.Instance.WriteError(ex); //LogUtil.WriteError("打开" + module.Name + "模块失败!", ex); } } /// /// 说明:特殊dll模块处理 /// 创建人:龚宇超 /// 创建日期:2018-04-20 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Name of the DLL. /// System.String. private static string ReplaceDllFileName(string dllName) { switch (dllName.ToLower().Trim()) { case "pubspec.dll": dllName = "Lskj.PubSpec.dll"; break; } return dllName; } private static void ExitSystem() { try { // 退出系统 if (DBConfig.Instance.NoticeExit) { Process[] p1 = Process.GetProcessesByName("Ls_Notice"); foreach (Process item in p1) item.Kill(); } } catch (Exception) { } } /// /// 说明:启动配置窗口 /// 创建人:龚宇超 /// 创建日期:2017-08-09 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private static DialogResult RunConfigForm() { FrmConfigSet frmConfigSet = new FrmConfigSet(); return frmConfigSet.ShowDialog(); } /// /// 说明:打开登录界面 /// 创建人:龚宇超 /// 创建日期:2017-08-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// DialogResult. private static DialogResult RunLoginForm() { FrmLogin frmLogin = new FrmLogin(); return frmLogin.ShowDialog(); } /// /// 说明:打开子系统 /// 创建人:龚宇超 /// 创建日期:2017-08-14 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.Windows.Forms.DialogResult. private static DialogResult RunSubSystemForm() { FrmSubSystem subSystem = new FrmSubSystem(); DialogResult dialogResult = subSystem.ShowDialog(); BsName = subSystem.BsName; BsUrl = subSystem.BsUrl; return dialogResult; } /// /// 说明:打开主程序 /// 创建人:龚宇超 /// 创建日期:2017-08-14 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// DialogResult. private static DialogResult RunMainForm() { _frmMain = new FrmMain(); return _frmMain.ShowDialog(); } #region 初始化服务器直接连接 /// /// 说明:初始化服务器直接连接 /// 创建人:龚宇超 /// 创建日期:2017-08-14 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// DialogResult. private static void CheckLocalConfig() { try { string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SystemResources.ini"); if (File.Exists(file)) { string configFlag = IniHelper.Read("SystemResources.ini", "FrmConfigFlag"); if (configFlag.Equals("1")) { string lastIPPort = string.Format("{0}:{1}", IniHelper.Read("SystemResources.ini", "LocalLastIP"), IniHelper.Read("SystemResources.ini", "LocalLastPort")); string downloadAESStrKey = string.Format("DownloadAESStr_{0}", lastIPPort); string[] downloadAESStrValue = IniHelper.Read("SystemResources.ini", downloadAESStrKey).Split('^'); string downloadAESStr = downloadAESStrValue.Length == 2 ? downloadAESStrValue[1] : ""; if (!string.IsNullOrEmpty(downloadAESStr)) { string args = AESUtil.Decrypt(downloadAESStr); string[] controlAfgs = args.Split('^'); if (controlAfgs != null && controlAfgs.Length == 5) { DBConfig.Instance.ServerName = controlAfgs[0]; DBConfig.Instance.DataBase = controlAfgs[1]; string Connection = "Server={0};Database={1};Persist Security Info=True;User ID=" + controlAfgs[2] + ";Password=" + controlAfgs[3] + ";Connection Timeout=5;MultipleActiveResultSets=true"; string dephistr = "Provider=SQLOLEDB.1;Server={0};Database={1};Persist Security Info=True;User ID=" + controlAfgs[2] + ";Password=" + controlAfgs[3] + ";Connection Timeout=5"; Connection = AESUtil.Encrypt(Connection); dephistr = AESUtil.Encrypt(dephistr); DBConfig.Instance.dephiConnection = dephistr; if (DBConfig.Instance.CreateConnection(Connection)) { DBConfig.Instance.Connection = Connection; } } } } } } catch (Exception) { } } #endregion } }