package com.xuqm.push.controller; import com.xuqm.common.exception.BusinessException; import com.xuqm.common.model.ApiResponse; import com.xuqm.push.entity.DeviceTokenEntity; import com.xuqm.push.service.PushAccountService; import com.xuqm.push.service.PushAppSecretClient; import com.xuqm.push.service.PushDispatcher; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Map; @RestController @RequestMapping("/api/push/auth") public class PushAuthController { private final PushAccountService accountService; private final PushDispatcher pushDispatcher; private final PushAppSecretClient appSecretClient; public PushAuthController(PushAccountService accountService, PushDispatcher pushDispatcher, PushAppSecretClient appSecretClient) { this.accountService = accountService; this.pushDispatcher = pushDispatcher; this.appSecretClient = appSecretClient; } @PostMapping("/login") public ResponseEntity>> login( @RequestParam String appKey, @RequestParam String userId, @RequestParam String userSig, @RequestParam String packageName, @RequestParam DeviceTokenEntity.Vendor vendor, @RequestParam String token, @RequestParam(required = false) String platform, @RequestParam(required = false) String deviceId, @RequestParam(required = false) String brand, @RequestParam(required = false) String model, @RequestParam(required = false) String osVersion, @RequestParam(required = false) String appVersion) { validatePackageName(appKey, packageName); PushAccountService.LoginResult result = accountService.loginWithUserSig(appKey, userId, userSig); pushDispatcher.registerToken(appKey, userId, vendor, token, platform, deviceId, brand, model, osVersion, appVersion); Map response = Map.of( "pushToken", result.pushToken(), "userId", result.user().getUserId(), "appKey", appKey, "nickname", result.user().getNickname() != null ? result.user().getNickname() : "" ); return ResponseEntity.ok(ApiResponse.success(response)); } private void validatePackageName(String appKey, String packageName) { if (packageName == null || packageName.isBlank()) { throw new BusinessException(403, "packageName is required"); } PushAppSecretClient.PlatformInfo info = appSecretClient.getPlatformInfo(appKey); String android = info.androidPackageName(); String ios = info.iosBundleId(); String harmony = info.harmonyBundleName(); boolean anyConfigured = hasText(android) || hasText(ios) || hasText(harmony); if (anyConfigured) { boolean matches = packageName.equals(android) || packageName.equals(ios) || packageName.equals(harmony); if (!matches) throw new BusinessException(403, "包名与应用配置不匹配"); } } private static boolean hasText(String s) { return s != null && !s.isBlank(); } }