App.xaml.cs 5.89 KB
using HHECS.Application.Licenses;
using HHECS.Application.Service;
using HHECS.Dal;
using HHECS.Infrastructure.Enums;
using HHECS.Infrastructure.Notice;
using HHECS.Model.Entities;
using HHECS.WinClient.View.Login;
using HHECS.WinClient.View.Main;
using HHECS.WinClient.View.UserPermission;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Threading.Tasks;
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.WinClient
{
    public partial class App
    {

        #region 服务

        public static EquipmentService EquipmentService { get; set; } = new EquipmentService();

        public static ErrorCodeService ErrorCodeService { get; set; } = new ErrorCodeService();

        public static ExecuteService ExcuteService { get; set; } = new ExecuteService();

        public static JobService JobService { get; set; } = new JobService();

        public static LogService LogService { get; set; } = new LogService();

        public static PermissionService PermissionService { get; set; } = new PermissionService();

        public static SystemService SystemService { get; set; } = new SystemService();

        #endregion

        #region 属性

        /// <summary>
        /// 当前用户
        /// </summary>
        public static User User { get; set; }

        /// <summary>
        /// 比对授权文件中的机器码
        /// </summary>
        public static string LicenseId { get; private set; } = "ECS4.0";

        /// <summary>
        /// 授权model
        /// </summary>
        public static LicenseModel LicenseModel { get; set; }

        /// <summary>
        /// 当前用户的权限
        /// </summary>
        public static List<Permission> UserPermissions { get; set; }

        /// <summary>
        /// 本程序对应的权限模块
        /// </summary>
        public static string Module { get; internal set; } = "ecswin";

        /// <summary>
        /// Nlog全局对象
        /// </summary>
        public static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

        /// <summary>
        /// Excel导出路径
        /// </summary>
        public static string ExportPath = ConfigurationManager.AppSettings["ExportPath"];

        /// <summary>
        /// 区域
        /// </summary>
        public static string DestinationArea = ConfigurationManager.AppSettings["DestinationArea"];

        #endregion

        private void Application_Startup(object sender, System.Windows.StartupEventArgs e)
        {
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;

            DALHelper.Constr = ConfigurationManager.ConnectionStrings["mysql"].ConnectionString;


            System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("HHECS.WinClient");
            if (p.Length > 1)
            {
                MessageBox.Show("程序已启动!", "重复启动");
                Environment.Exit(0);
            }

            //授权校验
            var licenseResult = App.PermissionService.GetLicense();
            if (!licenseResult.Success)
            {
                if (!licenseResult.Success)
                {
                    WinLicense winLicense = new WinLicense();
                    var flag = winLicense.ShowDialog();
                    if (flag != true)
                    {
                        Environment.Exit(0);
                    }
                }
                else
                {
                    MessageBox.Show(licenseResult.Msg, $"授权失败:{licenseResult.Msg}");
                    Environment.Exit(0);
                }
            }
            else
            {
                var license = licenseResult.Data;
                var checkResult = App.PermissionService.CheckLicense(license, App.LicenseId);
                if (!checkResult.Success)
                {
                    WinLicense winLicense = new WinLicense();
                    var flag = winLicense.ShowDialog();
                    if (flag != true)
                    {
                        Environment.Exit(0);
                    }
                }
            }

            App.LicenseModel = licenseResult.Data;

            var result1 = new WinLogin().ShowDialog();
            if (result1 == true)
            {
                new WinMain().Show();
            }
            else
            {
                this.Shutdown();
            }
        }

        #region 异常捕获

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            string msg = String.Format("{0}\n\n{1}", ex.Message, ex.StackTrace);
            NoticeBus.Notice($"Task线程异常", Level.Exception, ex);
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is Exception ex)
            {
                string msg = String.Format("发生异常,程序即将停止\n\n{0}\n\n{1}", ex.Message, ex.StackTrace);//异常信息 和 调用堆栈信息
                NoticeBus.Notice($"非UI线程异常", Level.Exception, ex);
                MessageBox.Show(msg, "非UI线程异常");
            }
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            Exception ex = e.Exception;
            string msg = String.Format("{0}\n\n{1}", ex.Message, ex.StackTrace);//异常信息 和 调用堆栈信息
            MessageBox.Show(msg, "UI线程异常");
            NoticeBus.Notice("UI线程异常", Level.Exception, ex);
            e.Handled = true;
        }

        #endregion
    }
}