From f64693c0da5483d8670220bf3a5bf89a32e94a20 Mon Sep 17 00:00:00 2001
From: peng <peng.com>
Date: 星期五, 07 十一月 2025 10:34:21 +0800
Subject: [PATCH] 解决线上打包问题
---
backend/src/main/java/com/rongyichuang/user/service/UserService.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 59 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..54e76b8 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 {
// 鐢ㄦ埛涓嶅瓨鍦紝鍒涘缓鏂扮敤鎴�
@@ -81,6 +126,13 @@
}
/**
+ * 鏍规嵁寰俊openid鏌ユ壘鐢ㄦ埛
+ */
+ public Optional<User> findByWxOpenid(String wxOpenid) {
+ return userRepository.findByWxOpenid(wxOpenid);
+ }
+
+ /**
* 淇濆瓨鐢ㄦ埛
*/
public User save(User user) {
@@ -95,6 +147,13 @@
}
/**
+ * 鑾峰彇瀵嗙爜缂栫爜鍣�
+ */
+ public BCryptPasswordEncoder getPasswordEncoder() {
+ return passwordEncoder;
+ }
+
+ /**
* 淇濆瓨鐢ㄦ埛鎵嬫満鍙风爜
* 鏍稿績閫昏緫锛�
* 1. 鏌ヨ褰撳墠t_user閲岄潰鏄惁瀛樺湪鐩稿悓鐨勭數璇濆彿鐮侊紝濡傛灉娌℃湁锛屽氨鏇存柊鍒板綋鍓島ser閲岄潰
--
Gitblit v1.8.0