AdvertisementUserController.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.cyksj.web.controller.advertisement;
  2. import com.cyksj.common.exception.BusinessRuntimeException;
  3. import com.cyksj.common.snowflake.Sequence;
  4. import com.cyksj.common.util.Codec;
  5. import com.cyksj.common.util.Jsons;
  6. import com.cyksj.dto.Result;
  7. import com.cyksj.enums.GatewayResponse;
  8. import com.cyksj.enums.WeChatAuthRedirectUrl;
  9. import com.cyksj.model.dto.AuthRedirect;
  10. import com.cyksj.model.dto.AuthToken;
  11. import com.cyksj.model.dto.UserWhoami;
  12. import com.cyksj.model.entity.AdvertisementUser;
  13. import com.cyksj.model.request.MiniProgramLoginPost;
  14. import com.cyksj.redis.RedisService;
  15. import com.cyksj.service.advertisement.AdvertisementUserService;
  16. import com.cyksj.service.wechat.WeChatService;
  17. import com.cyksj.web.util.StpUserUtil;
  18. import lombok.RequiredArgsConstructor;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import org.springframework.web.bind.annotation.*;
  24. import javax.servlet.http.Cookie;
  25. import javax.servlet.http.HttpServletResponse;
  26. /**
  27. * @author chan
  28. * @date 2023/5/16 11:49 AM
  29. */
  30. @Slf4j
  31. @RequiredArgsConstructor
  32. @RestController
  33. @RequestMapping("/applets/advertisement/user")
  34. public class AdvertisementUserController {
  35. @Value("${advertisementwxapp.notify}")
  36. private String notify;
  37. @Value("${advertisementwxapp.appId}")
  38. private String appId;
  39. private final static Sequence SEQUENCE = new Sequence(0);
  40. private final AdvertisementUserService advertisementUserService;
  41. private final RedisService redisService;
  42. private final WeChatService weChatService;
  43. /**
  44. * 小程序静默授权
  45. */
  46. @PostMapping(value = "/login")
  47. @Transactional(rollbackFor = Exception.class)
  48. public Result<String> wxlogin(@RequestBody MiniProgramLoginPost miniDataParams) throws Exception{
  49. String methodName = "小程序静默授权";
  50. log.info("{}[start] params:{}",methodName,miniDataParams);
  51. if (StringUtils.isBlank(miniDataParams.getCode())) {
  52. throw BusinessRuntimeException.getInstance("需要登录凭证code.");
  53. }
  54. final String code = miniDataParams.getCode();
  55. // 用户token
  56. AdvertisementUser user = advertisementUserService.wxlogin(code,miniDataParams.getAppId());
  57. StpUserUtil.login(user.getId());
  58. log.info("{}[end] uuid:{}",methodName, user.getId());
  59. return GatewayResponse.SUCCESS.newBuilder().toResult(StpUserUtil.getTokenValue());
  60. }
  61. @GetMapping(value = "/whoami")
  62. public Result<UserWhoami> whoami(){
  63. AdvertisementUser user = advertisementUserService.getById(StpUserUtil.getLoginIdAsLong());
  64. UserWhoami userWhoami = advertisementUserService.whoami(user.getId());
  65. return GatewayResponse.SUCCESS.newBuilder().toResult(userWhoami);
  66. }
  67. @GetMapping(value = "/weChat")
  68. public void base(String url, String authType, HttpServletResponse response) throws Exception {
  69. log.info("微信授权转跳. url: {},authType:{}",url,authType);
  70. // 无论是否为老用户 均转跳到授权页面
  71. // 若授权过 则无需再次授权 直接回调 /user/wechat/redirect
  72. // 新用户同意授权后 也会回调 /user/wechat/redirect
  73. Long mappingId = SEQUENCE.nextId();
  74. redisService.set(mappingId.toString(),url,60*60*24L);
  75. AuthRedirect redirect = new AuthRedirect().setMappingId(mappingId);
  76. String state = Codec.DoBase64.custom().setData(Jsons.toJson(redirect)).encodeAsText();
  77. response.sendRedirect(weChatService.createUrl(state, notify + WeChatAuthRedirectUrl.get(authType).getUrl(), appId, WeChatAuthRedirectUrl.get(authType).getScope() ));
  78. }
  79. /**
  80. * 静默授权
  81. * @author chan
  82. * @date 2021-10-11 上午11:58
  83. */
  84. @GetMapping("/weChat/silentRedirect")
  85. @Transactional(rollbackFor = Throwable.class)
  86. public void redirectV2(String code, String state, HttpServletResponse response) throws Exception {
  87. // 转跳参数
  88. String json = Codec.DoBase64.custom().setData(state).decodeAsText();
  89. AuthRedirect re = Jsons.parseObject(json, AuthRedirect.class);
  90. AuthToken token = weChatService.getAuthToken(code,appId);
  91. // 查询用户期望转跳的url
  92. String url = redisService.getStr(re.getMappingId().toString());
  93. String data = Codec.DoBase64.custom().setData(token.getOpenid()).encodeAsText();
  94. Cookie cookie = new Cookie("openKey",data);
  95. cookie.setMaxAge(31536000);
  96. cookie.setPath("/");
  97. response.addCookie(cookie);
  98. log.info("微信授权转跳. url: {}",url);
  99. response.sendRedirect(url);
  100. }
  101. }