| | |
| | | import com.rongyichuang.employee.repository.EmployeeRepository; |
| | | import com.rongyichuang.judge.repository.JudgeRepository; |
| | | import com.rongyichuang.player.repository.PlayerRepository; |
| | | import com.rongyichuang.common.util.UserContextUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | @Service |
| | | @Transactional |
| | | public class UserService { |
| | | |
| | | @Autowired |
| | | private UserContextUtil userContextUtil; |
| | | |
| | | private static final Logger logger = LoggerFactory.getLogger(UserService.class); |
| | | |
| | |
| | | if (password != null && !password.trim().isEmpty()) { |
| | | user.setPassword(passwordEncoder.encode(password)); |
| | | } |
| | | |
| | | // 覆盖 wxOpenid / wxUnionid:如果当前登录用户有openid/unionid,并与库中不同,则以当前登录覆盖 |
| | | try { |
| | | Long currentUserId = userContextUtil != null ? userContextUtil.getCurrentUserId() : null; |
| | | if (currentUserId != null) { |
| | | Optional<User> currentUserOpt = userRepository.findById(currentUserId); |
| | | if (currentUserOpt.isPresent()) { |
| | | User currentUser = currentUserOpt.get(); |
| | | String currentWxOpenid = currentUser.getWxOpenid(); |
| | | String currentWxUnionid = currentUser.getWxUnionid(); |
| | | |
| | | boolean needUpdateWx = false; |
| | | if (currentWxOpenid != null && !currentWxOpenid.trim().isEmpty()) { |
| | | if (user.getWxOpenid() == null || !currentWxOpenid.equals(user.getWxOpenid())) { |
| | | user.setWxOpenid(currentWxOpenid); |
| | | needUpdateWx = true; |
| | | } |
| | | } |
| | | if (currentWxUnionid != null && !currentWxUnionid.trim().isEmpty()) { |
| | | if (user.getWxUnionid() == null || !currentWxUnionid.equals(user.getWxUnionid())) { |
| | | user.setWxUnionid(currentWxUnionid); |
| | | needUpdateWx = true; |
| | | } |
| | | } |
| | | if (needUpdateWx) { |
| | | logger.info("覆盖用户(wx)标识:phone匹配的用户ID={}, 使用当前登录用户ID={} 的 openid/unionid 覆盖", user.getId(), currentUserId); |
| | | } |
| | | } |
| | | } |
| | | } catch (Exception ex) { |
| | | logger.warn("覆盖wxOpenid/unionid时发生异常(忽略,不阻断报名流程):{}", ex.getMessage()); |
| | | } |
| | | |
| | | return userRepository.save(user); |
| | | } else { |
| | | // 用户不存在,创建新用户 |