| | |
| | | import com.rongyichuang.player.repository.PlayerRepository; |
| | | import com.rongyichuang.user.entity.User; |
| | | import com.rongyichuang.user.repository.UserRepository; |
| | | import com.rongyichuang.common.entity.Media; |
| | | import com.rongyichuang.common.repository.MediaRepository; |
| | | import com.rongyichuang.common.enums.MediaTargetType; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.security.authentication.BadCredentialsException; |
| | | import org.springframework.security.crypto.password.PasswordEncoder; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.List; |
| | | import java.util.Optional; |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.spec.IvParameterSpec; |
| | |
| | | |
| | | @Autowired |
| | | private WechatApiService wechatApiService; |
| | | |
| | | @Autowired |
| | | private MediaRepository mediaRepository; |
| | | |
| | | @Value("${app.base-url}") |
| | | private String baseUrl; |
| | | |
| | | /** |
| | | * 用户登录 |
| | |
| | | logger.warn("⚠️ unionid为空或用户已找到,跳过unionid查找"); |
| | | } |
| | | |
| | | // 4. 如果都没找到,创建新用户 |
| | | // 4. 如果都没找到用户,不创建新用户,只记录登录信息 |
| | | if (user == null) { |
| | | logger.info("未找到现有用户,开始创建新用户"); |
| | | logger.info("新用户信息:"); |
| | | logger.info("未找到现有用户,普通访问用户不创建user记录,只记录登录信息"); |
| | | logger.info("访问用户信息:"); |
| | | logger.info("- openid: {}", wxLoginRequest.getWxOpenid()); |
| | | logger.info("- unionid: {}", wxLoginRequest.getWxUnionid()); |
| | | logger.info("- phone: {}", wxLoginRequest.getPhone()); |
| | | |
| | | // 记录登录信息到t_login_record,但不创建用户 |
| | | logger.info("步骤3: 记录访问用户的登录信息"); |
| | | WxLoginRecord loginRecord = null; |
| | | try { |
| | | user = new User(); |
| | | user.setWxOpenid(wxLoginRequest.getWxOpenid()); |
| | | user.setWxUnionid(wxLoginRequest.getWxUnionid()); |
| | | user.setName("微信用户"); // 默认名称,后续可以更新 |
| | | user.setPhone(wxLoginRequest.getPhone()); // 如果有授权手机号 |
| | | |
| | | user = userRepository.save(user); |
| | | isNewUser = true; |
| | | |
| | | logger.info("✅ 成功创建新的微信用户"); |
| | | logger.info("- 新用户ID: {}", user.getId()); |
| | | logger.info("- 新用户姓名: {}", user.getName()); |
| | | logger.info("- 新用户手机号: {}", user.getPhone()); |
| | | |
| | | loginRecord = wxLoginRecordService.createLoginRecord( |
| | | wxLoginRequest.getWxOpenid(), |
| | | wxLoginRequest.getWxUnionid(), |
| | | null, // 没有用户ID |
| | | wxLoginRequest.getLoginIp(), |
| | | wxLoginRequest.getDeviceInfo(), |
| | | wxLoginRequest.getSessionKey(), |
| | | wxLoginRequest.getPhoneAuthorized() |
| | | ); |
| | | logger.info("✅ 成功创建访问用户登录记录,记录ID: {}", loginRecord.getId()); |
| | | } catch (Exception e) { |
| | | logger.error("❌ 创建新用户失败"); |
| | | logger.error("异常信息: {}", e.getMessage()); |
| | | logger.error("❌ 创建登录记录失败: {}", e.getMessage()); |
| | | logger.error("异常堆栈:", e); |
| | | throw new RuntimeException("创建新用户失败: " + e.getMessage(), e); |
| | | throw new RuntimeException("创建登录记录失败: " + e.getMessage(), e); |
| | | } |
| | | |
| | | // 返回访问用户的响应(无用户信息,无token) |
| | | WxLoginResponse response = new WxLoginResponse(); |
| | | response.setSuccess(true); |
| | | response.setMessage("访问成功"); |
| | | response.setSessionKey(wxLoginRequest.getSessionKey()); |
| | | response.setIsNewUser(false); |
| | | response.setHasEmployee(false); |
| | | response.setHasJudge(false); |
| | | response.setHasPlayer(false); |
| | | |
| | | logger.info("=== 微信访问流程完成(无用户创建) ==="); |
| | | return response; |
| | | } |
| | | |
| | | // 5. 记录登录信息到新表 |
| | |
| | | throw new RuntimeException("构建基础用户信息失败: " + e.getMessage(), e); |
| | | } |
| | | |
| | | // 9. 设置所有关联的角色信息 |
| | | logger.info("步骤8: 设置角色详细信息"); |
| | | // 9. 获取并设置用户头像 |
| | | logger.info("步骤8: 获取用户头像"); |
| | | try { |
| | | List<Media> avatarMediaList = mediaRepository.findByTargetTypeAndTargetIdAndState( |
| | | MediaTargetType.USER_AVATAR.getValue(), |
| | | user.getId(), |
| | | 1 |
| | | ); |
| | | if (!avatarMediaList.isEmpty()) { |
| | | Media avatarMedia = avatarMediaList.get(0); |
| | | String fullAvatarUrl = buildFullMediaUrl(avatarMedia.getPath()); |
| | | userInfo.setAvatarUrl(fullAvatarUrl); |
| | | logger.info("✅ 找到用户头像: {} -> {}", avatarMedia.getPath(), fullAvatarUrl); |
| | | } else { |
| | | logger.info("用户{}没有找到头像", user.getId()); |
| | | } |
| | | } catch (Exception e) { |
| | | logger.error("❌ 获取用户头像失败: {}", e.getMessage()); |
| | | logger.error("异常堆栈:", e); |
| | | } |
| | | |
| | | // 10. 设置所有关联的角色信息 |
| | | logger.info("步骤9: 设置角色详细信息"); |
| | | |
| | | if (employeeOpt.isPresent()) { |
| | | try { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 构建完整的媒体URL |
| | | * @param path 媒体路径 |
| | | * @return 完整的URL |
| | | */ |
| | | private String buildFullMediaUrl(String path) { |
| | | if (path == null || path.trim().isEmpty()) { |
| | | return null; |
| | | } |
| | | |
| | | // 如果路径已经是完整URL,直接返回 |
| | | if (path.startsWith("http://") || path.startsWith("https://")) { |
| | | return path; |
| | | } |
| | | |
| | | // 构建完整URL |
| | | String cleanBaseUrl = baseUrl.endsWith("/") ? baseUrl.substring(0, baseUrl.length() - 1) : baseUrl; |
| | | String cleanPath = path.startsWith("/") ? path : "/" + path; |
| | | return cleanBaseUrl + cleanPath; |
| | | } |
| | | |
| | | /** |
| | | * 解密微信手机号 |
| | | */ |
| | | public PhoneDecryptResponse decryptPhoneNumber(String encryptedData, String iv, String sessionKey) { |