UserUtils.java
3.65 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
package com.glsx.common.utils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.constants.Constants;
import com.glsx.common.constants.ResultCodeEnum;
import com.glsx.common.feign.client.UserClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* @author zhanghj
* @version 1.0.0
* @Title UserUtils.java
* @Package com.glsx.common.utils
* @Description TODO 获取当前用户工具类
* @date 2019年2月12日 上午11:35:30
*/
@Component
public class UserUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(UserUtils.class.getName());
private static UserClient userClient;
public static String getUserName(HttpServletRequest request) {
HttpSession session = request.getSession();
String username = (String) session.getAttribute(Constants.SESSION_USERNAME_KEY);
return username;
}
public static Integer getUserId(HttpServletRequest request) {
HttpSession session = request.getSession();
Integer userId = (Integer) session.getAttribute(Constants.SESSION_USERID_KEY);
return userId;
}
public static JSONObject getUserInfo(Integer id) {
JSONObject respObj = userClient.getUserById(id);
JSONObject userObj;
if (respObj != null
&& respObj.getIntValue("code") == ResultCodeEnum.SUCCESS.getCode()) {
userObj = respObj.getJSONObject("data");
} else {
userObj = new JSONObject();
}
return userObj;
}
/**
* 获取当前登录用户对象
*
* @return 用户对象
*/
public static JSONObject getCurrentUser() {
JSONObject userObj;
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
userObj = getUserInfo(getUserId(request));
} else {
userObj = new JSONObject();
}
return userObj;
}
/**
* 获取当前登录用户ID
*
* @return 用户ID
*/
public static Integer getCurrentUserId() {
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return (attributes != null) ? getUserId(attributes.getRequest()) : null;
}
/**
* 获取当前登录用户角色
*
* @return 用户角色
*/
public static Byte getCurrentUserType() {
return getCurrentUser().getByte("userType");
}
/**
* 根据用户角色代码转换成文本
*
* @param userType 用户角色
* @return 角色名
*/
public static String convertUserTypeName(Byte userType) {
Map<Byte, String> userTypeMap = new HashMap<>(4);
userTypeMap.put((byte) 0, "平台运营");
userTypeMap.put((byte) 1, "服务商");
userTypeMap.put((byte) 2, "商户");
userTypeMap.put((byte) 3, "技工");
return userTypeMap.get(userType);
}
@Autowired
public void setUserClient(UserClient userClient) {
UserUtils.userClient = userClient;
}
}