From afeeed281e60466b576fbe74d339634cc5d07b82 Mon Sep 17 00:00:00 2001
From: Codex Assistant <codex@example.com>
Date: 星期三, 08 十月 2025 08:56:42 +0800
Subject: [PATCH] 修复评审功能和用户认证问题

---
 backend/src/main/java/com/rongyichuang/user/service/UserService.java |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/backend/src/main/java/com/rongyichuang/user/service/UserService.java b/backend/src/main/java/com/rongyichuang/user/service/UserService.java
index 8ee3be7..0e8f24a 100644
--- a/backend/src/main/java/com/rongyichuang/user/service/UserService.java
+++ b/backend/src/main/java/com/rongyichuang/user/service/UserService.java
@@ -5,6 +5,7 @@
 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;
@@ -20,6 +21,9 @@
 @Service
 @Transactional
 public class UserService {
+
+    @Autowired
+    private UserContextUtil userContextUtil;
 
     private static final Logger logger = LoggerFactory.getLogger(UserService.class);
 
@@ -57,6 +61,47 @@
             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())) {
+                                // 妫�鏌ヨ繖涓猳penid鏄惁宸茬粡琚叾浠栫敤鎴蜂娇鐢�
+                                Optional<User> existingUserWithOpenid = userRepository.findByWxOpenid(currentWxOpenid);
+                                if (existingUserWithOpenid.isEmpty() || existingUserWithOpenid.get().getId().equals(user.getId())) {
+                                    user.setWxOpenid(currentWxOpenid);
+                                    needUpdateWx = true;
+                                }
+                            }
+                        }
+                        if (currentWxUnionid != null && !currentWxUnionid.trim().isEmpty()) {
+                            if (user.getWxUnionid() == null || !currentWxUnionid.equals(user.getWxUnionid())) {
+                                // 妫�鏌ヨ繖涓猽nionid鏄惁宸茬粡琚叾浠栫敤鎴蜂娇鐢�
+                                Optional<User> existingUserWithUnionid = userRepository.findByWxUnionid(currentWxUnionid);
+                                if (existingUserWithUnionid.isEmpty() || existingUserWithUnionid.get().getId().equals(user.getId())) {
+                                    user.setWxUnionid(currentWxUnionid);
+                                    needUpdateWx = true;
+                                }
+                            }
+                        }
+                        if (needUpdateWx) {
+                            logger.info("瑕嗙洊鐢ㄦ埛(wx)鏍囪瘑锛歱hone鍖归厤鐨勭敤鎴稩D={}, 浣跨敤褰撳墠鐧诲綍鐢ㄦ埛ID={} 鐨� openid/unionid 瑕嗙洊", user.getId(), currentUserId);
+                        }
+                    }
+                }
+            } catch (Exception ex) {
+                logger.warn("瑕嗙洊wxOpenid/unionid鏃跺彂鐢熷紓甯革紙蹇界暐锛屼笉闃绘柇鎶ュ悕娴佺▼锛夛細{}", ex.getMessage());
+            }
+
             return userRepository.save(user);
         } else {
             // 鐢ㄦ埛涓嶅瓨鍦紝鍒涘缓鏂扮敤鎴�
@@ -95,6 +140,13 @@
     }
 
     /**
+     * 鑾峰彇瀵嗙爜缂栫爜鍣�
+     */
+    public BCryptPasswordEncoder getPasswordEncoder() {
+        return passwordEncoder;
+    }
+
+    /**
      * 淇濆瓨鐢ㄦ埛鎵嬫満鍙风爜
      * 鏍稿績閫昏緫锛�
      * 1. 鏌ヨ褰撳墠t_user閲岄潰鏄惁瀛樺湪鐩稿悓鐨勭數璇濆彿鐮侊紝濡傛灉娌℃湁锛屽氨鏇存柊鍒板綋鍓島ser閲岄潰

--
Gitblit v1.8.0