From 49c254ed5bdc8348551d808ee72579a6d2221e3b Mon Sep 17 00:00:00 2001
From: lrj <owen.stl@gmail.com>
Date: 星期六, 04 十月 2025 18:43:12 +0800
Subject: [PATCH] fix(miniprogram): 切换 GraphQL 端点为 /graphql,修复 review/index 400
---
backend/src/main/java/com/rongyichuang/user/service/UserService.java | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 156 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 ad01ab3..a4c4913 100644
--- a/backend/src/main/java/com/rongyichuang/user/service/UserService.java
+++ b/backend/src/main/java/com/rongyichuang/user/service/UserService.java
@@ -2,10 +2,16 @@
import com.rongyichuang.user.entity.User;
import com.rongyichuang.user.repository.UserRepository;
+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;
import org.springframework.transaction.annotation.Transactional;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import java.util.Optional;
@@ -17,7 +23,21 @@
public class UserService {
@Autowired
+ private UserContextUtil userContextUtil;
+
+ private static final Logger logger = LoggerFactory.getLogger(UserService.class);
+
+ @Autowired
private UserRepository userRepository;
+
+ @Autowired
+ private EmployeeRepository employeeRepository;
+
+ @Autowired
+ private JudgeRepository judgeRepository;
+
+ @Autowired
+ private PlayerRepository playerRepository;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
@@ -41,6 +61,39 @@
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)鏍囪瘑锛歱hone鍖归厤鐨勭敤鎴稩D={}, 浣跨敤褰撳墠鐧诲綍鐢ㄦ埛ID={} 鐨� openid/unionid 瑕嗙洊", user.getId(), currentUserId);
+ }
+ }
+ }
+ } catch (Exception ex) {
+ logger.warn("瑕嗙洊wxOpenid/unionid鏃跺彂鐢熷紓甯革紙蹇界暐锛屼笉闃绘柇鎶ュ悕娴佺▼锛夛細{}", ex.getMessage());
+ }
+
return userRepository.save(user);
} else {
// 鐢ㄦ埛涓嶅瓨鍦紝鍒涘缓鏂扮敤鎴�
@@ -77,4 +130,107 @@
public Optional<User> findById(Long id) {
return userRepository.findById(id);
}
+
+ /**
+ * 鑾峰彇瀵嗙爜缂栫爜鍣�
+ */
+ public BCryptPasswordEncoder getPasswordEncoder() {
+ return passwordEncoder;
+ }
+
+ /**
+ * 淇濆瓨鐢ㄦ埛鎵嬫満鍙风爜
+ * 鏍稿績閫昏緫锛�
+ * 1. 鏌ヨ褰撳墠t_user閲岄潰鏄惁瀛樺湪鐩稿悓鐨勭數璇濆彿鐮侊紝濡傛灉娌℃湁锛屽氨鏇存柊鍒板綋鍓島ser閲岄潰
+ * 2. 濡傛灉瀛樺湪鐩稿悓鐨勭數璇濆彿鐮侊紝骞朵笖瀵瑰簲user鐨刼penid鏄痭ull锛岄偅涔堟竻绌哄師鏉ser鐨刾hone璁剧疆涓簄ull锛�
+ * 鎶婃柊鐨勭數璇濆彿鐮佹洿鏂板埌褰撳墠鐢ㄦ埛鐨刾hone瀛楁锛屽悓鏃舵洿鏂皌_player銆乼_judge銆乼_employee鍏宠仈鐨剈ser_id鍒版柊鐨剈ser_id
+ *
+ * @param currentUserId 褰撳墠鐢ㄦ埛ID
+ * @param phone 瑕佷繚瀛樼殑鎵嬫満鍙风爜
+ * @return 鏇存柊鍚庣殑鐢ㄦ埛瀹炰綋
+ */
+ @Transactional
+ public User saveUserPhone(Long currentUserId, String phone) {
+ logger.info("寮�濮嬪鐞嗙敤鎴锋墜鏈哄彿鐮佷繚瀛橈紝鐢ㄦ埛ID: {}, 鎵嬫満鍙�: {}", currentUserId, phone);
+
+ // 鑾峰彇褰撳墠鐢ㄦ埛
+ User currentUser = userRepository.findById(currentUserId)
+ .orElseThrow(() -> new RuntimeException("鐢ㄦ埛涓嶅瓨鍦紝ID: " + currentUserId));
+
+ // 妫�鏌ユ槸鍚﹀瓨鍦ㄧ浉鍚屾墜鏈哄彿鐨勭敤鎴�
+ Optional<User> existingPhoneUser = userRepository.findByPhone(phone);
+
+ if (existingPhoneUser.isEmpty()) {
+ // 鎯呭喌1锛氭病鏈夊叾浠栫敤鎴蜂娇鐢ㄨ繖涓墜鏈哄彿锛岀洿鎺ユ洿鏂板綋鍓嶇敤鎴�
+ logger.info("鎵嬫満鍙� {} 鏈浣跨敤锛岀洿鎺ユ洿鏂板埌鐢ㄦ埛 {}", phone, currentUserId);
+ currentUser.setPhone(phone);
+ return userRepository.save(currentUser);
+ } else {
+ User phoneUser = existingPhoneUser.get();
+
+ // 濡傛灉鏄悓涓�涓敤鎴凤紝鏃犻渶澶勭悊
+ if (phoneUser.getId().equals(currentUserId)) {
+ logger.info("鎵嬫満鍙� {} 宸插睘浜庡綋鍓嶇敤鎴� {}锛屾棤闇�鏇存柊", phone, currentUserId);
+ return currentUser;
+ }
+
+ // 鎯呭喌2锛氬瓨鍦ㄧ浉鍚屾墜鏈哄彿鐨勭敤鎴凤紝涓旇鐢ㄦ埛鐨刼penid涓簄ull
+ if (phoneUser.getWxOpenid() == null || phoneUser.getWxOpenid().trim().isEmpty()) {
+ logger.info("鍙戠幇鎵嬫満鍙� {} 琚敤鎴� {} 浣跨敤锛屼絾璇ョ敤鎴锋棤openid锛屽紑濮嬫暟鎹縼绉�", phone, phoneUser.getId());
+
+ // 杩佺Щ鍏宠仈鏁版嵁
+ migrateUserRelatedData(phoneUser.getId(), currentUserId);
+
+ // 娓呯┖鍘熺敤鎴风殑鎵嬫満鍙�
+ phoneUser.setPhone(null);
+ userRepository.save(phoneUser);
+ logger.info("宸叉竻绌哄師鐢ㄦ埛 {} 鐨勬墜鏈哄彿", phoneUser.getId());
+
+ // 鏇存柊褰撳墠鐢ㄦ埛鐨勬墜鏈哄彿
+ currentUser.setPhone(phone);
+ User savedUser = userRepository.save(currentUser);
+ logger.info("宸插皢鎵嬫満鍙� {} 鏇存柊鍒板綋鍓嶇敤鎴� {}", phone, currentUserId);
+
+ return savedUser;
+ } else {
+ // 鎯呭喌3锛氬瓨鍦ㄧ浉鍚屾墜鏈哄彿鐨勭敤鎴凤紝涓旇鐢ㄦ埛鏈塷penid锛屼笉鑳借鐩�
+ logger.warn("鎵嬫満鍙� {} 宸茶鐢ㄦ埛 {} 浣跨敤锛屼笖璇ョ敤鎴锋湁openid锛屾棤娉曟洿鏂�", phone, phoneUser.getId());
+ throw new RuntimeException("鎵嬫満鍙峰凡琚叾浠栧井淇$敤鎴蜂娇鐢紝鏃犳硶鏇存柊");
+ }
+ }
+ }
+
+ /**
+ * 杩佺Щ鐢ㄦ埛鍏宠仈鏁版嵁
+ * 灏嗗師鐢ㄦ埛鐨勫憳宸ャ�佽瘎濮斻�佸鍛樹俊鎭縼绉诲埌鏂扮敤鎴�
+ *
+ * @param fromUserId 鍘熺敤鎴稩D
+ * @param toUserId 鐩爣鐢ㄦ埛ID
+ */
+ private void migrateUserRelatedData(Long fromUserId, Long toUserId) {
+ logger.info("寮�濮嬭縼绉荤敤鎴峰叧鑱旀暟鎹紝浠庣敤鎴� {} 鍒扮敤鎴� {}", fromUserId, toUserId);
+
+ // 杩佺Щ鍛樺伐淇℃伅
+ employeeRepository.findByUserId(fromUserId).ifPresent(employee -> {
+ logger.info("杩佺Щ鍛樺伐淇℃伅锛屽憳宸D: {}, 濮撳悕: {}", employee.getId(), employee.getName());
+ employee.setUserId(toUserId);
+ employeeRepository.save(employee);
+ });
+
+ // 杩佺Щ璇勫淇℃伅
+ judgeRepository.findByUserId(fromUserId).ifPresent(judge -> {
+ logger.info("杩佺Щ璇勫淇℃伅锛岃瘎濮擨D: {}, 濮撳悕: {}", judge.getId(), judge.getName());
+ judge.setUserId(toUserId);
+ judgeRepository.save(judge);
+ });
+
+ // 杩佺Щ瀛﹀憳淇℃伅
+ playerRepository.findByUserId(fromUserId).ifPresent(player -> {
+ logger.info("杩佺Щ瀛﹀憳淇℃伅锛屽鍛業D: {}, 濮撳悕: {}", player.getId(), player.getName());
+ player.setUserId(toUserId);
+ playerRepository.save(player);
+ });
+
+ logger.info("鐢ㄦ埛鍏宠仈鏁版嵁杩佺Щ瀹屾垚");
+ }
}
\ No newline at end of file
--
Gitblit v1.8.0