SystemService.cs
10.5 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
using ApkInfo;
using Hh.Mes.Common;
using Hh.Mes.Common.Json;
using Hh.Mes.Common.log;
using Hh.Mes.Common.Redis;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.ViewModel;
using Hh.Mes.Service.Repository;
using Hh.Mes.Service.SystemAuth;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Hh.Mes.Service
{
public class SystemService : RepositorySqlSugar<sys_user>
{
AuthContextFactory authContextFactory;
public SystemService(AuthContextFactory authContextFactory)
{
this.authContextFactory = authContextFactory;
}
/// <summary>
/// 登入
/// </summary>
public dynamic Login(string userName, string password, string appKey, string appSecret)
{
return ExceptionsHelp.Instance.ExecuteT<dynamic>(() =>
{
var response = new Response();
#region 获取应用信息
var appInfo = Context.Queryable<sys_info>().First(u => u.appKey == appKey);
if (appInfo == null)
{
response.Code = 500;
response.Message = "应用不存在,请检查应用密钥";
return response;
}
if (Encryption.Decrypt(appInfo.appSecret) != appSecret)
{
response.Code = 500;
response.Message = "应用密钥不正确!";
return response;
}
#endregion
#region 获取用户信息
var userInfo = Context.Queryable<sys_user>().First(u => u.account == userName);
if (userInfo == null || userInfo.account != userName)
{
response.Code = 500;
response.Token = "";
response.Message = "用户不存在!";
return response;
}
if (Encryption.Decrypt(userInfo.password) != password)
{
response.Code = 500;
response.Token = "";
response.Message = "密码错误!";
return response;
}
#endregion
var token = Guid.NewGuid().ToString("N");
var currentSession = new UserAuthSession
{
Id = userInfo.id,
Account = userInfo.account,
Name = userInfo.name,
Sex = userInfo.sex,
Token = token,
CreateTime = DateTime.Now,
};
//创建Session
var cli = new RedisBase();
cli.SetT(token, currentSession, cli.dayTime);
response.Code = 200;
response.Status = true;
response.Token = token;
response.Result = currentSession;
response.Message = "登入成功";
return response;
});
}
/// <summary>
/// 获取PDA用户可访问的模块列表
/// </summary>
public dynamic GetPDAModules(string token)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response<List<PDAModule>>();
if (string.IsNullOrEmpty(token)) return response.ResponseError("参数【token】为空!");
var cl = new RedisBase();
var userAuthSession = cl.GetT<UserAuthSession>(token);
if (userAuthSession == null) return response.ResponseError("登录已过期,请重新登录!");
//直接从redis中获取用户权限
var authStrategy = cl.GetT<AuthStrategyContext>(userAuthSession.Account);
if (authStrategy == null)
{
authStrategy = authContextFactory.GetAuthStrategyContext(userAuthSession.Account);
}
#region 根据用户权限,组合能访问的PDA模块
List<PDAModule> pdaModules = new List<PDAModule>();
var pdaModuleList = authStrategy.Modules.Where(t => t.Name.ToLower().StartsWith("pda"));
foreach (var moduleItem in pdaModuleList)
{
//组合PDA模块
PDAModule pdaModule = new PDAModule();
pdaModule.code = moduleItem.Code;
pdaModule.name = moduleItem.Name;
pdaModule.details = new List<PDAModuleLevel2>();
pdaModules.Add(pdaModule);
var sunModuleList = authStrategy.Modules.Where(t => t.ParentId == moduleItem.Id);
foreach (var sunModuleItem in sunModuleList)
{
//组合PDA二级模块
PDAModuleLevel2 pdaModuleLevel2 = new PDAModuleLevel2();
pdaModuleLevel2.code = sunModuleItem.Code;
pdaModuleLevel2.name = sunModuleItem.Name;
pdaModuleLevel2.details = new List<PDAElement>();
pdaModule.details.Add(pdaModuleLevel2);
foreach (var elementItem in sunModuleItem.Elements)
{
//组合PDA窗体
PDAElement pdaElement = new PDAElement();
pdaElement.icon = elementItem.Class;
pdaElement.tit = elementItem.Name;
pdaElement.path = elementItem.DomId;
pdaModuleLevel2.details.Add(pdaElement);
}
}
}
#endregion
response.Result = pdaModules;
return response;
});
}
/// <summary>
/// 登退 【删除redis,删除 sys_user_online】
/// </summary>
public dynamic Logout(string token)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
if (string.IsNullOrEmpty(token)) return response.ResponseError("参数【token】为空");
var cl = new RedisBase();
var result = cl.redisClient.Exists(token);
if (result)
{
//再清空用户登录信息
cl.redisClient.Del(token);
}
Context.Deleteable<sys_user_online>().Where(it => it.token == token).ExecuteCommand();
return response.ResponseSuccess();
});
}
/// <summary>
/// 判断token是否存在,过期
/// </summary>
public dynamic AppCheckToken(string token)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
if (string.IsNullOrEmpty(token)) return response.ResponseError("参数【token】为空");
var isOk = new RedisBase().ExistsKey(token);
return isOk ? response.ResponseSuccess() : response.ResponseError("token失效,请退出重新登入!");
});
}
/// <summary>
/// APP检查 最新版本 升级
/// </summary>
/// <returns></returns>
public dynamic AppCheckVerByAppNameAndVer(string appId, double ver)
{
return ExceptionsHelp.Instance.ExecuteT(() =>
{
var response = new Response();
var app = Context.Queryable<sys_app>().OrderBy(x => x.ver, OrderByType.Desc).First(x => x.appId == appId && x.ver > ver);
if (app == null)
{
response.Code = 400;
response.Status = false;
response.Message = $"当前APP【应用标识appId:{appId}、版本ver:{ver}】没有最新的版本更新!";
return response;
}
response.Result = new
{
wgtUrl = app.filePath,
installPath = "_downloads/UploadFile/APP"
};
return response;
});
}
/// <summary>
/// 第3方登入
/// </summary>
/// <returns></returns>
public dynamic OtherLogin(string otherToken)
{
var response = new Response();
#region before
if (string.IsNullOrEmpty(otherToken))
{
return response.ResponseError("参数【token】传入为空!");
}
var json = JwtEncryption.Decode(otherToken);
var userInfo = DynamicJson.Parse(json);
var token = Guid.NewGuid().ToString("N");
var cli = new RedisBase();
//直接从redis中获取用户权限
var authStrategy = cli.GetT<UserAuthSession>(token);
if (authStrategy == null)
{
var user = GetSysUserByAccount(userInfo.loginName);
if (user == null)
{
return response.ResponseError($"第三方登入没有查询到您的用户信息,请核实信息【loginName】。或者在中控系统【用户管理】新增此用户{user.loginName}!");
}
var currentSession = new UserAuthSession
{
Id = user.id,
Account = user.account,
Name = user.name,
Sex = user.sex,
Idcard = user.idcard,
Token = token,
CreateTime = DateTime.Now,
};
//创建Session
cli.SetT(token, currentSession, cli.dayTime);
}
response.Token = token;
return response.ResponseSuccess("登入成功!");
#endregion
}
/// <summary>
/// 枚举对象
/// </summary>
public string GetState()
{
string json = typeof(EnumLog).GetJsonEnums();
json = json.Replace("var ", "\"").Replace("=", "\":");
json = "{" + json + "}";
return json;
}
private sys_user GetSysUserByAccount(string account)
{
return Context.Queryable<sys_user>().First(x => x.account == account);
}
}
}