App.xaml.cs
5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
}
}