Commit 1659edf8cfa82313344a6bf873b501e7e9b54dac

Authored by 云中非
1 parent 2d2ba5a2

refactor: 第三方登录接口

1、参数由openid改为code
2、后台先调用小程序接口获取openid
3、再用openid获取平台用户信息
... ... @@ -5,6 +5,8 @@ import io.swagger.annotations.Api;
5 5 import io.swagger.annotations.ApiOperation;
6 6 import lombok.RequiredArgsConstructor;
7 7 import org.jetbrains.annotations.NotNull;
  8 +import org.springframework.http.HttpStatus;
  9 +import org.springframework.http.ResponseEntity;
8 10 import org.springframework.validation.annotation.Validated;
9 11 import org.springframework.web.bind.annotation.*;
10 12 import org.thingsboard.server.common.data.StringUtils;
... ... @@ -76,11 +78,15 @@ public class YtThirdPlatformController extends BaseController {
76 78 return thirdService.unbindUser(getCurrentUser().getCurrentTenantId(), dto.getAppUserId(),dto.getThirdUserId());
77 79 }
78 80
79   - @GetMapping("login/{thirdId}")
  81 + @GetMapping("login/{loginCode}")
80 82 @ApiOperation("第三方登录")
81   - public JwtTokenPair login(@PathVariable("thirdId") String thirdId)
  83 + public JwtTokenPair login(@PathVariable("loginCode") String loginCode)
82 84 throws ThingsboardException {
83   - UserDTO userDto = thirdService.login(thirdId);
  85 + String thirdUserId = thirdService.thirdLogin(loginCode);
  86 + UserDTO userDto = thirdService.login(thirdUserId);
  87 + if(userDto == null){
  88 + return new JwtTokenPair("", thirdUserId);
  89 + }
84 90 return buildJwtToken(userDto);
85 91 }
86 92
... ...
... ... @@ -61,6 +61,7 @@ public enum ErrorMessage {
61 61 DEVICE_USED_SCENE_REACT(400041,"场景联动【%s】正在使用该设备"),
62 62 SCENE_REACT_USED_ALARM_PROFILE(400042,"场景联动正在使用该告警配置"),
63 63 APP_USER_BINDED(400043,"平台用户【%s】已被其它账号绑定"),
  64 + THIRD_PLATFORM_EXCEPTION(400044,"【%s】,第三方异常【%s】"),
64 65 HAVE_NO_PERMISSION(500002,"没有修改权限");
65 66 private final int code;
66 67 private String message;
... ...
... ... @@ -6,12 +6,15 @@ 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.http.ResponseEntity;
9 10 import org.springframework.security.authentication.BadCredentialsException;
10 11 import org.springframework.security.core.userdetails.UsernameNotFoundException;
11 12 import org.springframework.security.crypto.password.PasswordEncoder;
12 13 import org.springframework.stereotype.Service;
13 14 import org.springframework.transaction.annotation.Transactional;
  15 +import org.springframework.web.client.RestTemplate;
14 16 import org.thingsboard.server.common.data.StringUtils;
  17 +import org.thingsboard.server.common.data.yunteng.config.third.WechatProperties;
15 18 import org.thingsboard.server.common.data.yunteng.core.cache.CacheUtils;
16 19 import org.thingsboard.server.common.data.yunteng.core.exception.YtDataValidationException;
17 20 import org.thingsboard.server.common.data.yunteng.core.message.ErrorMessage;
... ... @@ -23,6 +26,7 @@ import org.thingsboard.server.common.data.yunteng.dto.request.CodeTTL;
23 26 import org.thingsboard.server.common.data.yunteng.enums.MessageTypeEnum;
24 27 import org.thingsboard.server.common.data.yunteng.enums.MsgTemplatePurposeEnum;
25 28 import org.thingsboard.server.common.data.yunteng.enums.ThirdPlatformEnum;
  29 +import org.thingsboard.server.common.data.yunteng.utils.JacksonUtil;
26 30 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
27 31 import org.thingsboard.server.dao.yunteng.entities.User;
28 32 import org.thingsboard.server.dao.yunteng.entities.YtThirdUserEntity;
... ... @@ -47,6 +51,9 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf
47 51 private final CacheUtils cacheUtils;
48 52 private final PasswordEncoder passwordEncoder;
49 53
  54 + private final RestTemplate restTemplate = new RestTemplate();
  55 + private final WechatProperties wechatProperties;
  56 +
50 57 @Override
51 58 public YtPageData<YtThirdUserDTO> pageDatas(IPage<YtThirdUserEntity> pageInfrom, ThirdPlatformEnum platformName, String name) {
52 59 Wrapper pageFilter = new QueryWrapper<YtThirdUserEntity>()
... ... @@ -171,6 +178,19 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf
171 178 return baseMapper.delete(filter) > 0;
172 179 }
173 180
  181 + private static final String loginApi="/sns/jscode2session?appid={appid}&secret={appid}&js_code={appid}&grant_type=authorization_code";
  182 + @Override
  183 + public String thirdLogin(String loginCode) {
  184 + String response = restTemplate.getForObject(wechatProperties.getUrl()+loginApi,String.class, wechatProperties.getAppId(),wechatProperties.getAppSecret(),loginCode);
  185 + JsonNode result = JacksonUtil.toJsonNode(response);
  186 +
  187 + if(result.has("errcode")){
  188 + throw new YtDataValidationException(String.format(ErrorMessage.THIRD_PLATFORM_EXCEPTION.getMessage(),ThirdPlatformEnum.WECHAT,result.get("errmsg").asText()));
  189 + }
  190 + String thirdUserId = result.get("openid").asText();
  191 + return thirdUserId;
  192 + }
  193 +
174 194 @Override
175 195 public UserDTO login(String thirdUserId) {
176 196 User user = baseMapper.login(thirdUserId);
... ... @@ -186,4 +206,7 @@ public class YtThirdPlatformServiceImpl extends AbstractBaseService<YtThirdPlatf
186 206 public JsonNode message(String receiver, JsonNode message) {
187 207 return null;
188 208 }
  209 +
  210 +
  211 +
189 212 }
... ...
... ... @@ -44,6 +44,14 @@ public interface YtThirdPlatformService extends BaseService<YtThirdUserEntity> {
44 44 /**
45 45 * 第三方登录
46 46 *
  47 + * @param loginCode
  48 + * @return
  49 + */
  50 + String thirdLogin(String loginCode);
  51 +
  52 + /**
  53 + * 第三方登录
  54 + *
47 55 * @param thirdUserId
48 56 * @return
49 57 */
... ...