Esempio n. 1
0
        public void StartInit()
        {
            log = new Logger(Path.Combine(Directory.GetCurrentDirectory(), "Log"));
            /// <summary>
            /// 登录实例
            /// </summary>
            LoginForm _LoginForm = null;

            /// <summary>
            /// 获取调用实例
            /// </summary>
            CaijiCommHelper _CaijiCommHelper = null;

            /// <summary>
            /// 试验初始化配置信息
            /// </summary>
            SXCJModule _Module = null;

            try
            {
                _Module          = CalHelper.LoadModule();
                _CaijiCommHelper = new CaijiCommHelper(log, _Module);

                _LoginForm = new LoginForm(log, _CaijiCommHelper, _Module);
ReLogin:
                if (DialogResult.OK == _LoginForm.ShowDialog())
                {
                    if (_LoginForm.IsUpdate && !IsRuningProcess("JZUpgradeAgent"))
                    {
                        JZUpgrade.UpdateAlert ua = new JZUpgrade.UpdateAlert(2);
                        if (ua.ShowDialog() != DialogResult.OK)
                        {
                            goto ReLogin;
                        }

                        ProcessStartInfo Info = new ProcessStartInfo();
                        Info.CreateNoWindow  = false;
                        Info.UseShellExecute = true;
                        Info.FileName        = Path.Combine(Application.StartupPath, "JZUpgrade.exe");
                        Info.Arguments       = "\"5\"";
                        Process.Start(Info);
                        _LoginForm.Enabled = false;
                        _LoginForm.lblUpdateStatus.Text = "正在更新...";
                        goto ReLogin;
                    }

                    #region 用户登录
                    if (_LoginForm.bNetWorkIsConnected)
                    {
                        #region 在线登陆
                        ShowForm();
                        SetText("用户登录中.......");
                        try
                        {
                            string msg = _CaijiCommHelper.Login(_LoginForm.cmbUserName.Text, _LoginForm.txtUserPwd.Text, _Module.SpecialSetting.MachineCode, true);
                            if (msg.ToLower().Trim() != "true")
                            {
                                CloseFrom();
                                if (MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                                {
                                    goto ReLogin;
                                }
                                else
                                {
                                    Application.Exit();
                                    return;
                                }
                            }
                            else
                            {
                                #region 网络登录成功

                                _LoginForm.timerUpdate.Stop();

                                #region  步本地用户

                                SetText("同步本地用户.......");
                                _CaijiCommHelper.SysUserInfo(Yqun.Common.ContextCache.ApplicationContext.Current.UserName,
                                                             Yqun.Common.ContextCache.ApplicationContext.Current.Password,
                                                             Yqun.Common.ContextCache.ApplicationContext.Current.UserCode,
                                                             Yqun.Common.ContextCache.ApplicationContext.Current.InTestRoom.Code);
                                #endregion

                                #region  步本地模板
                                SetText("初始化用户配置信息.......");

                                SetText("同步本地模板.......");
                                _CaijiCommHelper.UpdateLocalModelInfo(_Module.SpecialSetting.TestRoomCode);
                                #endregion

                                if (!Yqun.Common.ContextCache.ApplicationContext.Current.IsAdministrator)
                                {
                                    #region  步本地配置信息

                                    try
                                    {
                                        SetText("检查本地配置信息状态.......");
                                        switch (_CaijiCommHelper.GetClientConfigStatus(_Module.SpecialSetting.MachineCode))
                                        {
                                        case -1:
                                            SetText("本地配置信息状态检查失败,请联系管理员.......");
                                            break;

                                        case 0:
                                            //SetText("同步本地配置信息.......");
                                            //_CaijiCommHelper.UploadClientConfig(_Module.SpecialSetting.TestRoomCode, _Module.SpecialSetting.MachineCode, Newtonsoft.Json.JsonConvert.SerializeObject(_Module.SpecialSetting));
                                            MessageBox.Show("本地设备信息未在服务上注册,请联系管理员!", "本地配置提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                                            CloseFrom();
                                            goto ReLogin;

                                        case 1:
                                            SetText("同步本地配置信息.......");
                                            _CaijiCommHelper.UpDateClientConfig(_Module.SpecialSetting.MachineCode, Newtonsoft.Json.JsonConvert.SerializeObject(_Module.SpecialSetting));
                                            break;

                                        case 2:
                                            SetText("同步本地配置信息.......");
                                            _CaijiCommHelper.GetClientConfig(ref _Module, _Module.SpecialSetting.MachineCode);
                                            break;
                                        }
                                        SetText("同步本地配置信息完成.......");
                                    }
                                    catch (Exception exSysConfig)
                                    {
                                        log.WriteLog(exSysConfig.Message + System.Environment.NewLine + exSysConfig.TargetSite, true, true);
                                    }

                                    #endregion
                                }
                                #region   本地数据

                                if (!Yqun.Common.ContextCache.ApplicationContext.Current.IsAdministrator)
                                {
                                    SetText("上传本地数据中.......");
                                    if (_CaijiCommHelper.UpdateLocalDateToServer())
                                    {
                                        SetText("上传本地数据成功.......");
                                    }
                                    else
                                    {
                                        SetText("上传本地数据失败,下次启动将继续上传.......");
                                    }
                                }

                                #endregion

                                #region 记录历史登陆
                                _LoginForm.MemberUser();
                                #endregion

                                #endregion
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("网络连接失败!已经转至本地服务登录!" + ex.Message, "登录提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                            Yqun.Common.ContextCache.ApplicationContext.Current.ISLocalService = true;
                        }
                        #endregion
                    }
                    else
                    {
                        #region 本地用户登录
                        ShowForm();
                        SetText("用户登录中.......");
                        SetText("读取本地登录用户信息.......");
                        DataTable _DataTable = new DataTable();

                        _DataTable = _CaijiCommHelper.GetUserInfo(_LoginForm.cmbUserName.Text, _LoginForm.txtUserPwd.Text);

                        if (_DataTable != null && _DataTable.Rows.Count > 0)
                        {
                            Yqun.Common.ContextCache.ApplicationContext.Current.UserName        = _LoginForm.cmbUserName.Text;
                            Yqun.Common.ContextCache.ApplicationContext.Current.Password        = _LoginForm.txtUserPwd.Text;
                            Yqun.Common.ContextCache.ApplicationContext.Current.UserCode        = _DataTable.Rows[0]["UserCode"].ToString();
                            Yqun.Common.ContextCache.ApplicationContext.Current.InTestRoom.Code = _DataTable.Rows[0]["UserTestCode"].ToString();
                            Yqun.Common.ContextCache.ApplicationContext.Current.ISLocalService  = true;
                            _Module = CalHelper.LoadModule();
                        }
                        else
                        {
                            if (MessageBox.Show("用户名或密码错误,请查证后在登录!", "登录错误", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
                            {
                                goto ReLogin;
                            }
                            else
                            {
                                Application.Exit();
                                return;
                            }
                        }
                        #endregion
                    }

                    #region 释放资源
                    if (_SplashScreen != null)
                    {
                        CloseFrom();
                    }
                    #endregion


                    #endregion

                    Application.EnableVisualStyles();
                    //Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm(log, _Module, _CaijiCommHelper));
                }
            }
            catch (Exception ex)
            {
                log.WriteLog(ex.StackTrace, true, false);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            try
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Registration registration = new Registration();
                string       filename     = Path.Combine(Application.StartupPath, "License.dat");
                bool         AuthResult   = false;
                if (!(File.Exists(filename) && registration.AuthSerial(filename)))
                {
                    if (DialogResult.OK == registration.ShowDialog())
                    {
                        AuthResult = true;
                    }
                }
                else
                {
                    AuthResult = true;
                }

                if (AuthResult)
                {
                    //JZUpgrade.Logger log = new JZUpgrade.Logger();
                    //log.Logfolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log");
                    //log.IsUseLog = true;
                    //log.QueueBufferSize = 5;
                    //JZUpgrade.UpdateHelperClient uhc = new JZUpgrade.UpdateHelperClient(log, "8");

                    //if (!ProcessHelper.IsRuningProcess("JZUpgradeAgent"))
                    //{
                    //    ProcessStartInfo Info = new ProcessStartInfo();
                    //    Info.CreateNoWindow = false;
                    //    Info.UseShellExecute = true;
                    //    Info.FileName = Path.Combine(Application.StartupPath, "JZUpgrade.exe");
                    //    Info.Arguments = "\"8\"";
                    //    //1 管理系统文件+不执行,2 采集系统文件+不执行,3 管理系统数据+不执行,
                    //    //4 管理系统文件+数据+执行;
                    //    //5 采集系统文件+执行
                    //    //6 管理系统执行
                    //    //7 采集系统执行
                    //    //8 管理系统文件+执行
                    //    Process.Start(Info);

                    //}
Again:
                    Boolean isRemote = Internet.IsWanAlive();
                    LoginDialog loginDialog = new LoginDialog();

                    //如果有sys.xml且NetTcpDomainAddress做了配置,强制更改Endpoint设置
                    string EndPointAddress0     = BizCommon.ConfigHelper.GetEndpointAddress("TransferServiceEndPoint").ToString();
                    string NetTcpDomainAddress0 = BizCommon.ConfigHelper.GetSysXmlValue("NetTcpDomainAddress");
                    string strAddressM0         = EndPointAddress0.Replace("net.tcp://", "");
                    strAddressM0 = strAddressM0.Substring(0, strAddressM0.IndexOf('/'));
                    string strAddressE0 = EndPointAddress0.Replace("net.tcp://", "").Replace(strAddressM0, "");
                    if (!string.IsNullOrEmpty(NetTcpDomainAddress0))//配置了域名地址,开始判断域名地址
                    {
                        EndPointAddress0 = "net.tcp://" + NetTcpDomainAddress0 + strAddressE0;
                        BizCommon.ConfigHelper.SetEndpointAddress("TransferServiceEndPoint", EndPointAddress0);
                    }
                    if (DialogResult.OK == loginDialog.ShowDialog())
                    {
                        if (loginDialog.IsUpdate == false)
                        {
                            #region 登录
                            const String resourceName = "Splash.jpg"; // 嵌入的资源图片的名字

                            splash = SplashScreen.Current;
                            splash.SetTransparentKey  = Color.Fuchsia;
                            splash.SetBackgroundImage = GetResource(resourceName);

                            splash.SetFade = true;//设置淡入淡出效果

                            splash.ShowSplashScreen();

                            AppDomain currentDomain = AppDomain.CurrentDomain;
                            currentDomain.AssemblyLoad += asmLoadHandler;

                            splash.SetStatus = "验证用户并登录...";

                            if (loginDialog.com_User.Text.Trim() == "")
                            {
                                splash.CloseSplashScreen();
                                MessageBox.Show("请输入用户名。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                goto Again;
                            }

                            try
                            {
                                isRemote = true;
                                Boolean logined = loginDialog.Login(isRemote);
                                if (!logined)
                                {
                                    //Modifyed By TanLiPing In 2014-03-16
                                    //添加智能识别DNS和域名代码
                                    if (Yqun.Services.Agent.TestNetwork() == true)//net.tcp配置可以正常访问网络
                                    {
                                        splash.CloseSplashScreen();
                                        MessageBox.Show("用户名或密码错误,请重新输入。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                        goto Again;
                                    }
                                    else//使用备用地址
                                    {
                                        bool   bNetWorkIsConnected = false;
                                        string EndPointAddress     = BizCommon.ConfigHelper.GetEndpointAddress("TransferServiceEndPoint").ToString();
                                        string NetTcpDomainAddress = BizCommon.ConfigHelper.GetSysXmlValue("NetTcpDomainAddress");
                                        string strAddressM         = EndPointAddress.Replace("net.tcp://", "");
                                        strAddressM = strAddressM.Substring(0, strAddressM.IndexOf('/'));
                                        string strAddressE = EndPointAddress.Replace("net.tcp://", "").Replace(strAddressM, "");
                                        if (!string.IsNullOrEmpty(NetTcpDomainAddress))//配置了域名地址,开始判断域名地址
                                        {
                                            EndPointAddress = "net.tcp://" + NetTcpDomainAddress + strAddressE;
                                            BizCommon.ConfigHelper.SetEndpointAddress("TransferServiceEndPoint", EndPointAddress);
                                            if (Yqun.Services.Agent.TestNetwork() == true)//域名可以连接上
                                            {
                                                bNetWorkIsConnected = true;
                                            }
                                            else
                                            {
                                                string NetTcpIPAddress = BizCommon.ConfigHelper.GetSysXmlValue("NetTcpIPAddress");
                                                if (!string.IsNullOrEmpty(NetTcpIPAddress))
                                                {
                                                    EndPointAddress = "net.tcp://" + NetTcpIPAddress + strAddressE;
                                                    BizCommon.ConfigHelper.SetEndpointAddress("TransferServiceEndPoint", EndPointAddress);
                                                    if (Yqun.Services.Agent.TestNetwork() == true)
                                                    {
                                                        bNetWorkIsConnected = true;
                                                    }
                                                }
                                                else
                                                {
                                                    bNetWorkIsConnected = false;
                                                    EndPointAddress     = "net.tcp://" + strAddressM + strAddressE;
                                                    BizCommon.ConfigHelper.SetEndpointAddress("TransferServiceEndPoint", EndPointAddress);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            string NetTcpIPAddress = BizCommon.ConfigHelper.GetSysXmlValue("NetTcpIPAddress");
                                            if (!string.IsNullOrEmpty(NetTcpIPAddress))
                                            {
                                                EndPointAddress = "net.tcp://" + NetTcpIPAddress + strAddressE;
                                                BizCommon.ConfigHelper.SetEndpointAddress("TransferServiceEndPoint", EndPointAddress);
                                                if (Yqun.Services.Agent.TestNetwork() == true)
                                                {
                                                    bNetWorkIsConnected = true;
                                                }
                                            }
                                            else
                                            {
                                                bNetWorkIsConnected = false;
                                                EndPointAddress     = "net.tcp://" + strAddressM + strAddressE;
                                                BizCommon.ConfigHelper.SetEndpointAddress("TransferServiceEndPoint", EndPointAddress);
                                            }
                                        }
                                        if (bNetWorkIsConnected)
                                        {
                                            logined = loginDialog.Login(isRemote);
                                            if (!logined)
                                            {
                                                splash.CloseSplashScreen();
                                                MessageBox.Show("用户名或密码错误,请重新输入。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                                goto Again;
                                            }
                                        }
                                        else
                                        {
                                            splash.CloseSplashScreen();
                                            MessageBox.Show("连接不上服务器端,请检查网络配置。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                            goto Again;
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                splash.CloseSplashScreen();
                                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                goto Again;
                            }

                            splash.SetStatus = "登录成功并注册用户信息...";

                            loginDialog.MemberUser();

                            splash.SetStatus = "启动主程序...";
                            splash.CloseSplashScreen();

                            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                            //验证初始密码,用户取消退出程序
                            if (!loginDialog.ChangDefaultPwd())
                            {
                                Application.Exit();
                            }
                            else
                            {
                                Application.Run(new MainForm());
                            }
                            #endregion
                        }
                        else
                        {
                            #region 更新
                            JZUpgrade.UpdateAlert ua = new JZUpgrade.UpdateAlert(1);
                            if (ua.ShowDialog() == DialogResult.OK)
                            {
                                if (!ProcessHelper.IsRuningProcess("JZUpgradeAgent"))
                                {
                                    ProcessStartInfo Info = new ProcessStartInfo();
                                    Info.CreateNoWindow  = false;
                                    Info.UseShellExecute = true;
                                    Info.FileName        = Path.Combine(Application.StartupPath, "JZUpgrade.exe");
                                    Info.Arguments       = "\"8\"";
                                    //1 管理系统文件+不执行,2 采集系统文件+不执行,3 管理系统数据+不执行,
                                    //4 管理系统文件+数据+执行;
                                    //5 采集系统文件+执行
                                    //6 管理系统执行
                                    //7 采集系统执行
                                    //8 管理系统文件+执行
                                    Process.Start(Info);
                                    //Process currentProcess = Process.GetCurrentProcess();
                                    //currentProcess.Kill();
                                }
                            }
                            #endregion
                        }
                    }
                }
                //}
                //else
                //{
                //    MessageBox.Show("一个实例正在运行...", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //    Application.Exit();
                //}
            }
            catch (Exception ex)
            {
                String Error = string.Format("应用程序出错,出错原因:{0}", ex.Message);
                logger.Error(Error);
                MessageBox.Show(Error, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }