refactor: 修改结构 功能动态从后端获取

This commit is contained in:
2026-08-16 19:41:44 +08:00
parent 8a8e36602f
commit 481518500e
27 changed files with 760 additions and 107 deletions
@@ -17,6 +17,7 @@ public final class OpenApiScopeConstants {
public static final String META_READ = "open:scope:meta:read";
public static final String PDD_GOODS_AD_FEE_IMPORT = "open:scope:pdd_goods_ad_fee:import";
public static final String PDD_SHOP_READ = "open:scope:pdd_shop:read";
public static final String EXTENSION_FEATURE_READ = "open:scope:extension_feature:read";
public static final List<String> ALL = List.of(
ORDER_IMPORT,
@@ -26,7 +27,8 @@ public final class OpenApiScopeConstants {
IMPORT_TASK_READ,
META_READ,
PDD_GOODS_AD_FEE_IMPORT,
PDD_SHOP_READ
PDD_SHOP_READ,
EXTENSION_FEATURE_READ
);
private OpenApiScopeConstants() {
@@ -0,0 +1,42 @@
package top.xyquan.ecomdmbbackend.modules.open.api.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.xyquan.ecomdmbbackend.common.annotation.RequireOpenScope;
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
import top.xyquan.ecomdmbbackend.modules.open.api.constants.OpenApiScopeConstants;
import top.xyquan.ecomdmbbackend.modules.open.api.model.vo.ExtensionFeatureManifestVO;
import top.xyquan.ecomdmbbackend.modules.open.api.model.vo.ExtensionFeatureScriptVO;
import top.xyquan.ecomdmbbackend.modules.open.api.service.ExtensionFeatureService;
/**
* 浏览器扩展功能清单 / 脚本。
* 数据来自 classpath:static/extension-features/(与构建产物 dist-features 对齐)。
*/
@RestController
@RequestMapping("/api/open/v1/extension/features")
@RequiredArgsConstructor
@Tag(name = "开放接口-浏览器扩展功能", description = "API Key 认证;清单为静态 JSON,脚本同目录按需下载")
public class OpenExtensionFeatureController {
private final ExtensionFeatureService extensionFeatureService;
@GetMapping
@RequireOpenScope(value = OpenApiScopeConstants.EXTENSION_FEATURE_READ, description = "查询浏览器扩展功能清单")
@Operation(summary = "获取功能清单", description = "读取 static/extension-features/manifest.json")
public ApiResponse<ExtensionFeatureManifestVO> listFeatures() {
return ApiResponse.success("获取扩展功能清单成功", extensionFeatureService.getManifest());
}
@GetMapping("/{featureId}/script")
@RequireOpenScope(value = OpenApiScopeConstants.EXTENSION_FEATURE_READ, description = "下载浏览器扩展功能脚本")
@Operation(summary = "下载功能脚本", description = "按清单 entry 读取同目录 JS,返回 content 文本")
public ApiResponse<ExtensionFeatureScriptVO> getScript(@PathVariable String featureId) {
return ApiResponse.success("获取扩展功能脚本成功", extensionFeatureService.getScript(featureId));
}
}
@@ -0,0 +1,21 @@
package top.xyquan.ecomdmbbackend.modules.open.api.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
@Schema(description = "浏览器扩展单个功能项")
public class ExtensionFeatureItemVO {
private String id;
private String name;
private String version;
private String description;
private List<String> matchUrlPatterns = new ArrayList<>();
private String entry;
private String sha256;
private String executionWorld;
}
@@ -0,0 +1,16 @@
package top.xyquan.ecomdmbbackend.modules.open.api.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
@Schema(description = "浏览器扩展功能清单(对应静态目录 manifest.json)")
public class ExtensionFeatureManifestVO {
private Integer schemaVersion;
private String updatedAt;
private List<ExtensionFeatureItemVO> features = new ArrayList<>();
}
@@ -0,0 +1,18 @@
package top.xyquan.ecomdmbbackend.modules.open.api.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "浏览器扩展功能脚本内容")
public class ExtensionFeatureScriptVO {
private String featureId;
private String version;
private String sha256;
private String content;
}
@@ -0,0 +1,11 @@
package top.xyquan.ecomdmbbackend.modules.open.api.service;
import top.xyquan.ecomdmbbackend.modules.open.api.model.vo.ExtensionFeatureManifestVO;
import top.xyquan.ecomdmbbackend.modules.open.api.model.vo.ExtensionFeatureScriptVO;
public interface ExtensionFeatureService {
ExtensionFeatureManifestVO getManifest();
ExtensionFeatureScriptVO getScript(String featureId);
}
@@ -0,0 +1,81 @@
package top.xyquan.ecomdmbbackend.modules.open.api.service.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
import top.xyquan.ecomdmbbackend.modules.open.api.model.vo.ExtensionFeatureItemVO;
import top.xyquan.ecomdmbbackend.modules.open.api.model.vo.ExtensionFeatureManifestVO;
import top.xyquan.ecomdmbbackend.modules.open.api.model.vo.ExtensionFeatureScriptVO;
import top.xyquan.ecomdmbbackend.modules.open.api.service.ExtensionFeatureService;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
@Service
@RequiredArgsConstructor
public class ExtensionFeatureServiceImpl implements ExtensionFeatureService {
private static final String MANIFEST_PATH = "static/extension-features/manifest.json";
private static final String FEATURE_DIR = "static/extension-features/";
private final ObjectMapper objectMapper;
@Override
public ExtensionFeatureManifestVO getManifest() {
Resource resource = new ClassPathResource(MANIFEST_PATH);
if (!resource.exists()) {
throw new BusinessException("扩展功能清单不存在,请先部署 dist-features 到 static/extension-features/");
}
try (InputStream in = resource.getInputStream()) {
return objectMapper.readValue(in, ExtensionFeatureManifestVO.class);
} catch (IOException e) {
throw new BusinessException("读取扩展功能清单失败: " + e.getMessage());
}
}
@Override
public ExtensionFeatureScriptVO getScript(String featureId) {
if (!StringUtils.hasText(featureId)) {
throw new BusinessException("featureId 不能为空");
}
ExtensionFeatureManifestVO manifest = getManifest();
ExtensionFeatureItemVO item = manifest.getFeatures().stream()
.filter(f -> featureId.equals(f.getId()))
.findFirst()
.orElseThrow(() -> new BusinessException("未知功能: " + featureId));
String entry = item.getEntry();
if (!StringUtils.hasText(entry)) {
throw new BusinessException("功能未配置入口脚本: " + featureId);
}
// 防止路径穿越
Path normalized = Paths.get(entry).normalize();
if (normalized.isAbsolute() || normalized.startsWith("..") || entry.contains("..")) {
throw new BusinessException("非法脚本路径");
}
Resource resource = new ClassPathResource(FEATURE_DIR + normalized);
if (!resource.exists()) {
throw new BusinessException("功能脚本文件不存在: " + entry);
}
try (InputStream in = resource.getInputStream()) {
String content = StreamUtils.copyToString(in, StandardCharsets.UTF_8);
return new ExtensionFeatureScriptVO(
item.getId(),
item.getVersion(),
item.getSha256(),
content
);
} catch (IOException e) {
throw new BusinessException("读取功能脚本失败: " + e.getMessage());
}
}
}