ConfigRead.cs
7.64 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using Hh.Mes.Common.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Text;
namespace Hh.Mes.Common.config
{
/// <summary>
/// 配置读取 ,公共变量 支持内存缓存
/// </summary>
public class ConfigRead
{
#region 属性
private static volatile ConfigRead _configRead = null;
private readonly AppsettingsHelp _appsettingsHelp;
/// <summary>
/// ConnectionStrings
/// </summary>
private ConnectionStrings appsetConnection { get; set; }
private Dictionary<string, string> OperType { get; set; }
/// <summary>
/// Gets the instance.
/// </summary>
public static ConfigRead GetInstance
{
get
{
if (_configRead == null)
{
lock (typeof(ConfigRead))
{
if (_configRead == null)
{
_configRead = new ConfigRead();
}
}
}
return _configRead;
}
}
/// <summary>
/// BaseInfoCache.json sql
/// </summary>
private Dictionary<string, string> baseSql { get; set; }
#endregion
private ConfigRead()
{
_appsettingsHelp = new AppsettingsHelp();
}
#region method
/// <summary>
/// ConnectionStrings 密码已解密
/// </summary>
/// <returns></returns>
public ConnectionStrings GetAppsetConnection()
{
try
{
if (appsetConnection == null)
{
appsetConnection = _appsettingsHelp.GetAppsettings<ConnectionStrings>("ConnectionStrings");
var stringBuilder = new SqlConnectionStringBuilder(appsetConnection.BaseDBContext);
stringBuilder.Password = Encryption.Decrypt(stringBuilder.Password);
appsetConnection.BaseDBContext= stringBuilder.ConnectionString;
if (!string.IsNullOrEmpty(appsetConnection.AopAllowed))
{
var temp = appsetConnection.AopAllowed.Split(',', StringSplitOptions.RemoveEmptyEntries);
appsetConnection.allowed = temp;
}
}
}
catch
{
throw new Exception("ConfigRead 类 ,GetAppsetConnection 方法读取数据库连接失败");
}
return appsetConnection;
}
/// <summary>
/// OperLogFilter OperType
/// </summary>
/// <returns></returns>
public Dictionary<string, string> GetOperType()
{
if (OperType == null)
{
OperType = new Dictionary<string, string>
{
{"Index", "查看"},
{"Ins", "增加"},
{"Upd", "更新"},
{"DelByIds", "删除"}
};
}
return OperType;
}
#region BaseInfoCache
/// <summary>
/// GetBaseInfoSql
/// </summary>
/// <returns></returns>
public Dictionary<string, string> GetBaseInfoSql()
{
if (baseSql == null || baseSql.Count == 0)
{
baseSql = new Dictionary<string, string>();
string filePath = Path.Combine(AppContext.BaseDirectory, "BaseInfoCache.json");
string content = ReadFileString(filePath);
var tempData = JsonHelper.JsonConvertObject<BaseInfoCls>(content);
for (int i = 0; i < tempData.baseInfo.Count; i++)
{
baseSql.Add(tempData.baseInfo[i].key, tempData.baseInfo[i].sql);
}
}
return baseSql;
}
/// <summary>
/// 读取文件 返回string
/// </summary>
protected string ReadFileString(string filePath)
{
string content = "";
using (StreamReader sr = new StreamReader(filePath, Encoding.Default))
{
content = sr.ReadToEnd();
}
return content;
}
#endregion
#endregion
}
/// <summary>
/// appsettings.json ConnectionStrings 节点类
/// </summary>
public class ConnectionStrings
{
public string BaseDBContext { get; set; }
public string InterfaceDBContext { get; set; }
public string RedisIp { get; set; }
public int HttpsPort { get; set; }
public int HttpPort { get; set; }
//当前环境是http还是https 默认值是http
public string HttpOrHttps{ get; set; }
public int ReportPort { get; set; }
public string LoginTitle { get; set; }
public string FaceBaseUrl { get; set; }
public string ServerIPList { get; set; }
public string CerPwd { get; set; }
public string CerPath { get; set; }
/// <summary>
/// 是否是开发环境 false是开发环境,true是正式环境
/// 开发者程序禁止修改
///</summary>
public bool IsDevelopment { get; set; }
/// <summary>
/// AOP 不拦截
/// </summary>
public string AopAllowed { get; set; }
/// <summary>
///重置密码
/// </summary>
public string ResetPwd { get; set; }
/// <summary>
/// aop 判断使用次字段
/// </summary>
public string[] allowed { get; set; }
/// <summary>
/// aop 判断使用次字段
/// </summary>
public string[] ext { get; set; }
/// <summary>
/// 扩展1
/// </summary>
public dynamic Extend1 { get; set; }
/// <summary>
/// 扩展2
/// </summary>
public dynamic Extend2 { get; set; }
/// <summary>
/// 扩展3
/// </summary>
public dynamic Extend3 { get; set; }
/// <summary>
/// 扩展4
/// </summary>
public dynamic Extend4 { get; set; }
/// <summary>
/// 扩展5
/// </summary>
public dynamic Extend5 { get; set; }
/// <summary>
/// 扩展6
/// </summary>
public dynamic Extend6 { get; set; }
/// <summary>
/// 扩展7
/// </summary>
public dynamic Extend7 { get; set; }
/// <summary>
/// 扩展8
/// </summary>
public dynamic Extend8{ get; set; }
#region 配置文件AppCustomSettings 扩展字段
public dynamic AppCustomExtend1 { get; set; }
public dynamic AppCustomExtend2 { get; set; }
public dynamic AppCustomExtend3 { get; set; }
public dynamic AppCustomExtend4 { get; set; }
public dynamic AppCustomExtend5 { get; set; }
public dynamic AppCustomExtend6 { get; set; }
public dynamic AppCustomExtend7 { get; set; }
public dynamic AppCustomExtend8 { get; set; }
#endregion
}
#region BaseInfoCache.json 缓存类
internal class BaseInfoCls
{
/// <summary>
///
/// </summary>
public List<BaseInfoItem> baseInfo { get; set; }
}
internal class BaseInfoItem
{
/// <summary>
///
/// </summary>
public string key { get; set; }
/// <summary>
///
/// </summary>
public string sql { get; set; }
}
#endregion
}