...
|
...
|
@@ -6,26 +6,18 @@ import com.baomidou.mybatisplus.core.metadata.IPage; |
6
|
6
|
import com.fasterxml.jackson.databind.JsonNode;
|
7
|
7
|
import lombok.RequiredArgsConstructor;
|
8
|
8
|
import lombok.extern.slf4j.Slf4j;
|
9
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
10
|
9
|
import org.springframework.security.authentication.BadCredentialsException;
|
11
|
|
-import org.springframework.security.authentication.InsufficientAuthenticationException;
|
12
|
|
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
13
|
10
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
14
|
11
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
15
|
12
|
import org.springframework.stereotype.Service;
|
16
|
13
|
import org.springframework.transaction.annotation.Transactional;
|
17
|
14
|
import org.thingsboard.server.common.data.StringUtils;
|
18
|
|
-import org.thingsboard.server.common.data.audit.ActionType;
|
19
|
|
-import org.thingsboard.server.common.data.id.EntityId;
|
20
|
|
-import org.thingsboard.server.common.data.id.TenantId;
|
21
|
|
-import org.thingsboard.server.common.data.id.UserId;
|
22
|
|
-import org.thingsboard.server.common.data.security.Authority;
|
23
|
15
|
import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils;
|
24
|
16
|
import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException;
|
25
|
17
|
import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
|
26
|
18
|
import org.thingsboard.server.common.data.yunteng.dto.DeleteDTO;
|
|
19
|
+import org.thingsboard.server.common.data.yunteng.dto.UserDTO;
|
27
|
20
|
import org.thingsboard.server.common.data.yunteng.dto.UserDetailsDTO;
|
28
|
|
-import org.thingsboard.server.common.data.yunteng.dto.YtOpinionDTO;
|
29
|
21
|
import org.thingsboard.server.common.data.yunteng.dto.YtThirdUserDTO;
|
30
|
22
|
import org.thingsboard.server.common.data.yunteng.dto.request.CodeTTL;
|
31
|
23
|
import org.thingsboard.server.common.data.yunteng.enums.MessageTypeEnum;
|
...
|
...
|
@@ -33,17 +25,14 @@ import org.thingsboard.server.common.data.yunteng.enums.MsgTemplatePurposeEnum; |
33
|
25
|
import org.thingsboard.server.common.data.yunteng.enums.ThirdPlatformEnum;
|
34
|
26
|
import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
|
35
|
27
|
import org.thingsboard.server.dao.yunteng.entities.User;
|
36
|
|
-import org.thingsboard.server.dao.yunteng.entities.YtOpinionEntity;
|
37
|
28
|
import org.thingsboard.server.dao.yunteng.entities.YtThirdUserEntity;
|
38
|
29
|
import org.thingsboard.server.dao.yunteng.mapper.UserMapper;
|
39
|
|
-import org.thingsboard.server.dao.yunteng.mapper.YtOpinionMapper;
|
40
|
30
|
import org.thingsboard.server.dao.yunteng.mapper.YtThirdPlatformMapper;
|
41
|
|
-import org.thingsboard.server.dao.yunteng.service.*;
|
|
31
|
+import org.thingsboard.server.dao.yunteng.service.AbstractBaseService;
|
|
32
|
+import org.thingsboard.server.dao.yunteng.service.YtThirdPlatformService;
|
42
|
33
|
|
43
|
|
-import java.util.List;
|
44
|
34
|
import java.util.Objects;
|
45
|
35
|
import java.util.Optional;
|
46
|
|
-import java.util.UUID;
|
47
|
36
|
|
48
|
37
|
import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstants.CacheConfigKey.MOBILE_LOGIN_SMS_CODE;
|
49
|
38
|
import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstants.DEFAULT_DELIMITER;
|
...
|
...
|
@@ -52,140 +41,143 @@ import static org.thingsboard.server.common.data.yunteng.constant.FastIotConstan |
52
|
41
|
@Service
|
53
|
42
|
@RequiredArgsConstructor
|
54
|
43
|
public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatformMapper, YtThirdUserEntity>
|
55
|
|
- implements YtThirdPlatformService {
|
56
|
|
-
|
57
|
|
- private final UserMapper userMapper;
|
58
|
|
- private final CacheUtils cacheUtils;
|
59
|
|
- private final PasswordEncoder passwordEncoder;
|
60
|
|
- @Override
|
61
|
|
- public YtPageData<YtThirdUserDTO> pageDatas(IPage<YtThirdUserEntity> pageInfrom, ThirdPlatformEnum platformName, String name) {
|
62
|
|
- Wrapper pageFilter = new QueryWrapper<YtThirdUserEntity>()
|
63
|
|
- .lambda()
|
64
|
|
- .eq(pageInfrom != null,YtThirdUserEntity::getPlatformName,platformName)
|
65
|
|
- .like(StringUtils.isNotEmpty(name),YtThirdUserEntity::getNickName,name);
|
66
|
|
- IPage<YtThirdUserEntity> page = baseMapper.selectPage(pageInfrom, pageFilter);
|
67
|
|
- return getPageData(page, YtThirdUserDTO.class);
|
68
|
|
- }
|
69
|
|
-
|
70
|
|
-
|
71
|
|
-
|
72
|
|
-
|
73
|
|
-
|
74
|
|
- @Override
|
75
|
|
- @Transactional(rollbackFor=Exception.class)
|
76
|
|
- public String saveOrUpdate(YtThirdUserDTO dto) {
|
77
|
|
- User user = null;
|
78
|
|
- switch (dto.getLoginMethod()){
|
79
|
|
- case PHONE:
|
80
|
|
- user = checkPhoneCode(dto.getAppUserKey(),dto.getAppUserSecret());
|
81
|
|
- break;
|
82
|
|
- case ACCOUNT:
|
83
|
|
- user = checkUsernamePassword(dto.getAppUserKey(),dto.getAppUserSecret());
|
84
|
|
- break;
|
|
44
|
+ implements YtThirdPlatformService {
|
|
45
|
+
|
|
46
|
+ private final UserMapper userMapper;
|
|
47
|
+ private final CacheUtils cacheUtils;
|
|
48
|
+ private final PasswordEncoder passwordEncoder;
|
|
49
|
+
|
|
50
|
+ @Override
|
|
51
|
+ public YtPageData<YtThirdUserDTO> pageDatas(IPage<YtThirdUserEntity> pageInfrom, ThirdPlatformEnum platformName, String name) {
|
|
52
|
+ Wrapper pageFilter = new QueryWrapper<YtThirdUserEntity>()
|
|
53
|
+ .lambda()
|
|
54
|
+ .eq(pageInfrom != null, YtThirdUserEntity::getPlatformName, platformName)
|
|
55
|
+ .like(StringUtils.isNotEmpty(name), YtThirdUserEntity::getNickName, name);
|
|
56
|
+ IPage<YtThirdUserEntity> page = baseMapper.selectPage(pageInfrom, pageFilter);
|
|
57
|
+ return getPageData(page, YtThirdUserDTO.class);
|
85
|
58
|
}
|
86
|
|
- if(user == null){
|
87
|
|
- throw new YtDataValidationException(ErrorMessage.USER_NOT_EXISTS.getMessage());
|
88
|
|
- }
|
89
|
|
- if(StringUtils.isEmpty(user.getAvatar()) && StringUtils.isNotEmpty(dto.getAvatarUrl())){
|
90
|
|
- user.setAvatar(dto.getAvatarUrl());
|
91
|
|
- userMapper.updateById(user);
|
92
|
|
- }
|
93
|
|
- Wrapper filter = new QueryWrapper<YtThirdUserEntity>().lambda()
|
94
|
|
- .eq(YtThirdUserEntity::getThirdUserId,dto.getThirdUserId());
|
95
|
|
- YtThirdUserEntity oldVideo = baseMapper.selectOne(filter);
|
96
|
|
- if (null == oldVideo) {
|
97
|
|
- baseMapper.insert(dto.getEntity(YtThirdUserEntity.class));
|
98
|
|
- }else {
|
99
|
|
- dto.setId(oldVideo.getId());
|
100
|
|
- baseMapper.updateById(dto.getEntity(YtThirdUserEntity.class));
|
101
|
|
- }
|
102
|
|
- return user.getTbUser();
|
103
|
|
- }
|
104
|
|
-
|
105
|
|
- /**
|
106
|
|
- * 验证用户名和密码有效性
|
107
|
|
- * @param key 用户名
|
108
|
|
- * @param secret 密码
|
109
|
|
- * @return
|
110
|
|
- */
|
111
|
|
- private User checkUsernamePassword(String key,String secret){
|
112
|
|
- Wrapper filter = new QueryWrapper<User>()
|
113
|
|
- .lambda()
|
114
|
|
- .eq(User::getUsername,key);
|
115
|
|
- User user = userMapper.selectOne(filter);
|
116
|
|
-
|
117
|
|
- if (user == null) {
|
118
|
|
- throw new UsernameNotFoundException("User not found: " + key);
|
119
|
|
- }
|
120
|
|
- if(!passwordEncoder.matches(secret, user.getPassword())){
|
121
|
|
- throw new BadCredentialsException("Authentication Failed. Username or Password not valid.");
|
122
|
|
- }
|
123
|
|
- return user;
|
124
|
|
- }
|
125
|
|
-
|
126
|
|
- /**
|
127
|
|
- * 验证手机号和验证码有效性
|
128
|
|
- * @param key 手机号
|
129
|
|
- * @param secret 验证码
|
130
|
|
- * @return
|
131
|
|
- */
|
132
|
|
- private User checkPhoneCode(String key,String secret){
|
133
|
|
- Wrapper filter = new QueryWrapper<User>()
|
134
|
|
- .lambda()
|
135
|
|
- .eq(User::getPhoneNumber,key);
|
136
|
|
- User users = userMapper.selectOne(filter);
|
137
|
|
- if (users == null) {
|
138
|
|
- throw new UsernameNotFoundException("phone number not found: " + key);
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+ @Override
|
|
62
|
+ @Transactional(rollbackFor = Exception.class)
|
|
63
|
+ public UserDTO saveOrUpdate(YtThirdUserDTO dto) {
|
|
64
|
+ User user = null;
|
|
65
|
+ switch (dto.getLoginMethod()) {
|
|
66
|
+ case PHONE:
|
|
67
|
+ user = checkPhoneCode(dto.getAppUserKey(), dto.getAppUserSecret());
|
|
68
|
+ break;
|
|
69
|
+ case ACCOUNT:
|
|
70
|
+ user = checkUsernamePassword(dto.getAppUserKey(), dto.getAppUserSecret());
|
|
71
|
+ break;
|
|
72
|
+ }
|
|
73
|
+ if (user == null) {
|
|
74
|
+ throw new YtDataValidationException(ErrorMessage.USER_NOT_EXISTS.getMessage());
|
|
75
|
+ }
|
|
76
|
+ if (StringUtils.isEmpty(user.getAvatar()) && StringUtils.isNotEmpty(dto.getAvatarUrl())) {
|
|
77
|
+ user.setAvatar(dto.getAvatarUrl());
|
|
78
|
+ userMapper.updateById(user);
|
|
79
|
+ }
|
|
80
|
+ dto.setAppUserId(user.getId());
|
|
81
|
+
|
|
82
|
+ Wrapper filter = new QueryWrapper<YtThirdUserEntity>().lambda()
|
|
83
|
+ .eq(YtThirdUserEntity::getThirdUserId, dto.getThirdUserId());
|
|
84
|
+ YtThirdUserEntity oldVideo = baseMapper.selectOne(filter);
|
|
85
|
+ if (null == oldVideo) {
|
|
86
|
+ baseMapper.insert(dto.getEntity(YtThirdUserEntity.class));
|
|
87
|
+ } else {
|
|
88
|
+ dto.setId(oldVideo.getId());
|
|
89
|
+ baseMapper.updateById(dto.getEntity(YtThirdUserEntity.class));
|
|
90
|
+ }
|
|
91
|
+ return user.getDTO(UserDTO.class);
|
139
|
92
|
}
|
140
|
|
- String cacheKey =
|
141
|
|
- MsgTemplatePurposeEnum.FOR_LOGIN.name()
|
142
|
|
- + DEFAULT_DELIMITER
|
143
|
|
- + MessageTypeEnum.PHONE_MESSAGE.name()
|
144
|
|
- + DEFAULT_DELIMITER
|
145
|
|
- + key;
|
146
|
|
-
|
147
|
|
- boolean correct =
|
148
|
|
- cacheUtils
|
149
|
|
- .get(MOBILE_LOGIN_SMS_CODE, cacheKey)
|
150
|
|
- .map(
|
151
|
|
- o -> {
|
152
|
|
- CodeTTL codeTTL = (CodeTTL) o;
|
153
|
|
- if (System.currentTimeMillis() - codeTTL.getSendTs() < 5 * 60 * 1000) {
|
154
|
|
- return Objects.equals(codeTTL.getCode(), secret);
|
155
|
|
- } else {
|
156
|
|
- return false;
|
157
|
|
- }
|
158
|
|
- })
|
159
|
|
- .orElse(false);
|
160
|
|
- Optional<UserDetailsDTO> optionalUser;
|
161
|
|
- if (!correct) {
|
162
|
|
- throw new BadCredentialsException("验证码不正确");
|
|
93
|
+
|
|
94
|
+ /**
|
|
95
|
+ * 验证用户名和密码有效性
|
|
96
|
+ *
|
|
97
|
+ * @param key 用户名
|
|
98
|
+ * @param secret 密码
|
|
99
|
+ * @return
|
|
100
|
+ */
|
|
101
|
+ private User checkUsernamePassword(String key, String secret) {
|
|
102
|
+ Wrapper filter = new QueryWrapper<User>()
|
|
103
|
+ .lambda()
|
|
104
|
+ .eq(User::getUsername, key)
|
|
105
|
+ .or(query -> query.eq(User::getEmail, key));
|
|
106
|
+ User user = userMapper.selectOne(filter);
|
|
107
|
+
|
|
108
|
+ if (user == null) {
|
|
109
|
+ throw new UsernameNotFoundException("User not found: " + key);
|
|
110
|
+ }
|
|
111
|
+ if (!passwordEncoder.matches(secret, user.getPassword())) {
|
|
112
|
+ throw new BadCredentialsException("Authentication Failed. Username or Password not valid.");
|
|
113
|
+ }
|
|
114
|
+ return user;
|
163
|
115
|
}
|
164
|
|
- return users;
|
165
|
|
- }
|
166
|
116
|
|
|
117
|
+ /**
|
|
118
|
+ * 验证手机号和验证码有效性
|
|
119
|
+ *
|
|
120
|
+ * @param key 手机号
|
|
121
|
+ * @param secret 验证码
|
|
122
|
+ * @return
|
|
123
|
+ */
|
|
124
|
+ private User checkPhoneCode(String key, String secret) {
|
|
125
|
+ Wrapper filter = new QueryWrapper<User>()
|
|
126
|
+ .lambda()
|
|
127
|
+ .eq(User::getPhoneNumber, key);
|
|
128
|
+ User users = userMapper.selectOne(filter);
|
|
129
|
+ if (users == null) {
|
|
130
|
+ throw new UsernameNotFoundException("phone number not found: " + key);
|
|
131
|
+ }
|
|
132
|
+ String cacheKey =
|
|
133
|
+ MsgTemplatePurposeEnum.FOR_LOGIN.name()
|
|
134
|
+ + DEFAULT_DELIMITER
|
|
135
|
+ + MessageTypeEnum.PHONE_MESSAGE.name()
|
|
136
|
+ + DEFAULT_DELIMITER
|
|
137
|
+ + key;
|
|
138
|
+
|
|
139
|
+ boolean correct =
|
|
140
|
+ cacheUtils
|
|
141
|
+ .get(MOBILE_LOGIN_SMS_CODE, cacheKey)
|
|
142
|
+ .map(
|
|
143
|
+ o -> {
|
|
144
|
+ CodeTTL codeTTL = (CodeTTL) o;
|
|
145
|
+ if (System.currentTimeMillis() - codeTTL.getSendTs() < 5 * 60 * 1000) {
|
|
146
|
+ return Objects.equals(codeTTL.getCode(), secret);
|
|
147
|
+ } else {
|
|
148
|
+ return false;
|
|
149
|
+ }
|
|
150
|
+ })
|
|
151
|
+ .orElse(false);
|
|
152
|
+ Optional<UserDetailsDTO> optionalUser;
|
|
153
|
+ if (!correct) {
|
|
154
|
+ throw new BadCredentialsException("验证码不正确");
|
|
155
|
+ }
|
|
156
|
+ return users;
|
|
157
|
+ }
|
167
|
158
|
|
168
|
159
|
|
169
|
|
- @Override
|
170
|
|
- @Transactional(rollbackFor=Exception.class)
|
171
|
|
- public boolean deleteDataByIds(DeleteDTO deleteDTO) {
|
172
|
|
- Wrapper filter = new QueryWrapper<YtThirdUserEntity>().lambda()
|
173
|
|
- .in(YtThirdUserEntity::getId,deleteDTO.getIds());
|
174
|
|
- return baseMapper.delete(filter) > 0;
|
175
|
|
- }
|
|
160
|
+ @Override
|
|
161
|
+ @Transactional(rollbackFor = Exception.class)
|
|
162
|
+ public boolean deleteDataByIds(DeleteDTO deleteDTO) {
|
|
163
|
+ Wrapper filter = new QueryWrapper<YtThirdUserEntity>().lambda()
|
|
164
|
+ .in(YtThirdUserEntity::getId, deleteDTO.getIds());
|
|
165
|
+ return baseMapper.delete(filter) > 0;
|
|
166
|
+ }
|
176
|
167
|
|
177
|
|
- @Override
|
178
|
|
- public String login(String thirdUserId) {
|
179
|
|
- return baseMapper.login(thirdUserId);
|
180
|
|
- }
|
|
168
|
+ @Override
|
|
169
|
+ public UserDTO login(String thirdUserId) {
|
|
170
|
+ return baseMapper.login(thirdUserId)
|
|
171
|
+ .getDTO(UserDTO.class);
|
|
172
|
+ }
|
181
|
173
|
|
182
|
|
- @Override
|
183
|
|
- public String accessToken(String appKey, String appSecret) {
|
184
|
|
- return null;
|
185
|
|
- }
|
|
174
|
+ @Override
|
|
175
|
+ public String accessToken(String appKey, String appSecret) {
|
|
176
|
+ return null;
|
|
177
|
+ }
|
186
|
178
|
|
187
|
|
- @Override
|
188
|
|
- public JsonNode message(String receiver, JsonNode message) {
|
189
|
|
- return null;
|
190
|
|
- }
|
|
179
|
+ @Override
|
|
180
|
+ public JsonNode message(String receiver, JsonNode message) {
|
|
181
|
+ return null;
|
|
182
|
+ }
|
191
|
183
|
} |
...
|
...
|
|