LoginService.java
15.1 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package cn.csbr.app.auth;
import cn.csbr.app.config.FxConfigure;
import cn.csbr.app.exception.AuthException;
import cn.csbr.app.gui.GUIContext;
import cn.csbr.app.gui.page.setting.SettingPage;
import cn.csbr.app.model.LoginUser;
import cn.csbr.app.model.RfidDataModel;
import cn.csbr.app.service.BaseService;
import cn.csbr.app.service.CabinetService;
import cn.csbr.springboot.dao.mapper.*;
import cn.csbr.springboot.dao.model.*;
import cn.hutool.core.date.DateTime;
import com.csbr.util.IdUtils;
import javafx.application.Platform;
import javafx.util.Callback;
import javafx.util.Pair;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import tk.mybatis.mapper.entity.Example;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.csbr.util.MD5Util;
/**
* 就是登录类了
*/
@Service
public class LoginService implements Callback<Pair<String, String>, Void> {
private static Logger log = LoggerFactory.getLogger(LoginService.class);
/** RFID数据集合 */
private static final List<RfidDataModel> RFID_DATAS;
static {
RFID_DATAS = new LinkedList<>();
}
private Object lock = new Object();
@Autowired
private SysUserMapper sysUserMapper;
@Autowired
private SysOrgMapper sysOrgMapper;
@Autowired
private SysRoleMapper sysRoleMapper;
@Autowired
private SysMenuMapper sysMenuMapper;
@Autowired
private GUIContext guiContext;
@Autowired
private FxConfigure fxConfigure;
@Autowired
private BaseService baseService;
@Autowired
private DatUserLogMapper datUserLogMapper;
@Autowired
private CabinetService cabinetService;
@Value("${loginuser}")
private String loginuser;
/**
* 账户密码验证身份
* @param user
*/
public String loginVali(LoginUser user){
log.info("开始验证账号");
System.out.println("-----------------------login----user:" + user);
if (StringUtils.isEmpty(user.getUserName())) {
//guiContext.setLoginingFlag(false);
throw new AuthException("请录入用户名");
}
if (StringUtils.isEmpty(user.getPassword())) {
//guiContext.setLoginingFlag(false);
throw new AuthException("请录入密码");
}
//构建SysUser类型的通用查询对象
// 根据用户名调用查询方法
List<SysUser> users = sysUserMapper.findUserLoginCheck(guiContext.getStoreguid(),user.getUserName());
if (users.size() == 0) {
// guiContext.setLoginingFlag(false);
throw new AuthException("不存在");
}
SysUser sysUser = users.get(0);
// 判断页面密码加密后与数据库查出的是否相等
if (!sysUser.getPwd().equals(MD5Util.md5Encode(user.getPassword()))) {
//guiContext.setLoginingFlag(false);
throw new AuthException("密码录入错误");
}
if(sysUser.getBizstate().equals("N")) {
//guiContext.setLoginingFlag(false);
throw new AuthException("该用户已被禁用");
}
user.setUserName(sysUser.getRealname());
user.setGuid(sysUser.getGuid());
user.setCardId(sysUser.getCardId());
// 若存在权限信息
if (sysUser.getRoleguid() != null) {
//设置角色id到登录信息中
user.setRoleguid(sysUser.getRoleguid());
// 根据roleguid去sys_role表获取权限信息
SysRole role = sysRoleMapper.selectByPrimaryKey(sysUser.getRoleguid());
// 以“-”为分界将权限菜单信息(对应sys_menu表的guid)放入menuGuids数组列表
if(role == null) {
throw new AuthException("未查询到当前角色权限");
}
String[] menuGuids = role.getRolemenu().split("-");
Example mexample = new Example(SysMenu.class);
mexample.createCriteria().andIn("guid", Arrays.asList(menuGuids));
// 根据sys_menu表的guid数组集合查询对应的菜单信息列表
List<SysMenu> sysMenus = sysMenuMapper.selectByExample(mexample);
List<String> menusStrs = new ArrayList<>();
for (SysMenu menu : sysMenus) {
// 操作目录添加到menusStrs列表中
menusStrs.add(menu.getMenudir());
log.info("1111111");
}
user.setMenus(menusStrs);
} else {
user.setMenus(new ArrayList());
}
Example mexample = new Example(SysOrg.class);
mexample.createCriteria().andEqualTo("guid", sysUser.getOrgguid());
// 根据sys_menu表的guid数组集合查询对应的菜单信息列表
List<SysOrg> org = sysOrgMapper.selectByExample(mexample);
user.setRealName(sysUser.getRealname());
user.setOrgGuid(sysUser.getOrgguid());
user.setOrgName(org.get(0).getOrgname());
guiContext.setLoginUserVali(user);
guiContext.startOtherCabFingerAndId();
return sysUser.getRoleguid();
}
/**
* id卡指纹验证身份
* @param sysUser
* @return
*/
public String LoginByCarid(SysUser sysUser){
System.out.println("-----------------------login----user:" + sysUser);
LoginUser loginUser = new LoginUser();
if(sysUser==null){
throw new AuthException("验证失败,没有该用户");
}
if(sysUser.getBizstate().equals("N")) {
throw new AuthException("该用户已被禁用");
}
loginUser.setUserName(sysUser.getRealname());
loginUser.setPassword(sysUser.getPwd());
loginUser.setRoleguid(sysUser.getRoleguid());
loginUser.setOrgGuid(sysUser.getOrgguid());
//loginUser.setOrgName(sysUser.ge);
guiContext.setLoginUserVali(loginUser);
return sysUser.getRoleguid();
}
@Autowired
private SysStoreOrgMapper sysStoreOrgMapper;
/**
* 录入用户名密码登录
*
* @param user
*/
public void login(LoginUser user) {
guiContext.setOrgids(sysStoreOrgMapper.getorgids(guiContext.getStoreguid()));
if((user.getUserName()==null)||(user.getPassword()==null)){
user.setUserName(loginuser);
//user.setPassword("11111");
}
guiContext.setLoginingFlag(true);
log.info("开始登录");
System.out.println("-----------------------login----user:" + user);
if (fxConfigure.isNodatabase()) {
user.setUserName("uerName");
user.setRealName("realName");
guiContext.setLoginUser(user);
guiContext.setLoginingFlag(false);
return;
}
if (StringUtils.isEmpty(user.getUserName())) {
guiContext.setLoginingFlag(false);
throw new AuthException("请录入用户名");
}
//构建SysUser类型的通用查询对象
Example example = new Example(SysUser.class);
example.createCriteria().andEqualTo("loginuser", user.getUserName());
log.info(user.getUserName());
log.info(guiContext.getStoreguid());
// 根据用户名调用查询方法
List<SysUser> users = sysUserMapper.selectByExample(example);
if (users.size() == 0) {
guiContext.setLoginingFlag(false);
throw new AuthException("该用户不存在");
}
SysUser sysUser = users.get(0);
// 判断页面密码加密后与数据库查出的是否相等
// if (!sysUser.getPwd().equals(MD5Util.md5Encode(user.getPassword()))) {
// guiContext.setLoginingFlag(false);
// throw new AuthException("密码录入错误");
// }
if(sysUser.getBizstate().equals("N")) {
guiContext.setLoginingFlag(false);
throw new AuthException("该用户已被禁用");
}
user.setUserName(sysUser.getRealname());
user.setGuid(sysUser.getGuid());
user.setCardId(sysUser.getCardId());
// 根据guid去sys_org表中获取组织(公司)信息
SysOrg org = sysOrgMapper.selectByPrimaryKey(sysUser.getOrgguid());
if (org != null) {
user.setOrgName(org.getOrgname());
user.setOrgGuid(org.getGuid());
}
// 设置登录时间和操作时间为当前时间
user.setLoginTime(new Date());
user.setLastActionTime(user.getLoginTime());
// 若存在权限信息
if (sysUser.getRoleguid() != null) {
//设置角色id到登录信息中
user.setRoleguid(sysUser.getRoleguid());
// 根据roleguid去sys_role表获取权限信息
SysRole role = sysRoleMapper.selectByPrimaryKey(sysUser.getRoleguid());
// 以“-”为分界将权限菜单信息(对应sys_menu表的guid)放入menuGuids数组列表
if(role == null) {
throw new AuthException("未查询到当前角色权限");
}
String[] menuGuids = role.getRolemenu().split("-");
Example mexample = new Example(SysMenu.class);
mexample.createCriteria().andIn("guid", Arrays.asList(menuGuids));
// 根据sys_menu表的guid数组集合查询对应的菜单信息列表
List<SysMenu> sysMenus = sysMenuMapper.selectByExample(mexample);
List<String> menusStrs = new ArrayList<>();
for (SysMenu menu : sysMenus) {
// 操作目录添加到menusStrs列表中
menusStrs.add(menu.getMenudir());
log.info("123");
}
user.setMenus(menusStrs);
} else {
user.setMenus(new ArrayList());
}
user.setRealName(sysUser.getRealname());
guiContext.setLoginUser(user);
guiContext.setLoginingFlag(false);
//通用分组
//guiContext.setCommongroupcode();
try {
//停掉指纹和ID卡登录线程
// 查询所有柜子
List<Cabinet> cabinets = cabinetService.queryAllCabinet();
guiContext.setCabinets(cabinets);
log.info("开始初始化硬件");
// 初始化RFID
guiContext.InitRfid();
String checkis=hcgOptionsMapper.getHcgOptionValue("ischeck");
settingPage.setZwCheck(checkis);
DatUserLog datUserLog=new DatUserLog();
datUserLog.setGuid(IdUtils.getUUID());
datUserLog.setLogintime(DateTime.now());
datUserLog.setDeptguid(org.getGuid());
datUserLog.setDeptname(org.getOrgname());
datUserLog.setLogintype("login");
datUserLog.setStoreguid(guiContext.getStoreguid());
datUserLog.setStorename(guiContext.getStorename());
datUserLog.setUsercode(sysUser.getLoginuser());
datUserLog.setUsername(sysUser.getRealname());
datUserLogMapper.insert(datUserLog);
} catch (Exception e) {
log.info("开始初始化硬件失败"+e.getMessage());
e.printStackTrace();
throw new RuntimeException(e.getLocalizedMessage());
}
}
@Autowired
private HcgOptionsMapper hcgOptionsMapper;
@Autowired
private SettingPage settingPage;
/**
* 登出类
*/
public void logout() {
guiContext.setLoginUser(null);
Platform.exit();
}
/**
* 回调时候使用的登录类用于
* 刷卡指纹等
*
* @param param
*
* @return
*
* @throws RuntimeException
*/
@Override
public Void call(Pair<String, String> param) throws RuntimeException {
// authenticator.call(new Pair(this.txUserName.getText(), this.txPassword.getText()));
LoginUser user = new LoginUser();
user.setPassword(param.getValue());
user.setUserName(param.getKey());
login(user);
return null;
}
/**
* 检查登录状态 未登录抛出authexception
*
* @Throws authexception
*/
public void checkLoginWithRefresh() {
if (guiContext.getLoginUser() == null) {
throw new AuthException("请登录");
}
//获取操作时间
Date actionTime = guiContext.getLoginUser().getLastActionTime();
//获取当前时间
Long current = System.currentTimeMillis();
//当前时间减去上次操作的时间,单位毫秒再除1000获得以秒为单位的浪费时间
Long wastTime = current / 1000 - actionTime.getTime() / 1000;
// Long wastTime=current-actionTime.getTime();
//若浪费的时间大于设置的超时时间,则打印超时信息,并抛出超时异常
// if (wastTime > fxConfigure.getTimout()) {
// log.debug(guiContext.getLoginUser().getUserName() + " timouted");
// guiContext.setLoginUser(null);
//
// throw new AuthException("登录超时,请重新登录");
// }
}
/**
* 检查登录状态
*/
public void checkLogin() {
if (guiContext.getLoginUser() == null) {
throw new AuthException("请登录");
}
}
/**
* 刷新登录状态
*/
public void updateAction() {
if (guiContext.getLoginUser() == null) {
// throw new AuthException("请登录");
return;
}
guiContext.getLoginUser().setLastActionTime(new Date());
}
/**
* 检查菜单授权
*
* @param menuCode
*
* @return
*/
public boolean menusExist(String menuCode) {
//checkLoginWithRefresh();
if (guiContext.getLoginUser().getMenus() == null) {
return false;
}
return guiContext.getLoginUser().getMenus().contains(menuCode);
}
/**
* 刷卡开门并且登录的情况
*
* @param cardId
* @param cabinet
*
* @return
*/
public void cardLogin(String cardId, Cabinet cabinet) {
Example example = new Example(SysUser.class);
example.createCriteria().andEqualTo("cardid", cardId);
List<SysUser> users = sysUserMapper.selectByExample(example);
log.info("刷卡开门登录:"+cardId);
if (users.size() == 0) {//用户都没有不能开门
throw new AuthException("未发现该卡信息!");
}
SysUser user = users.get(0);
//需要检查用户是否对柜子有权限吗
if (cabinet.getGroupcode().equals(fxConfigure.getCabinetGroupCode())) {
throw new AuthException("该卡无此耗材柜使用权限!");
}
LoginUser loginUser = new LoginUser();
loginUser.setUserName(user.getLoginuser());
loginUser.setPassword(user.getPwd());
if (guiContext.getLoginUser() == null) {//需要登录
this.login(loginUser);
return;
}
if (!guiContext.getLoginUser().getUserName().equals(loginUser.getUserName())) { //非当前登陆用户
this.logout();
this.login(loginUser);
return;
}
updateAction();//当前登陆用户更新信息
}
}