Codex Assistant
23 小时以前 c8dffd157cd8b62023b26e62a0b92c152d959423
backend/src/main/java/com/rongyichuang/player/service/ActivityPlayerService.java
@@ -19,6 +19,7 @@
import com.rongyichuang.media.service.MediaV2Service;
import com.rongyichuang.media.dto.MediaSaveInput;
import com.rongyichuang.message.service.MessageService;
import com.rongyichuang.auth.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -61,6 +62,9 @@
    @Autowired
    private MessageService messageService;
    @Autowired
    private JwtUtil jwtUtil;
    public ActivityPlayer getMyActivityPlayer(Long activityId) {
@@ -240,6 +244,18 @@
            String phone = input.getPlayerInfo().getPhone();
            String name = input.getPlayerInfo().getName();
            
            // 获取当前用户的wxopenid(从JWT token中)
            String currentWxOpenid = null;
            try {
                String token = userContextUtil.getTokenFromRequest();
                if (token != null && jwtUtil.validateToken(token)) {
                    currentWxOpenid = jwtUtil.getWxOpenidFromToken(token);
                    log.debug("从JWT token中获取到wxopenid: {}", currentWxOpenid);
                }
            } catch (Exception e) {
                log.warn("获取当前用户wxopenid时发生异常: {}", e.getMessage());
            }
            // 先查找现有用户
            Optional<User> existingUserOpt = userService.findByPhone(phone);
            
@@ -251,6 +267,18 @@
                // 更新用户的生日信息
                if (input.getPlayerInfo().getBirthDate() != null) {
                    user.setBirthday(input.getPlayerInfo().getBirthDate());
                }
                // 更新wxopenid(如果当前token中包含且不为空)
                if (currentWxOpenid != null && !currentWxOpenid.trim().isEmpty()) {
                    // 检查这个openid是否已经被其他用户使用
                    Optional<User> existingUserWithOpenid = userService.findByWxOpenid(currentWxOpenid);
                    if (existingUserWithOpenid.isEmpty() || existingUserWithOpenid.get().getId().equals(user.getId())) {
                        user.setWxOpenid(currentWxOpenid);
                        log.info("更新用户wxopenid: {}", currentWxOpenid);
                    } else {
                        log.warn("wxopenid {} 已被其他用户使用,用户ID: {}", currentWxOpenid, existingUserWithOpenid.get().getId());
                    }
                }
                
                user = userService.save(user);
@@ -270,6 +298,18 @@
                    newUser.setBirthday(input.getPlayerInfo().getBirthDate());
                }
                
                // 设置wxopenid(如果当前token中包含且不为空)
                if (currentWxOpenid != null && !currentWxOpenid.trim().isEmpty()) {
                    // 检查这个openid是否已经被其他用户使用
                    Optional<User> existingUserWithOpenid = userService.findByWxOpenid(currentWxOpenid);
                    if (existingUserWithOpenid.isEmpty()) {
                        newUser.setWxOpenid(currentWxOpenid);
                        log.info("为新用户设置wxopenid: {}", currentWxOpenid);
                    } else {
                        log.warn("wxopenid {} 已被其他用户使用,用户ID: {}", currentWxOpenid, existingUserWithOpenid.get().getId());
                    }
                }
                newUser = userService.save(newUser);
                log.info("为小程序报名成功创建新用户,用户ID: {}", newUser.getId());
                return newUser;