UserServiceImpl.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.cyksj.service.user.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  6. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  7. import com.cyksj.common.exception.BusinessRuntimeException;
  8. import com.cyksj.config.WeChatConfig;
  9. import com.cyksj.mapper.UserMapper;
  10. import com.cyksj.mapper.UserTransferLogMapper;
  11. import com.cyksj.mapper.WxSyncLogMapper;
  12. import com.cyksj.model.dto.GzhOAuth2UserInfo;
  13. import com.cyksj.model.dto.UserWhoami;
  14. import com.cyksj.model.entity.User;
  15. import com.cyksj.model.entity.UserTransferLog;
  16. import com.cyksj.model.entity.WxSyncLog;
  17. import com.cyksj.service.distribute.DistributeService;
  18. import com.cyksj.service.user.UserService;
  19. import lombok.RequiredArgsConstructor;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.util.Assert;
  23. import java.util.Optional;
  24. /**
  25. * @author chan
  26. * @date 2021/10/10 下午3:27
  27. */
  28. @Slf4j
  29. @Service
  30. @RequiredArgsConstructor
  31. public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
  32. private final WeChatConfig weChatConfig;
  33. private final UserTransferLogMapper userTransferLogMapper;
  34. private final WxSyncLogMapper wxSyncLogMapper;
  35. private final DistributeService distributeService;
  36. @Override
  37. public User saveAuth(GzhOAuth2UserInfo info, Long sharedId, Integer dsType, Long pid) {
  38. final String unionid = info.getUnionid();
  39. if (StrUtil.isEmpty(unionid)) {
  40. throw BusinessRuntimeException.getInstance("授权操作异常,请刷新页面");
  41. }
  42. WxSyncLog wxSyncLog = wxSyncLogMapper.selectOne(Wrappers.lambdaQuery(WxSyncLog.class)
  43. .eq(WxSyncLog::getToAppid, weChatConfig.getAppid())
  44. .eq(WxSyncLog::getNewOpenid, info.getOpenid()).last("limit 1"));
  45. User user = getSyncUser(info.getOpenid(), info.getUnionid(), wxSyncLog);
  46. user = Optional.ofNullable(user).orElseGet(() -> new User()
  47. .setUnionid(unionid));
  48. user.setNickname(info.getNickname())
  49. .setSex(info.getSex())
  50. .setCountry(info.getCountry())
  51. .setProvince(info.getProvince())
  52. .setCity(info.getCity())
  53. .setHeadimgurl(info.getHeadimgurl());
  54. if (!user.getUnionid().equals(unionid)) {
  55. UserTransferLog log = new UserTransferLog();
  56. if (wxSyncLog != null) {
  57. log.setType("公众号迁移");
  58. } else {
  59. log.setType("绑定开发者账号");
  60. }
  61. log.setOldAppId(weChatConfig.getOldAppid());
  62. log.setNewAppId(weChatConfig.getAppid());
  63. log.setUserId(user.getId());
  64. log.setOldUnionid(user.getUnionid());
  65. log.setNewUnionid(info.getUnionid());
  66. userTransferLogMapper.insert(log);
  67. }
  68. user.setUnionid(unionid);
  69. user.setOpenId(info.getOpenid());
  70. Boolean isNewUser = false;
  71. if (user.getId() == null) {
  72. isNewUser = true;
  73. user.setPopularizeId(pid);
  74. }
  75. this.saveOrUpdate(user);
  76. if (sharedId != null && isNewUser) {
  77. distributeService.saveDistributionInvitation(sharedId, user.getId());
  78. }
  79. return user;
  80. }
  81. @Override
  82. public UserWhoami whoami(long userId) {
  83. User user = this.getById(userId);
  84. Assert.notNull(user,"业务处理失败,请联系客服");
  85. UserWhoami userWhoami = new UserWhoami();
  86. BeanUtil.copyProperties(user, userWhoami);
  87. return userWhoami;
  88. }
  89. @Override
  90. public User getSyncUser(String openId, String unionId, WxSyncLog wxSyncLog) {
  91. User user;
  92. //迁移公众号之后同步授权后信息
  93. //是否是迁移用户
  94. if (wxSyncLog != null) {
  95. user = this.getOne(Wrappers.lambdaQuery(User.class)
  96. .ne(User::getUnionid, unionId)
  97. .eq(User::getOpenId, wxSyncLog.getOriOpenid()).last("limit 1"));
  98. if (user != null) {
  99. user.setOpenId(wxSyncLog.getNewOpenid());
  100. } else {
  101. // 查询用户是否存在
  102. user = Optional.ofNullable(
  103. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  104. .orderByAsc(User::getId)
  105. .last("limit 1")))
  106. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  107. .orderByAsc(User::getId)
  108. .last("limit 1")));
  109. }
  110. } else {
  111. // 查询用户是否存在
  112. user = Optional.ofNullable(
  113. this.getOne(new LambdaQueryWrapper<User>().eq(User::getUnionid, unionId)
  114. .orderByAsc(User::getId)
  115. .last("limit 1")))
  116. .orElseGet(() -> this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId)
  117. .orderByAsc(User::getId)
  118. .last("limit 1")));
  119. }
  120. return user;
  121. }
  122. }