commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.modular.monitor.service.impl; |
|
26 |
|
|
27 |
import cn.hutool.core.bean.BeanUtil; |
|
28 |
import cn.hutool.core.collection.CollectionUtil; |
|
29 |
import cn.hutool.core.util.ObjectUtil; |
|
30 |
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
31 |
import org.springframework.stereotype.Service; |
|
32 |
import vip.xiaonuo.core.context.constant.ConstantContextHolder; |
|
33 |
import vip.xiaonuo.core.context.login.LoginContextHolder; |
|
34 |
import vip.xiaonuo.core.exception.DemoException; |
|
35 |
import vip.xiaonuo.core.factory.PageFactory; |
|
36 |
import vip.xiaonuo.core.pojo.login.SysLoginUser; |
|
37 |
import vip.xiaonuo.core.pojo.page.PageResult; |
|
38 |
import vip.xiaonuo.core.util.PageUtil; |
|
39 |
import vip.xiaonuo.sys.core.cache.UserCache; |
|
40 |
import vip.xiaonuo.sys.core.log.LogManager; |
|
41 |
import vip.xiaonuo.sys.modular.monitor.param.SysOnlineUserParam; |
|
42 |
import vip.xiaonuo.sys.modular.monitor.result.SysOnlineUserResult; |
|
43 |
import vip.xiaonuo.sys.modular.monitor.service.SysOnlineUserService; |
|
44 |
|
|
45 |
import javax.annotation.Resource; |
|
46 |
import java.util.Comparator; |
|
47 |
import java.util.List; |
|
48 |
import java.util.Map; |
|
49 |
import java.util.stream.Collectors; |
|
50 |
|
|
51 |
/** |
|
52 |
* 系统组织机构service接口实现类 |
|
53 |
* |
|
54 |
* @author xuyuxiang |
|
55 |
* @date 2020/4/7 17:06 |
|
56 |
*/ |
|
57 |
@Service |
|
58 |
public class SysOnlineUserServiceImpl implements SysOnlineUserService { |
|
59 |
|
|
60 |
@Resource |
|
61 |
private UserCache userCache; |
|
62 |
|
|
63 |
@Override |
|
64 |
public PageResult<SysOnlineUserResult> list(SysOnlineUserParam sysOnlineUserParam) { |
|
65 |
List<SysOnlineUserResult> tempList = CollectionUtil.newArrayList(); |
|
66 |
// 获取缓存中的所有用户 |
|
67 |
Map<String, SysLoginUser> allKeyValues = userCache.getAllKeyValues(); |
|
68 |
for (Map.Entry<String, SysLoginUser> sysLoginUserEntry : allKeyValues.entrySet()) { |
|
69 |
SysOnlineUserResult sysOnlineUserResult = new SysOnlineUserResult(); |
|
70 |
sysOnlineUserResult.setSessionId(sysLoginUserEntry.getKey()); |
|
71 |
BeanUtil.copyProperties(sysLoginUserEntry.getValue(), sysOnlineUserResult); |
|
72 |
tempList.add(sysOnlineUserResult); |
|
73 |
} |
|
74 |
List<SysOnlineUserResult> listAll = tempList.stream() |
|
75 |
.sorted(Comparator.comparing(SysOnlineUserResult::getLastLoginTime, Comparator.reverseOrder())) |
|
76 |
.collect(Collectors.toList()); |
|
77 |
Page<SysOnlineUserResult> page = PageFactory.defaultPage(); |
|
78 |
page.setTotal(tempList.size()); |
|
79 |
List<SysOnlineUserResult> resultList = PageUtil.page(page, listAll); |
|
80 |
return new PageResult<>(page, resultList); |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public void forceExist(SysOnlineUserParam sysOnlineUserParam) { |
|
85 |
Boolean demoEnvFlag = ConstantContextHolder.getDemoEnvFlag(); |
|
86 |
if (demoEnvFlag) { |
|
87 |
throw new DemoException(); |
|
88 |
} |
|
89 |
|
|
90 |
//获取缓存的key |
|
91 |
String redisLoginUserKey = sysOnlineUserParam.getSessionId(); |
|
92 |
|
|
93 |
//获取缓存的用户 |
|
94 |
SysLoginUser sysLoginUser = userCache.get(redisLoginUserKey); |
|
95 |
|
|
96 |
//如果缓存的用户存在,清除会话,否则表示该会话信息已失效,不执行任何操作 |
|
97 |
if (ObjectUtil.isNotNull(sysLoginUser)) { |
|
98 |
|
|
99 |
//清除登录会话 |
|
100 |
userCache.remove(redisLoginUserKey); |
|
101 |
|
|
102 |
//获取登录用户的账户信息 |
|
103 |
String account = LoginContextHolder.me().getSysLoginUserAccount(); |
|
104 |
|
|
105 |
//创建退出登录日志 |
|
106 |
LogManager.me().executeExitLog(account); |
|
107 |
} |
|
108 |
} |
|
109 |
} |