2026-04-21 22:07:29 +08:00
|
|
|
package com.xuqm.update.controller;
|
|
|
|
|
|
|
|
|
|
import com.xuqm.common.model.ApiResponse;
|
|
|
|
|
import com.xuqm.update.entity.AppVersionEntity;
|
|
|
|
|
import com.xuqm.update.repository.AppVersionRepository;
|
2026-04-29 12:33:25 +08:00
|
|
|
import com.xuqm.update.model.AppPackageInspectResult;
|
2026-04-21 22:07:29 +08:00
|
|
|
import org.springframework.http.ResponseEntity;
|
2026-04-24 16:16:33 +08:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
2026-04-21 22:07:29 +08:00
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.UUID;
|
2026-04-28 21:05:06 +08:00
|
|
|
import com.xuqm.update.service.UpdateAssetService;
|
2026-04-21 22:07:29 +08:00
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/v1/updates")
|
|
|
|
|
public class AppVersionController {
|
|
|
|
|
|
|
|
|
|
private final AppVersionRepository versionRepository;
|
2026-04-28 21:05:06 +08:00
|
|
|
private final UpdateAssetService updateAssetService;
|
2026-04-21 22:07:29 +08:00
|
|
|
|
2026-04-28 21:05:06 +08:00
|
|
|
public AppVersionController(AppVersionRepository versionRepository, UpdateAssetService updateAssetService) {
|
2026-04-21 22:07:29 +08:00
|
|
|
this.versionRepository = versionRepository;
|
2026-04-28 21:05:06 +08:00
|
|
|
this.updateAssetService = updateAssetService;
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/app/check")
|
|
|
|
|
public ResponseEntity<ApiResponse<Map<String, Object>>> checkUpdate(
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestParam AppVersionEntity.Platform platform,
|
|
|
|
|
@RequestParam int currentVersionCode) {
|
|
|
|
|
|
|
|
|
|
Optional<AppVersionEntity> latest = versionRepository
|
|
|
|
|
.findTopByAppIdAndPlatformAndPublishStatusOrderByVersionCodeDesc(
|
|
|
|
|
appId, platform, AppVersionEntity.PublishStatus.PUBLISHED);
|
|
|
|
|
|
|
|
|
|
if (latest.isEmpty() || latest.get().getVersionCode() <= currentVersionCode) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(Map.of("needsUpdate", false)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppVersionEntity v = latest.get();
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(Map.of(
|
|
|
|
|
"needsUpdate", true,
|
|
|
|
|
"versionName", v.getVersionName(),
|
|
|
|
|
"versionCode", v.getVersionCode(),
|
|
|
|
|
"downloadUrl", v.getDownloadUrl() != null ? v.getDownloadUrl() : "",
|
|
|
|
|
"changeLog", v.getChangeLog() != null ? v.getChangeLog() : "",
|
|
|
|
|
"forceUpdate", v.isForceUpdate(),
|
|
|
|
|
"appStoreUrl", v.getAppStoreUrl() != null ? v.getAppStoreUrl() : "",
|
|
|
|
|
"marketUrl", v.getMarketUrl() != null ? v.getMarketUrl() : ""
|
|
|
|
|
)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/app/upload")
|
|
|
|
|
public ResponseEntity<ApiResponse<AppVersionEntity>> upload(
|
|
|
|
|
@RequestParam String appId,
|
|
|
|
|
@RequestParam AppVersionEntity.Platform platform,
|
2026-04-29 12:33:25 +08:00
|
|
|
@RequestParam(required = false) String versionName,
|
|
|
|
|
@RequestParam(required = false) Integer versionCode,
|
2026-04-21 22:07:29 +08:00
|
|
|
@RequestParam(required = false) String changeLog,
|
|
|
|
|
@RequestParam(defaultValue = "false") boolean forceUpdate,
|
2026-04-29 00:34:17 +08:00
|
|
|
@RequestParam(required = false) MultipartFile apkFile,
|
|
|
|
|
@RequestParam(required = false) String scheduledPublishAt,
|
|
|
|
|
@RequestParam(required = false) String webhookUrl,
|
|
|
|
|
@RequestParam(required = false) String storeSubmitTargets,
|
|
|
|
|
@RequestParam(defaultValue = "false") boolean autoPublishAfterReview,
|
2026-04-29 15:46:40 +08:00
|
|
|
@RequestParam(defaultValue = "false") boolean publishImmediately,
|
|
|
|
|
@RequestParam(required = false) String packageName,
|
|
|
|
|
@RequestParam(required = false) String appStoreUrl,
|
|
|
|
|
@RequestParam(required = false) String marketUrl) throws Exception {
|
2026-04-21 22:07:29 +08:00
|
|
|
|
2026-04-29 15:46:40 +08:00
|
|
|
AppPackageInspectResult inspected = apkFile != null && !apkFile.isEmpty()
|
|
|
|
|
? updateAssetService.inspectAppPackage(apkFile)
|
|
|
|
|
: null;
|
|
|
|
|
String resolvedVersionName = hasText(versionName) ? versionName : (inspected != null ? inspected.versionName() : null);
|
|
|
|
|
Integer resolvedVersionCode = versionCode != null ? versionCode : (inspected != null ? inspected.versionCode() : null);
|
|
|
|
|
String resolvedPackageName = hasText(packageName) ? packageName : (inspected != null ? inspected.packageName() : null);
|
2026-04-29 12:33:25 +08:00
|
|
|
if (!hasText(resolvedVersionName) || resolvedVersionCode == null) {
|
|
|
|
|
throw new IllegalArgumentException("versionName and versionCode are required or must be readable from the uploaded package");
|
|
|
|
|
}
|
2026-04-29 15:46:40 +08:00
|
|
|
if (platform != AppVersionEntity.Platform.HARMONY && (apkFile == null || apkFile.isEmpty())) {
|
|
|
|
|
throw new IllegalArgumentException("apkFile is required for ANDROID and IOS releases");
|
|
|
|
|
}
|
|
|
|
|
if (platform == AppVersionEntity.Platform.HARMONY && !hasText(marketUrl)) {
|
|
|
|
|
throw new IllegalArgumentException("marketUrl is required for HARMONY releases");
|
|
|
|
|
}
|
|
|
|
|
if (platform == AppVersionEntity.Platform.HARMONY && !hasText(resolvedPackageName)) {
|
|
|
|
|
throw new IllegalArgumentException("packageName is required for HARMONY releases");
|
|
|
|
|
}
|
2026-04-29 12:33:25 +08:00
|
|
|
|
2026-04-21 22:07:29 +08:00
|
|
|
AppVersionEntity entity = new AppVersionEntity();
|
|
|
|
|
entity.setId(UUID.randomUUID().toString());
|
|
|
|
|
entity.setAppId(appId);
|
|
|
|
|
entity.setPlatform(platform);
|
2026-04-29 12:33:25 +08:00
|
|
|
entity.setVersionName(resolvedVersionName);
|
|
|
|
|
entity.setVersionCode(resolvedVersionCode);
|
2026-04-29 15:46:40 +08:00
|
|
|
entity.setDownloadUrl(platform == AppVersionEntity.Platform.HARMONY ? null : updateAssetService.storeAppPackage(apkFile));
|
2026-04-21 22:07:29 +08:00
|
|
|
entity.setChangeLog(changeLog);
|
|
|
|
|
entity.setForceUpdate(forceUpdate);
|
|
|
|
|
entity.setPublishStatus(AppVersionEntity.PublishStatus.DRAFT);
|
|
|
|
|
entity.setCreatedAt(LocalDateTime.now());
|
2026-04-29 00:34:17 +08:00
|
|
|
if (scheduledPublishAt != null && !scheduledPublishAt.isBlank()) {
|
|
|
|
|
entity.setScheduledPublishAt(LocalDateTime.parse(scheduledPublishAt));
|
|
|
|
|
}
|
|
|
|
|
entity.setWebhookUrl(webhookUrl);
|
|
|
|
|
entity.setStoreSubmitTargets(storeSubmitTargets);
|
|
|
|
|
entity.setAutoPublishAfterReview(autoPublishAfterReview);
|
2026-04-29 12:33:25 +08:00
|
|
|
entity.setPackageName(resolvedPackageName);
|
2026-04-29 15:46:40 +08:00
|
|
|
entity.setAppStoreUrl(appStoreUrl);
|
|
|
|
|
entity.setMarketUrl(marketUrl);
|
|
|
|
|
if (publishImmediately) {
|
|
|
|
|
entity.setPublishStatus(AppVersionEntity.PublishStatus.PUBLISHED);
|
|
|
|
|
entity.setGrayEnabled(false);
|
|
|
|
|
entity.setGrayPercent(0);
|
|
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
return ResponseEntity.ok(ApiResponse.success(versionRepository.save(entity)));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-29 12:33:25 +08:00
|
|
|
@PostMapping("/app/inspect")
|
|
|
|
|
public ResponseEntity<ApiResponse<AppPackageInspectResult>> inspect(@RequestParam(required = false) MultipartFile apkFile) throws Exception {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(updateAssetService.inspectAppPackage(apkFile)));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 22:07:29 +08:00
|
|
|
@PostMapping("/app/{id}/publish")
|
|
|
|
|
public ResponseEntity<ApiResponse<AppVersionEntity>> publish(@PathVariable String id) {
|
|
|
|
|
AppVersionEntity entity = versionRepository.findById(id).orElseThrow();
|
|
|
|
|
entity.setPublishStatus(AppVersionEntity.PublishStatus.PUBLISHED);
|
2026-04-24 16:16:33 +08:00
|
|
|
entity.setGrayEnabled(false);
|
|
|
|
|
entity.setGrayPercent(0);
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(versionRepository.save(entity)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/app/{id}/unpublish")
|
|
|
|
|
public ResponseEntity<ApiResponse<AppVersionEntity>> unpublish(@PathVariable String id) {
|
|
|
|
|
AppVersionEntity entity = versionRepository.findById(id).orElseThrow();
|
|
|
|
|
entity.setPublishStatus(AppVersionEntity.PublishStatus.DEPRECATED);
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(versionRepository.save(entity)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("/app/{id}/gray")
|
|
|
|
|
public ResponseEntity<ApiResponse<AppVersionEntity>> gray(
|
|
|
|
|
@PathVariable String id,
|
|
|
|
|
@RequestBody Map<String, Object> body) {
|
|
|
|
|
AppVersionEntity entity = versionRepository.findById(id).orElseThrow();
|
|
|
|
|
entity.setGrayEnabled(Boolean.TRUE.equals(body.get("enabled")));
|
|
|
|
|
entity.setGrayPercent(body.get("percent") instanceof Number n ? n.intValue() : 0);
|
|
|
|
|
entity.setPublishStatus(AppVersionEntity.PublishStatus.PUBLISHED);
|
2026-04-21 22:07:29 +08:00
|
|
|
return ResponseEntity.ok(ApiResponse.success(versionRepository.save(entity)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/app/list")
|
|
|
|
|
public ResponseEntity<ApiResponse<List<AppVersionEntity>>> list(
|
|
|
|
|
@RequestParam String appId, @RequestParam AppVersionEntity.Platform platform) {
|
|
|
|
|
return ResponseEntity.ok(ApiResponse.success(
|
|
|
|
|
versionRepository.findByAppIdAndPlatformOrderByVersionCodeDesc(appId, platform)));
|
|
|
|
|
}
|
2026-04-29 12:33:25 +08:00
|
|
|
|
|
|
|
|
private boolean hasText(String value) {
|
|
|
|
|
return value != null && !value.isBlank();
|
|
|
|
|
}
|
2026-04-21 22:07:29 +08:00
|
|
|
}
|