UserService.java
6.51 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
package cn.com.glsx.app.modules.service;
import cn.com.glsx.admin.common.constant.UserConstants;
import cn.com.glsx.app.common.enums.LoginType;
import cn.com.glsx.app.common.exception.AppServerException;
import cn.com.glsx.app.modules.entity.User;
import cn.com.glsx.app.modules.entity.UserThirdAuth;
import cn.com.glsx.app.modules.entity.UserWechatAccount;
import cn.com.glsx.app.modules.mapper.UserLocalAccountMapper;
import cn.com.glsx.app.modules.mapper.UserMapper;
import cn.com.glsx.app.modules.mapper.UserThirdAuthMapper;
import cn.com.glsx.app.modules.mapper.UserWechatAccountMapper;
import cn.com.glsx.app.modules.model.AppUser;
import cn.com.glsx.app.modules.model.CTerminalUserModel;
import cn.com.glsx.app.modules.model.param.UpdateWxUserInfoBO;
import cn.com.glsx.auth.utils.ShieldContextHolder;
import cn.hutool.core.lang.UUID;
import com.glsx.plat.common.utils.ObjectUtils;
import com.glsx.plat.jwt.base.ComJwtUser;
import com.glsx.plat.jwt.util.JwtUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
public class UserService {
@Autowired
private JwtUtils<ComJwtUser> jwtUtils;
@Resource
private UserMapper userMapper;
@Resource
private UserLocalAccountMapper userLocalAccountMapper;
@Resource
private UserThirdAuthMapper userThirdAuthMapper;
@Resource
private UserWechatAccountMapper userWechatAccountMapper;
/**
* 生成带用户信息的token
*
* @param user
* @return
*/
public String createToken(AppUser user) {
Assert.notNull(user, "获取当前用户信息失败");
String uuid = UUID.randomUUID().toString(); //JWT 随机ID,做为验证的key
String jwtId = jwtUtils.getApplication() + ":" + uuid + "_" + jwtUtils.JWT_SESSION_PREFIX + user.getUserId();
ComJwtUser jwtUser = new ComJwtUser();
jwtUser.setApplication(jwtUtils.getApplication());
jwtUser.setJwtId(jwtId);
jwtUser.setUserId(String.valueOf(user.getUserId()));
jwtUser.setAccount(user.getAccount());
jwtUser.setBelong(user.getUserType());
Map<String, Object> userMap = (Map<String, Object>) ObjectUtils.objectToMap(jwtUser);
return jwtUtils.createToken(userMap);
}
public AppUser getLocalUserByUserId(Long userId) {
//获取C端用户信息
return userLocalAccountMapper.selectLocalUserByUserId(userId);
}
public AppUser getRelUserByUserId(Long userId) {
//获取后台用户信息
return userLocalAccountMapper.selectRelUserByUserId(userId);
}
/**
* 获取c端用户id
*
* @return
*/
public Long getCTerminalUserId() {
if (UserConstants.RolePermitCastType.wxShared.getCode().equals(ShieldContextHolder.getRolePermissionType())) {
//如果是被分享的账号,直接返回当前用户id
return ShieldContextHolder.getUserId();
} else {
//如果是非被分享的账号,需要转换成c端用户id
User user = userMapper.selectByRelUserId(ShieldContextHolder.getUserId());
if (user == null) {
throw new AppServerException("数据权限错误");
}
return user.getId();
}
}
/**
* 获取本地C端账号用户信息
*
* @param userIds
* @return
*/
public List<CTerminalUserModel> getLocalUsers(Collection<Long> userIds) {
if (CollectionUtils.isEmpty(userIds)) {
return Lists.newArrayList();
}
return userLocalAccountMapper.selectByUserIds(userIds);
}
public Map<Long, CTerminalUserModel> getLocalUserMap(Collection<Long> userIds) {
if (CollectionUtils.isEmpty(userIds)) {
return Maps.newHashMap();
}
List<CTerminalUserModel> userModelList = this.getLocalUsers(userIds);
return userModelList.stream().collect(Collectors.toMap(CTerminalUserModel::getUserId, Function.identity()));
}
/**
* 获取后台账号用户信息
*
* @param relUserIds
* @return
*/
public List<CTerminalUserModel> getLocalRelUsers(Collection<Long> relUserIds) {
if (CollectionUtils.isEmpty(relUserIds)) {
return Lists.newArrayList();
}
return userLocalAccountMapper.selectByRelUserIds(relUserIds);
}
public Map<Long, CTerminalUserModel> getLocalRelUserMap(Collection<Long> relUserIds) {
if (CollectionUtils.isEmpty(relUserIds)) {
return Maps.newHashMap();
}
List<CTerminalUserModel> userModelList = this.getLocalRelUsers(relUserIds);
return userModelList.stream().filter(um -> um.getRelUserId() != null).collect(Collectors.toMap(CTerminalUserModel::getRelUserId, Function.identity()));
}
/**
* 获取微信账号用户信息
*
* @param userIds
* @return
*/
public List<CTerminalUserModel> getWechatUsers(Collection<Long> userIds) {
if (CollectionUtils.isEmpty(userIds)) {
return Lists.newArrayList();
}
return userWechatAccountMapper.selectByUserIds(userIds);
}
public Map<Long, CTerminalUserModel> getWechatUserMap(Collection<Long> userIds) {
if (CollectionUtils.isEmpty(userIds)) {
return Maps.newHashMap();
}
List<CTerminalUserModel> userModelList = this.getWechatUsers(userIds);
return userModelList.stream().collect(Collectors.toMap(CTerminalUserModel::getUserId, Function.identity()));
}
public void updateWxUserInfo(UpdateWxUserInfoBO userInfoBO) {
UserThirdAuth userThirdAuth = userThirdAuthMapper.selectByLoginTypeAndUserId(LoginType.WECHAT.getType(), getCTerminalUserId());
if (userThirdAuth != null) {
UserWechatAccount wechatAccount = userWechatAccountMapper.selectByAuthId(userThirdAuth.getId());
if (wechatAccount != null) {
wechatAccount.setWechatName(userInfoBO.getNickName());
wechatAccount.setAvatar(userInfoBO.getAvatarUrl());
wechatAccount.setUpdatedDate(new Date());
userWechatAccountMapper.updateByPrimaryKeySelective(wechatAccount);
}
}
}
}