唐召明
authored
|
1
|
using Hh.Mes.Common;
|
赖素文
authored
|
2
3
4
5
6
7
8
9
|
using Hh.Mes.Common.Json;
using Hh.Mes.Common.log;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.ViewModel;
using Hh.Mes.Service.Repository;
using Hh.Mes.Service.SystemAuth;
|
唐召明
authored
|
10
|
using Microsoft.Extensions.Caching.Distributed;
|
赖素文
authored
|
11
12
13
14
|
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
|
唐召明
authored
|
15
|
using System.Text.Json;
|
赖素文
authored
|
16
17
18
19
20
21
|
namespace Hh.Mes.Service
{
public class SystemService : RepositorySqlSugar<sys_user>
{
AuthContextFactory authContextFactory;
|
唐召明
authored
|
22
23
24
|
private readonly IDistributedCache _cache;
public SystemService(AuthContextFactory authContextFactory, IDistributedCache cache)
|
赖素文
authored
|
25
26
|
{
this.authContextFactory = authContextFactory;
|
唐召明
authored
|
27
|
_cache = cache;
|
赖素文
authored
|
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
|
}
/// <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
|
唐召明
authored
|
84
85
|
var cacheOption = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromDays(1));
_cache.Set(token, JsonSerializer.SerializeToUtf8Bytes(currentSession), cacheOption);
|
赖素文
authored
|
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
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】为空!");
|
唐召明
authored
|
105
106
107
108
109
110
|
var userAuthSessionBytes = _cache.Get(token);
if (userAuthSessionBytes == null)
{
return response.ResponseError("登录已过期,请重新登录!");
}
var userAuthSession = JsonSerializer.Deserialize<UserAuthSession>(userAuthSessionBytes);
|
赖素文
authored
|
111
112
|
//直接从redis中获取用户权限
|
唐召明
authored
|
113
114
115
|
var authStrategyBytes = _cache.Get(userAuthSession.Account);
AuthStrategyContext authStrategy;
if (authStrategyBytes == null)
|
赖素文
authored
|
116
117
118
|
{
authStrategy = authContextFactory.GetAuthStrategyContext(userAuthSession.Account);
}
|
唐召明
authored
|
119
120
121
122
123
|
else
{
authStrategy = JsonSerializer.Deserialize<AuthStrategyContext>(authStrategyBytes);
}
|
赖素文
authored
|
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
|
#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】为空");
|
唐召明
authored
|
174
175
|
//再清空用户登录信息
_cache.Remove(token);
|
赖素文
authored
|
176
177
178
179
180
181
182
183
184
185
186
187
188
|
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();
|
唐召明
authored
|
189
190
191
192
193
194
|
if (string.IsNullOrEmpty(token))
{
return response.ResponseError("参数【token】为空");
}
var bytes = _cache.Get(token);
return bytes != null ? response.ResponseSuccess() : response.ResponseError("token失效,请退出重新登入!");
|
赖素文
authored
|
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
|
});
}
/// <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");
//直接从redis中获取用户权限
|
唐召明
authored
|
241
242
243
|
var authStrategyBytes = _cache.Get(token);
if (authStrategyBytes == null)
|
赖素文
authored
|
244
|
{
|
唐召明
authored
|
245
|
var authStrategy = JsonSerializer.Deserialize<UserAuthSession>(token);
|
赖素文
authored
|
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
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,
};
|
唐召明
authored
|
262
|
var cacheOption = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromDays(1));
|
赖素文
authored
|
263
|
//创建Session
|
唐召明
authored
|
264
|
_cache.Set(token, JsonSerializer.SerializeToUtf8Bytes(currentSession), cacheOption);
|
赖素文
authored
|
265
266
267
268
269
270
271
272
273
274
275
276
277
|
}
response.Token = token;
return response.ResponseSuccess("登入成功!");
#endregion
}
private sys_user GetSysUserByAccount(string account)
{
return Context.Queryable<sys_user>().First(x => x.account == account);
}
}
}
|