2026-05-14 23:40:35 +08:00
|
|
|
package com.xuqm.push.controller;
|
|
|
|
|
|
2026-05-22 16:41:17 +08:00
|
|
|
import com.xuqm.common.exception.BusinessException;
|
2026-05-14 23:40:35 +08:00
|
|
|
import com.xuqm.common.model.ApiResponse;
|
|
|
|
|
import com.xuqm.push.entity.DeviceTokenEntity;
|
|
|
|
|
import com.xuqm.push.service.PushAccountService;
|
2026-05-22 16:41:17 +08:00
|
|
|
import com.xuqm.push.service.PushAppSecretClient;
|
2026-05-14 23:40:35 +08:00
|
|
|
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;
|
2026-05-22 16:41:17 +08:00
|
|
|
private final PushAppSecretClient appSecretClient;
|
2026-05-14 23:40:35 +08:00
|
|
|
|
2026-05-22 16:41:17 +08:00
|
|
|
public PushAuthController(PushAccountService accountService, PushDispatcher pushDispatcher,
|
|
|
|
|
PushAppSecretClient appSecretClient) {
|
2026-05-14 23:40:35 +08:00
|
|
|
this.accountService = accountService;
|
|
|
|
|
this.pushDispatcher = pushDispatcher;
|
2026-05-22 16:41:17 +08:00
|
|
|
this.appSecretClient = appSecretClient;
|
2026-05-14 23:40:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/login")
|
|
|
|
|
public ResponseEntity<ApiResponse<Map<String, Object>>> login(
|
|
|
|
|
@RequestParam String appKey,
|
|
|
|
|
@RequestParam String userId,
|
|
|
|
|
@RequestParam String userSig,
|
2026-05-22 16:41:17 +08:00
|
|
|
@RequestParam String packageName,
|
2026-05-14 23:40:35 +08:00
|
|
|
@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) {
|
|
|
|
|
|
2026-05-22 16:41:17 +08:00
|
|
|
validatePackageName(appKey, packageName);
|
2026-05-14 23:40:35 +08:00
|
|
|
PushAccountService.LoginResult result = accountService.loginWithUserSig(appKey, userId, userSig);
|
|
|
|
|
pushDispatcher.registerToken(appKey, userId, vendor, token, platform, deviceId, brand, model, osVersion, appVersion);
|
|
|
|
|
|
|
|
|
|
Map<String, Object> 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));
|
|
|
|
|
}
|
2026-05-22 16:41:17 +08:00
|
|
|
|
|
|
|
|
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(); }
|
2026-05-14 23:40:35 +08:00
|
|
|
}
|