LoginUserInfoUtils.java
6.14 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
package cn.com.glsx.stoassistant.utils;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.scrm.proxy.dto.merchant.FindDataPermissionRequest;
import cn.com.glsx.scrm.proxy.enums.UserTypeEnum;
import cn.com.glsx.scrm.proxy.remote.merchant.MerchantRemote;
import cn.com.glsx.scrm.system.dto.LoginUserDTO;
import cn.com.glsx.scrm.system.model.wechatapplets.LoginUserInfoResponse;
import org.apache.commons.collections.CollectionUtils;
import org.oreframework.boot.autoconfigure.rop.util.RopThreadLocal;
import org.oreframework.boot.security.cas.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author hedong
* @version V1.0
* @Title: LoginUserInfoUtils
* @Description: 获取登录用户,兼容运营平台与saas平台
* @Company: Didihu.com.cn
* @Date 2021/9/27 19:06
*/
@Component
public class LoginUserInfoUtils {
private static MerchantRemote merchantRemote;
@Autowired
public LoginUserInfoUtils(MerchantRemote merchantRemote) {
LoginUserInfoUtils.merchantRemote = merchantRemote;
}
public static LoginUserDTO getLoginUser(){
if(RopThreadLocal.getUser()!=null){
User user = RopThreadLocal.getUser();
return convertUserToLoginUserDTO(user);
}
LoginUserInfoResponse loginUser = RopThreadLocal.getLoginUser(LoginUserInfoResponse.class);
LoginUserDTO loginUserDTO = new LoginUserDTO();
if (loginUser != null) BeanUtils.copyProperties(loginUser,loginUserDTO);
return loginUserDTO;
}
public static String getUseruuid(){
LoginUserDTO loginUserDTO = getLoginUser();
if (loginUserDTO != null){
return loginUserDTO.getUuid();
}
return null;
}
public static Integer getMerchantLevel(){
LoginUserDTO loginUserDTO = getLoginUser();
if (loginUserDTO != null){
return loginUserDTO.getAccountLevel();
}
return null;
}
private static LoginUserDTO convertUserToLoginUserDTO(User user){
LoginUserDTO loginUserDTO = new LoginUserDTO();
loginUserDTO.setAccountName(user.getRealname());
loginUserDTO.setId(user.getId());
loginUserDTO.setAdmin(user.getIsadmin()==1);
loginUserDTO.setPhone(user.getMobile());
return loginUserDTO;
}
public static String getAccountName(){
return getLoginUser()==null?"": getLoginUser().getAccountName();
}
public static Integer getMerchantId(){
LoginUserDTO loginUserDTO = getLoginUser();
if (loginUserDTO != null){
return loginUserDTO.getMerchantId();
}
return null;
}
public static String getMerchantName(){
return getLoginUser().getMerchantName();
}
public static Boolean getAdmin() {
LoginUserDTO loginUserDTO = getLoginUser();
if (loginUserDTO != null){
return loginUserDTO.getAdmin() == null ? false : loginUserDTO.getAdmin();
}
return false;
}
public static Integer getChannel(){
/*LoginUserDTO loginUserDTO = getLoginUser();
if (loginUserDTO != null){
return loginUserDTO.getChannelType();
}*/
// 默认是2,则不需要查询开通明细。1则需要查询开通明细
return 2;
}
public static Integer convertAccountLevel(Integer accountLevel,Integer merchantId){
if (accountLevel == null){
// 如果是超管,则默认查询全部
if (LoginUserInfoUtils.getAdmin()){
return null;
}else {
// 如果不是超管,则默认查询当前门店以及其下级门店的订单
return LoginUserInfoUtils.getMerchantLevel();
}
}else if (!LoginUserInfoUtils.merchantPermission(merchantId)){
return LoginUserInfoUtils.getMerchantLevel();
}
return accountLevel;
}
public static Integer convertMerchantId(Integer merchantId){
if (merchantId == null){
// 如果是超管,则默认查询全部
if (LoginUserInfoUtils.getAdmin()){
return null;
}else {
// 如果不是超管,则默认查询当前门店以及其下级门店的订单
return LoginUserInfoUtils.getMerchantId();
}
}else if (!LoginUserInfoUtils.merchantPermission(merchantId)){
return LoginUserInfoUtils.getMerchantId();
}
return merchantId;
}
public static boolean merchantPermission(Integer merchantId){
if (merchantId.equals(getMerchantId()) || getAdmin()){
return true;
}
List<Integer> merchantIds = getPermissionMerchantIds();
if (CollectionUtils.isNotEmpty(merchantIds) && merchantIds.contains(merchantId)){
return true;
}
return false;
}
public static List<Integer> getPermissionMerchantIds(){
if (getMerchantId() == null){
return null;
}
FindDataPermissionRequest request = new FindDataPermissionRequest();
request.setMerchantId(getMerchantId());
request.setUserType(UserTypeEnum.MERCHANT.getValue());
RpcResponse<List<Integer>> response = merchantRemote.findDataPermission(request);
if (CollectionUtils.isNotEmpty(response.getResult())){
return response.getResult();
}
return null;
}
public static List<Integer> getPermissionMerchantIdsByMerchantId(Integer merchantId){
if (merchantId == null){
return null;
}
FindDataPermissionRequest request = new FindDataPermissionRequest();
request.setMerchantId(merchantId);
request.setUserType(UserTypeEnum.MERCHANT.getValue());
RpcResponse<List<Integer>> response = merchantRemote.findDataPermission(request);
if (CollectionUtils.isNotEmpty(response.getResult())){
return response.getResult();
}
return null;
}
public static LoginUserDTO getLoginUserDTO(){
return RopThreadLocal.getLoginUser(LoginUserDTO.class);
}
}