From 58d9f460b2f8c34430285115e2557d18333c5cab Mon Sep 17 00:00:00 2001 From: Codex Assistant <codex@example.com> Date: 星期三, 08 十月 2025 14:16:55 +0800 Subject: [PATCH] feat: 修复Player实体phone字段数据冗余问题并优化小程序报名逻辑 --- backend/src/main/java/com/rongyichuang/user/resolver/UserResolver.java | 550 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 537 insertions(+), 13 deletions(-) diff --git a/backend/src/main/java/com/rongyichuang/user/resolver/UserResolver.java b/backend/src/main/java/com/rongyichuang/user/resolver/UserResolver.java index dec68fa..dd400f0 100644 --- a/backend/src/main/java/com/rongyichuang/user/resolver/UserResolver.java +++ b/backend/src/main/java/com/rongyichuang/user/resolver/UserResolver.java @@ -4,22 +4,47 @@ import com.rongyichuang.auth.dto.LoginResponse.JudgeInfo; import com.rongyichuang.auth.dto.LoginResponse.PlayerInfo; import com.rongyichuang.common.util.UserContextUtil; +import com.rongyichuang.auth.util.JwtUtil; import com.rongyichuang.employee.entity.Employee; import com.rongyichuang.employee.service.EmployeeService; import com.rongyichuang.judge.entity.Judge; import com.rongyichuang.judge.service.JudgeService; import com.rongyichuang.player.entity.Player; import com.rongyichuang.player.service.PlayerService; +import com.rongyichuang.player.repository.ActivityPlayerRepository; +import com.rongyichuang.common.repository.MediaRepository; +import com.rongyichuang.common.entity.Media; +import com.rongyichuang.common.enums.MediaTargetType; +import com.rongyichuang.media.service.MediaV2Service; +import com.rongyichuang.media.dto.MediaSaveInput; +import com.rongyichuang.media.dto.MediaSaveResponse; import com.rongyichuang.user.dto.response.UserProfile; import com.rongyichuang.user.dto.response.UserStats; import com.rongyichuang.user.dto.response.UserRegistration; +import com.rongyichuang.user.dto.response.UserProject; +import com.rongyichuang.user.dto.request.UserInput; +import com.rongyichuang.user.dto.response.UserProfileInfo; +import com.rongyichuang.user.entity.User; +import com.rongyichuang.user.repository.UserRepository; +import com.rongyichuang.player.dto.response.SubmissionMediaResponse; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.graphql.data.method.annotation.Argument; import org.springframework.graphql.data.method.annotation.QueryMapping; +import org.springframework.graphql.data.method.annotation.MutationMapping; import org.springframework.stereotype.Controller; +import org.springframework.util.StringUtils; + +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import jakarta.persistence.Query; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; /** * 鐢ㄦ埛GraphQL瑙f瀽鍣� @@ -38,7 +63,28 @@ private PlayerService playerService; @Autowired + private ActivityPlayerRepository activityPlayerRepository; + + @Autowired + private MediaRepository mediaRepository; + + @Autowired + private UserRepository userRepository; + + @PersistenceContext + private EntityManager entityManager; + + @Autowired private UserContextUtil userContextUtil; + + @Autowired + private JwtUtil jwtUtil; + + @Autowired + private MediaV2Service mediaV2Service; + + @Value("${app.media-url}") + private String mediaBaseUrl; /** * 鑾峰彇褰撳墠鐢ㄦ埛妗f @@ -48,12 +94,39 @@ try { Long userId = userContextUtil.getCurrentUserId(); logger.debug("杩涘叆userProfile锛岃В鏋愬埌鐢ㄦ埛ID: {}", userId); + + // 濡傛灉鏄尶鍚嶇敤鎴凤紝杩斿洖鍩烘湰鐨勭敤鎴锋。妗� if (userId == null) { - throw new RuntimeException("鐢ㄦ埛鏈櫥褰�"); + logger.debug("鍖垮悕鐢ㄦ埛璁块棶userProfile锛岃繑鍥炲熀鏈。妗�"); + UserProfile profile = new UserProfile(); + profile.setId("0"); // 鍖垮悕鐢ㄦ埛ID璁句负0 + profile.setName("鍖垮悕鐢ㄦ埛"); + profile.setRoles(new ArrayList<>()); + profile.setUserType("user"); // 鍖垮悕鐢ㄦ埛绫诲瀷涓烘櫘閫氱敤鎴� + profile.setCreatedAt(java.time.LocalDateTime.now().toString()); + return profile; } UserProfile profile = new UserProfile(); profile.setId(userId.toString()); + + // 鑾峰彇鐢ㄦ埛鍩烘湰淇℃伅 + Optional<User> userOpt = userRepository.findById(userId); + User user = null; + if (userOpt.isPresent()) { + user = userOpt.get(); + // 璁剧疆鍩烘湰淇℃伅锛堝鍚嶅拰鐢佃瘽鍙风爜锛� + profile.setName(user.getName()); + profile.setPhone(user.getPhone()); + // 璁剧疆鎬у埆 + if (user.getGender() != null) { + profile.setGender(user.getGender() == 1 ? "MALE" : "FEMALE"); + } + // 璁剧疆鐢熸棩 + if (user.getBirthday() != null) { + profile.setBirthday(user.getBirthday().toString()); + } + } List<String> roles = new ArrayList<>(); @@ -62,8 +135,13 @@ Employee employee = employeeService.findByUserId(userId); logger.debug("鍛樺伐鏌ヨ缁撴灉: {}", employee != null ? ("id=" + employee.getId() + ", name=" + employee.getName()) : "鏃�"); if (employee != null) { - profile.setName(employee.getName()); - profile.setPhone(employee.getPhone()); + // 濡傛灉鍛樺伐淇℃伅瀛樺湪锛屽垯瑕嗙洊鍩烘湰淇℃伅 + if (employee.getName() != null) { + profile.setName(employee.getName()); + } + if (employee.getPhone() != null) { + profile.setPhone(employee.getPhone()); + } roles.add("EMPLOYEE"); EmployeeInfo employeeInfo = new EmployeeInfo( @@ -80,8 +158,11 @@ Judge judge = judgeService.findByUserId(userId); logger.debug("璇勫鏌ヨ缁撴灉: {}", judge != null ? ("id=" + judge.getId() + ", name=" + judge.getName()) : "鏃�"); if (judge != null) { - if (profile.getName() == null) { + // 濡傛灉璇勫淇℃伅瀛樺湪涓斿熀鏈俊鎭负绌猴紝鍒欒缃瘎濮斾俊鎭� + if (profile.getName() == null && judge.getName() != null) { profile.setName(judge.getName()); + } + if (profile.getPhone() == null && judge.getPhone() != null) { profile.setPhone(judge.getPhone()); } roles.add("JUDGE"); @@ -101,10 +182,14 @@ Player player = playerService.findByUserId(userId); logger.debug("瀛﹀憳鏌ヨ缁撴灉: {}", player != null ? ("id=" + player.getId() + ", name=" + player.getName()) : "鏃�"); if (player != null) { - if (profile.getName() == null) { + // 濡傛灉瀛﹀憳淇℃伅瀛樺湪涓斿熀鏈俊鎭负绌猴紝鍒欒缃鍛樹俊鎭� + if (profile.getName() == null && player.getName() != null) { profile.setName(player.getName()); - profile.setPhone(player.getPhone()); } + // 涓嶅啀浠嶱layer鑾峰彇phone淇℃伅锛宲hone缁熶竴浠嶶ser瀹炰綋鑾峰彇 + // if (profile.getPhone() == null && player.getPhone() != null) { + // profile.setPhone(player.getPhone()); + // } roles.add("PLAYER"); PlayerInfo playerInfo = new PlayerInfo( @@ -117,7 +202,42 @@ } profile.setRoles(roles); - logger.debug("userProfile鏋勫缓瀹屾垚锛宺oles={}, name={}, phone={}", roles, profile.getName(), profile.getPhone()); + + // 纭畾涓昏瑙掕壊绫诲瀷锛堜紭鍏堢骇锛歟mployee > judge > player锛� + String userType; + if (employee != null) { + userType = "employee"; + } else if (judge != null) { + userType = "judge"; + } else if (player != null) { + userType = "player"; + } else { + userType = "user"; // 鏅�氱敤鎴� + } + profile.setUserType(userType); + logger.debug("璁剧疆鐢ㄦ埛绫诲瀷: {}", userType); + + // 鑾峰彇鐢ㄦ埛澶村儚 + try { + List<Media> avatarMediaList = mediaRepository.findByTargetTypeAndTargetIdAndState( + MediaTargetType.USER_AVATAR.getValue(), + userId, + 1 + ); + if (!avatarMediaList.isEmpty()) { + Media avatarMedia = avatarMediaList.get(0); + String fullAvatarUrl = buildFullMediaUrl(avatarMedia.getPath()); + profile.setAvatar(fullAvatarUrl); + logger.debug("鎵惧埌鐢ㄦ埛澶村儚: {} -> {}", avatarMedia.getPath(), fullAvatarUrl); + } else { + logger.debug("鐢ㄦ埛{}娌℃湁鎵惧埌澶村儚", userId); + } + } catch (Exception e) { + logger.warn("鑾峰彇鐢ㄦ埛澶村儚澶辫触: {}", e.getMessage()); + } + + logger.debug("userProfile鏋勫缓瀹屾垚锛宺oles={}, name={}, phone={}, avatar={}", + roles, profile.getName(), profile.getPhone(), profile.getAvatar()); profile.setCreatedAt(java.time.LocalDateTime.now().toString()); return profile; @@ -133,7 +253,7 @@ @QueryMapping public UserStats userStats() { try { - Long userId = UserContextUtil.getCurrentUserId(); + Long userId = userContextUtil.getCurrentUserId(); if (userId == null) { return null; } @@ -157,17 +277,421 @@ @QueryMapping public List<UserRegistration> myRegistrations(@Argument Integer limit) { try { - Long userId = UserContextUtil.getCurrentUserId(); + Long userId = userContextUtil.getCurrentUserId(); if (userId == null) { return new ArrayList<>(); } - // 杩欓噷搴旇瀹炵幇鐪熸鐨勬姤鍚嶈褰曟煡璇㈤�昏緫 - // 鏆傛椂杩斿洖绌哄垪琛� - return new ArrayList<>(); + // 鏋勫缓SQL鏌ヨ锛岃幏鍙栫敤鎴风殑鎶ュ悕璁板綍 + StringBuilder sql = new StringBuilder(); + sql.append("SELECT ap.id, a.id as activity_id, a.name as activity_name, "); + sql.append("ap.project_name, ap.state, ap.create_time, "); + sql.append("m.path as cover_image_path "); + sql.append("FROM t_activity_player ap "); + sql.append("INNER JOIN t_player p ON ap.player_id = p.id "); + sql.append("INNER JOIN t_activity a ON ap.activity_id = a.id "); + sql.append("LEFT JOIN t_media m ON a.cover_image_id = m.id "); + sql.append("WHERE p.user_id = :userId "); + sql.append("ORDER BY ap.create_time DESC"); + + if (limit != null && limit > 0) { + sql.append(" LIMIT :limit"); + } + + Query query = entityManager.createNativeQuery(sql.toString()); + query.setParameter("userId", userId); + if (limit != null && limit > 0) { + query.setParameter("limit", limit); + } + + @SuppressWarnings("unchecked") + List<Object[]> results = query.getResultList(); + + List<UserRegistration> registrations = new ArrayList<>(); + for (Object[] row : results) { + UserRegistration registration = new UserRegistration(); + registration.setId(String.valueOf(row[0])); + + // 鍒涘缓ActivityInfo + UserRegistration.ActivityInfo activityInfo = new UserRegistration.ActivityInfo(); + activityInfo.setId(String.valueOf(row[1])); + activityInfo.setTitle((String) row[2]); + + // 璁剧疆灏侀潰鍥剧墖 + if (row[6] != null) { + UserRegistration.MediaInfo mediaInfo = new UserRegistration.MediaInfo(); + mediaInfo.setPath((String) row[6]); + mediaInfo.setFullUrl(buildFullMediaUrl((String) row[6])); + activityInfo.setCoverImage(mediaInfo); + } + + registration.setActivity(activityInfo); + + // 璁剧疆鐘舵�� + Integer state = (Integer) row[4]; + String status = "pending"; + if (state != null) { + switch (state) { + case 0: status = "pending"; break; + case 1: status = "approved"; break; + case 2: status = "rejected"; break; + default: status = "unknown"; break; + } + } + registration.setStatus(status); + + // 璁剧疆鎶ュ悕鏃堕棿 + if (row[5] != null) { + registration.setRegistrationTime(row[5].toString()); + } + + registrations.add(registration); + } + + return registrations; } catch (Exception e) { - e.printStackTrace(); + logger.error("鑾峰彇鐢ㄦ埛鎶ュ悕璁板綍澶辫触", e); return new ArrayList<>(); } } + + /** + * 鑾峰彇鎴戠殑椤圭洰鍒楄〃 + */ + @QueryMapping + public List<UserProject> myProjects() { + try { + Long userId = userContextUtil.getCurrentUserId(); + logger.debug("鑾峰彇鐢ㄦ埛椤圭洰鍒楄〃锛寀serId: {}", userId); + if (userId == null) { + return new ArrayList<>(); + } + + // 鏌ヨ鐢ㄦ埛鐨勬墍鏈夐」鐩紙state = 0, 1, 2锛� + // 浣跨敤 t_activity_player join t_player join t_user 鐨勬柟寮忛�氳繃鐢ㄦ埛ID鏌ヨ + String sql = """ + SELECT ap.id, ap.project_name, a.name as activity_name, ap.state, ap.create_time + FROM t_activity_player ap + JOIN t_player p ON ap.player_id = p.id + JOIN t_activity a ON a.id = ap.activity_id + WHERE p.user_id = ? AND ap.state IN (0, 1, 2) + ORDER BY ap.create_time DESC + """; + + Query query = entityManager.createNativeQuery(sql); + query.setParameter(1, userId); + + @SuppressWarnings("unchecked") + List<Object[]> results = query.getResultList(); + + List<UserProject> projects = new ArrayList<>(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + for (Object[] row : results) { + String id = row[0].toString(); + String projectName = row[1] != null ? row[1].toString() : ""; + String activityName = row[2] != null ? row[2].toString() : ""; + String status = row[3] != null ? row[3].toString() : "0"; + String createTime = row[4] != null ? row[4].toString() : ""; + + UserProject project = new UserProject(id, projectName, activityName, status, createTime); + + // 鏌ヨ椤圭洰鐨勬彁浜ゆ枃浠� + List<SubmissionMediaResponse> submissionFiles = getSubmissionFiles(Long.parseLong(id)); + project.setSubmissionFiles(submissionFiles); + + projects.add(project); + } + + logger.debug("鏌ヨ鍒� {} 涓」鐩�", projects.size()); + return projects; + } catch (Exception e) { + logger.error("鑾峰彇鐢ㄦ埛椤圭洰鍒楄〃澶辫触", e); + return new ArrayList<>(); + } + } + + /** + * 鑾峰彇椤圭洰鐨勬彁浜ゆ枃浠� + * @param activityPlayerId 娲诲姩鍙備笌鑰匢D + * @return 鎻愪氦鏂囦欢鍒楄〃 + */ + private List<SubmissionMediaResponse> getSubmissionFiles(Long activityPlayerId) { + try { + // 鏌ヨ鎻愪氦鐨勫獟浣撴枃浠� (target_type = 5 琛ㄧず娲诲姩鍙備笌鑰呮彁浜ょ殑鏂囦欢锛宻tate = 1 琛ㄧず鏈夋晥鐘舵��) + List<Media> medias = mediaRepository.findByTargetTypeAndTargetIdAndState(5, activityPlayerId, 1); + + List<SubmissionMediaResponse> submissionFiles = new ArrayList<>(); + for (Media media : medias) { + SubmissionMediaResponse response = new SubmissionMediaResponse(); + response.setId(media.getId()); + response.setName(media.getName()); + response.setPath(media.getPath()); + response.setUrl(buildFullMediaUrl(media.getPath())); + response.setFullUrl(buildFullMediaUrl(media.getPath())); + response.setFullThumbUrl(buildFullMediaUrl(media.getThumbPath())); + response.setFileExt(media.getFileExt()); + response.setFileSize(media.getFileSize() != null ? media.getFileSize().longValue() : null); + response.setMediaType(media.getMediaType()); + response.setThumbUrl(buildFullMediaUrl(media.getThumbPath())); + submissionFiles.add(response); + } + + return submissionFiles; + } catch (Exception e) { + logger.error("鑾峰彇椤圭洰鎻愪氦鏂囦欢澶辫触锛宎ctivityPlayerId: {}", activityPlayerId, e); + return new ArrayList<>(); + } + } + + /** + * 鏋勫缓瀹屾暣鐨勫獟浣揢RL + * @param path 濯掍綋璺緞 + * @return 瀹屾暣鐨刄RL + */ + private String buildFullMediaUrl(String path) { + if (!StringUtils.hasText(path)) { + return null; + } + + // 濡傛灉璺緞宸茬粡鏄畬鏁碪RL锛岀洿鎺ヨ繑鍥� + if (path.startsWith("http://") || path.startsWith("https://")) { + return path; + } + + // 鏋勫缓瀹屾暣URL + if (StringUtils.hasText(mediaBaseUrl)) { + // 纭繚baseUrl浠�/缁撳熬锛宲ath涓嶄互/寮�澶� + String baseUrl = mediaBaseUrl.endsWith("/") ? mediaBaseUrl : mediaBaseUrl + "/"; + String relativePath = path.startsWith("/") ? path.substring(1) : path; + return baseUrl + relativePath; + } + + // 濡傛灉娌℃湁閰嶇疆baseUrl锛岃繑鍥炲師璺緞 + return path; + } + + /** + * 淇濆瓨鐢ㄦ埛淇℃伅 + */ + @MutationMapping + public UserProfileInfo saveUserInfo(@Argument UserInput input) { + try { + Long userId = userContextUtil.getCurrentUserIdIncludingAnonymous(); + logger.debug("杩涘叆saveUserInfo锛岃В鏋愬埌鐢ㄦ埛ID锛堝寘鎷尶鍚嶇敤鎴凤級: {}", userId); + if (userId == null) { + throw new RuntimeException("鐢ㄦ埛鏈櫥褰�"); + } + + User user; + boolean isNewUser = false; + + // 妫�鏌ユ墜鏈哄彿鏄惁瀛樺湪 + if (StringUtils.hasText(input.getPhone())) { + Optional<User> existingUserByPhone = userRepository.findByPhone(input.getPhone()); + if (existingUserByPhone.isPresent()) { + // 鎵嬫満鍙峰凡瀛樺湪锛屾洿鏂扮幇鏈夌敤鎴� + user = existingUserByPhone.get(); + logger.debug("鎵嬫満鍙穥}宸插瓨鍦紝鏇存柊鐜版湁鐢ㄦ埛锛岀敤鎴稩D: {}", input.getPhone(), user.getId()); + + // 濡傛灉褰撳墠鐢ㄦ埛ID涓庢墜鏈哄彿瀵瑰簲鐨勭敤鎴稩D涓嶅悓锛岄渶瑕佸鐞嗗尶鍚嶇敤鎴疯浆姝g殑鎯呭喌 + if (!user.getId().equals(userId)) { + logger.debug("鍖垮悕鐢ㄦ埛{}杞涓烘寮忕敤鎴穥}", userId, user.getId()); + + // 妫�鏌ュ尶鍚嶇敤鎴锋槸鍚︽湁闇�瑕佸悎骞剁殑淇℃伅 + if (userId < 0) { + // 杩欐槸鍖垮悕鐢ㄦ埛杞锛屾鏌ユ槸鍚︽湁涓存椂淇℃伅闇�瑕佸悎骞� + Optional<User> anonymousUserOpt = userRepository.findById(userId); + if (anonymousUserOpt.isPresent()) { + User anonymousUser = anonymousUserOpt.get(); + logger.debug("鍙戠幇鍖垮悕鐢ㄦ埛璁板綍锛屽噯澶囧悎骞朵俊鎭�"); + + // 鍚堝苟鍖垮悕鐢ㄦ埛鐨勪俊鎭埌姝e紡鐢ㄦ埛锛堝鏋滄寮忕敤鎴锋病鏈夎繖浜涗俊鎭級 + if (!StringUtils.hasText(user.getName()) && StringUtils.hasText(anonymousUser.getName())) { + user.setName(anonymousUser.getName()); + logger.debug("鍚堝苟鍖垮悕鐢ㄦ埛鐨勫鍚�: {}", anonymousUser.getName()); + } + + if (user.getGender() == null && anonymousUser.getGender() != null) { + user.setGender(anonymousUser.getGender()); + logger.debug("鍚堝苟鍖垮悕鐢ㄦ埛鐨勬�у埆: {}", anonymousUser.getGender()); + } + + if (user.getBirthday() == null && anonymousUser.getBirthday() != null) { + user.setBirthday(anonymousUser.getBirthday()); + logger.debug("鍚堝苟鍖垮悕鐢ㄦ埛鐨勭敓鏃�: {}", anonymousUser.getBirthday()); + } + + // 鍒犻櫎鍖垮悕鐢ㄦ埛璁板綍锛堝彲閫夛紝閬垮厤鏁版嵁鍐椾綑锛� + try { + userRepository.delete(anonymousUser); + logger.debug("鍒犻櫎鍖垮悕鐢ㄦ埛璁板綍: {}", userId); + } catch (Exception e) { + logger.warn("鍒犻櫎鍖垮悕鐢ㄦ埛璁板綍澶辫触: {}", e.getMessage()); + } + } + } + + userId = user.getId(); // 浣跨敤姝e紡鐢ㄦ埛ID + } + } else { + // 鎵嬫満鍙蜂笉瀛樺湪锛屾鏌ュ綋鍓嶇敤鎴稩D鏄惁瀛樺湪 + Optional<User> userOpt = userRepository.findById(userId); + if (userOpt.isPresent()) { + // 鐢ㄦ埛ID瀛樺湪锛屾洿鏂颁俊鎭� + user = userOpt.get(); + logger.debug("鏇存柊鐜版湁鐢ㄦ埛淇℃伅锛岀敤鎴稩D: {}", userId); + } else { + // 鐢ㄦ埛ID涓嶅瓨鍦紝鍒涘缓鏂扮敤鎴� + user = new User(); + user.setId(userId); + isNewUser = true; + logger.debug("鍒涘缓鏂扮敤鎴凤紝鐢ㄦ埛ID: {}", userId); + } + } + } else { + // 娌℃湁鎻愪緵鎵嬫満鍙凤紝鐩存帴鏍规嵁鐢ㄦ埛ID鏌ユ壘鎴栧垱寤� + Optional<User> userOpt = userRepository.findById(userId); + if (userOpt.isPresent()) { + user = userOpt.get(); + logger.debug("鏇存柊鐜版湁鐢ㄦ埛淇℃伅锛岀敤鎴稩D: {}", userId); + } else { + user = new User(); + user.setId(userId); + isNewUser = true; + logger.debug("鍒涘缓鏂扮敤鎴凤紝鐢ㄦ埛ID: {}", userId); + } + } + + // 鏇存柊鐢ㄦ埛鍩烘湰淇℃伅锛堜笉鍖呭惈澶村儚锛� + if (StringUtils.hasText(input.getName())) { + user.setName(input.getName()); + } + if (StringUtils.hasText(input.getPhone())) { + user.setPhone(input.getPhone()); + } + + // 澶勭悊鎬у埆杞崲锛歁ALE -> 1, FEMALE -> 0 + if ("MALE".equals(input.getGender())) { + user.setGender(1); + } else if ("FEMALE".equals(input.getGender())) { + user.setGender(0); + } + + // 澶勭悊鐢熸棩杞崲 + if (StringUtils.hasText(input.getBirthday())) { + try { + LocalDate birthday = LocalDate.parse(input.getBirthday()); + user.setBirthday(birthday); + } catch (Exception e) { + logger.warn("鐢熸棩鏍煎紡瑙f瀽澶辫触: {}", input.getBirthday(), e); + } + } + + // 澶勭悊wxopenid鏇存柊锛氫粠JWT token涓幏鍙栧綋鍓嶇敤鎴风殑wxopenid + try { + String token = userContextUtil.getTokenFromRequest(); + if (token != null && jwtUtil.validateToken(token)) { + Long currentUserId = jwtUtil.getUserIdFromToken(token); + String wxopenidFromToken = jwtUtil.getWxOpenidFromToken(token); + + // 濡傛灉token涓寘鍚玾xopenid锛屽垯鏇存柊鐢ㄦ埛鐨剋xopenid + if (StringUtils.hasText(wxopenidFromToken)) { + logger.debug("浠巘oken涓幏鍙栧埌wxopenid: {}", wxopenidFromToken); + + // 妫�鏌ヨ繖涓猳penid鏄惁宸茬粡琚叾浠栫敤鎴蜂娇鐢� + Optional<User> existingUserWithOpenid = userRepository.findByWxOpenid(wxopenidFromToken); + if (existingUserWithOpenid.isEmpty() || existingUserWithOpenid.get().getId().equals(user.getId())) { + user.setWxOpenid(wxopenidFromToken); + logger.debug("璁剧疆鐢ㄦ埛wxopenid: {}", wxopenidFromToken); + } else { + logger.warn("wxopenid {} 宸茶鍏朵粬鐢ㄦ埛浣跨敤锛岀敤鎴稩D: {}", wxopenidFromToken, existingUserWithOpenid.get().getId()); + } + } else { + logger.debug("token涓湭鍖呭惈wxopenid淇℃伅"); + } + } + } catch (Exception e) { + logger.warn("澶勭悊wxopenid鏇存柊鏃跺彂鐢熷紓甯�: {}", e.getMessage(), e); + } + + // 淇濆瓨鐢ㄦ埛鍩烘湰淇℃伅 + user = userRepository.save(user); + logger.debug("鐢ㄦ埛淇℃伅淇濆瓨鎴愬姛锛岀敤鎴稩D: {}, 鏄惁鏂扮敤鎴�: {}", user.getId(), isNewUser); + + // 澶勭悊澶村儚淇濆瓨 + if (StringUtils.hasText(input.getAvatar())) { + try { + logger.debug("寮�濮嬩繚瀛樼敤鎴峰ご鍍忥紝璺緞: {}", input.getAvatar()); + + // 鏋勫缓MediaSaveInput + MediaSaveInput mediaSaveInput = new MediaSaveInput(); + mediaSaveInput.setTargetType("player"); // 浣跨敤"player"浣滀负鐩爣绫诲瀷 + mediaSaveInput.setTargetId(user.getId()); + mediaSaveInput.setPath(input.getAvatar()); + mediaSaveInput.setMediaType(1); // 1琛ㄧず鍥剧墖 + mediaSaveInput.setFileSize(0L); // 璁剧疆榛樿鏂囦欢澶у皬涓�0锛岄伩鍏嶆暟鎹簱绾︽潫閿欒 + + // 浠庤矾寰勪腑鎻愬彇鏂囦欢鍚嶅拰鎵╁睍鍚� + String fileName = input.getAvatar(); + if (fileName.contains("/")) { + fileName = fileName.substring(fileName.lastIndexOf("/") + 1); + } + mediaSaveInput.setFileName(fileName); + + if (fileName.contains(".")) { + String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1); + mediaSaveInput.setFileExt(fileExt); + } + + // 淇濆瓨澶村儚濯掍綋璁板綍 + MediaSaveResponse saveResponse = mediaV2Service.saveMedia(mediaSaveInput); + if (saveResponse.getSuccess()) { + logger.debug("澶村儚淇濆瓨鎴愬姛锛屽獟浣揑D: {}", saveResponse.getMediaId()); + } else { + logger.warn("澶村儚淇濆瓨澶辫触: {}", saveResponse.getMessage()); + } + } catch (Exception e) { + logger.error("淇濆瓨澶村儚鏃跺彂鐢熼敊璇�", e); + // 澶村儚淇濆瓨澶辫触涓嶅奖鍝嶇敤鎴蜂俊鎭繚瀛� + } + } + + // 鏋勫缓杩斿洖缁撴灉 + UserProfileInfo result = new UserProfileInfo(); + result.setId(user.getId().toString()); + result.setName(user.getName()); + result.setPhone(user.getPhone()); + // 姝g‘澶勭悊鎬у埆杞崲锛�1 -> MALE, 0 -> FEMALE, null -> null + if (user.getGender() != null) { + result.setGender(user.getGender() == 1 ? "MALE" : "FEMALE"); + } else { + result.setGender(null); + } + result.setBirthday(user.getBirthday() != null ? user.getBirthday().toString() : null); + result.setWxOpenId(user.getWxOpenid()); + result.setUnionId(user.getWxUnionid()); + + // 鏌ユ壘骞惰缃ご鍍廢RL锛堜粠t_media琛ㄨ幏鍙栵級 + try { + List<Media> avatarMedias = mediaRepository.findByTargetTypeAndTargetIdAndState( + MediaTargetType.USER_AVATAR.getValue(), + user.getId(), + 1 + ); + if (!avatarMedias.isEmpty()) { + Media avatarMedia = avatarMedias.get(0); + result.setAvatar(buildFullMediaUrl(avatarMedia.getPath())); + logger.debug("璁剧疆澶村儚URL: {}", result.getAvatar()); + } + } catch (Exception e) { + logger.warn("鑾峰彇澶村儚澶辫触: {}", e.getMessage(), e); + } + + return result; + } catch (Exception e) { + logger.error("淇濆瓨鐢ㄦ埛淇℃伅澶辫触", e); + throw new RuntimeException("淇濆瓨鐢ㄦ埛淇℃伅澶辫触: " + e.getMessage()); + } + } } \ No newline at end of file -- Gitblit v1.8.0