/******************************
* 说明:主程序启动相关管理类
* 创建人:龚宇超
* 创建日期: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.Main4.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();
///
/// 记录当前U盾权限窗口
///
public static List DogVerifyModuleForms = new List();
///
/// 说明:程序启动入口
/// 创建人:龚宇超
/// 创建日期: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
{
string guid = Guid.NewGuid().ToString();
XtraTabPage tp = new XtraTabPage();
if (SystemInfo.Instance.IsDogVerify)
{
int keyHandle = ERPInfo.Instance.keyHandles[0];
int uPin1 = Convert.ToInt32("0x987F6BCD", 16);
int uPin2 = Convert.ToInt32("0xE193C5B2", 16);
int uPin3 = Convert.ToInt32("0xD507CC28", 16);
int uPin4 = Convert.ToInt32("0x4B125AF6", 16);
int rtn = SmartX1Api.SmartX1Open(keyHandle, uPin1, uPin2, uPin3, uPin4);
if (rtn == 0)
{
byte[] buffer = new byte[4096];
int nrtn = SmartX1Api.SmartX1ReadStorage(keyHandle, 0, 4096, buffer);
if (nrtn == 0)
{
string verifyStr = Encoding.Default.GetString(buffer);
string[] verifyArray = verifyStr.Replace("\0", "").Split(',');
if (!verifyArray.Contains(args[4].Trim()))
{
MessageUtil.Show("未拥有当前模块查看权限");
return;
}
else
{
DogVerifyModuleForms.Add(tp);
}
}
else
{
MessageUtil.Show("U盾数据获取失败,请重试或联系管理员");
return;
}
}
else
{
MessageUtil.Show("U盾验证失败,请重试或联系管理员");
return;
}
SmartX1Api.SmartX1Close(keyHandle);
}
IForm iform = FormHelper.LoadDllForm(module.DllName, args);
// 菜单打开的模块默认最大化,界面FormState设置为Normal模式,否则会出现界面不是全屏,会显示默认阴影界面
//iform.SubForm.WindowState = _frmMain.WindowState;
iform.SubForm.WindowState = FormWindowState.Normal; ;
Form form = iform.SubForm; // 表示组成应用程序的用户界面的窗口或对话框。
form.Tag = guid;
tp.Text = module.Name;
tp.Tag = guid;
// 这个必须有不然会提示:"不能向tabControl中添加顶级控件"
form.TopLevel = false;
//form.Location = new Point(0, 0);
form.Dock = DockStyle.Fill;//客户需要缩放
form.AutoSize = true;
//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;
//module.Tag = "1";
module.Tag = guid;
module.ModuleForm = form;
form.Show();
TabMain.TabPages.Add(tp);
TabMain.SelectedTabPage = tp;
ModuleForms.Add(guid, module);
//LastModuleForm.Add(module);
//Lskj.Main4.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
}
}