mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 06:29:20 +08:00
refactor: 修改结构 功能动态从后端获取
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
-- 开放接口:浏览器扩展功能清单/脚本读取
|
||||
INSERT INTO `sys_permission` (`parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT (SELECT id FROM `sys_permission` WHERE `code` = 'MODULE:open' LIMIT 1), 'open:scope:extension_feature:read', '开放-浏览器扩展功能读取', 'api', 1, 'GET /open/v1/extension/features', 90
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `code` = 'open:scope:extension_feature:read');
|
||||
+3
-1
@@ -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() {
|
||||
|
||||
+42
@@ -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));
|
||||
}
|
||||
}
|
||||
+21
@@ -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;
|
||||
}
|
||||
+16
@@ -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<>();
|
||||
}
|
||||
+18
@@ -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;
|
||||
}
|
||||
+11
@@ -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);
|
||||
}
|
||||
+81
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -177,6 +177,10 @@ INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status
|
||||
SELECT 5208, (SELECT id FROM `sys_permission` WHERE `code` = 'MODULE:open' LIMIT 1), 'open:scope:pdd_shop:read', '开放-拼多多渠道店铺查询', 'api', 1, 'POST /open/v1/channel/pdd/shop/list', 80
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `code` = 'open:scope:pdd_shop:read');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 5209, (SELECT id FROM `sys_permission` WHERE `code` = 'MODULE:open' LIMIT 1), 'open:scope:extension_feature:read', '开放-浏览器扩展功能读取', 'api', 1, 'GET /open/v1/extension/features', 90
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `code` = 'open:scope:extension_feature:read');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 1101, (SELECT id FROM `sys_permission` WHERE `code` = 'system:menu' LIMIT 1), 'system:menu:read', '菜单查看', 'api', 1, '系统管理-菜单查看', 14
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `code` = 'system:menu:read');
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"schemaVersion": 1,
|
||||
"updatedAt": "2026-08-16T10:21:40.757Z",
|
||||
"features": [
|
||||
{
|
||||
"id": "pdd-ad-fee",
|
||||
"name": "拼多多商品推广费同步",
|
||||
"version": "0.1.0",
|
||||
"description": "从推广总览读取单日总花费并同步到 BI(壳内嵌入执行;远程包用于版本校验)",
|
||||
"matchUrlPatterns": [
|
||||
"*://yingxiao.pinduoduo.com/*",
|
||||
"https://yingxiao.pinduoduo.com/"
|
||||
],
|
||||
"entry": "pdd-ad-fee.js",
|
||||
"sha256": "56dc0b6672a9cac6a7cffa8ee4139dc31a2bce37975d8a57b67f6a0e77221c6c",
|
||||
"executionWorld": "extension"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
var EcbiFeaturePddAdFee=function(){"use strict";const e="pdd-ad-fee",E={id:e,version:"0.1.0",executionWorld:"extension"},_=globalThis;return _.__ECBI_FEATURES__=_.__ECBI_FEATURES__??{},_.__ECBI_FEATURES__[e]=E,E}();
|
||||
//# sourceMappingURL=pdd-ad-fee.js.map
|
||||
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"pdd-ad-fee.js","sources":["../src/features/pdd-ad-fee/index.ts"],"sourcesContent":["/**\n * 远程功能包入口(IIFE 构建)。\n * 当前阶段导出元数据;真正 UI 仍由壳内 embedded 模块挂载。\n * 后续可将 FloatingPanel 整包打入此入口并在 main world 执行。\n */\nconst FEATURE_ID = 'pdd-ad-fee';\n\nexport interface RemoteFeatureExport {\n id: string;\n version: string;\n executionWorld: 'extension';\n}\n\nconst meta: RemoteFeatureExport = {\n id: FEATURE_ID,\n version: '0.1.0',\n executionWorld: 'extension',\n};\n\ndeclare global {\n interface Window {\n __ECBI_FEATURES__?: Record<string, RemoteFeatureExport>;\n }\n}\n\nconst g = globalThis as typeof globalThis & {\n __ECBI_FEATURES__?: Record<string, RemoteFeatureExport>;\n};\ng.__ECBI_FEATURES__ = g.__ECBI_FEATURES__ ?? {};\ng.__ECBI_FEATURES__[FEATURE_ID] = meta;\n\nexport default meta;\n"],"names":["FEATURE_ID","meta","g"],"mappings":"gDAKA,MAAMA,EAAa,aAQbC,EAA4B,CAChC,GAAID,EACJ,QAAS,QACT,eAAgB,WAClB,EAQME,EAAI,WAGV,OAAAA,EAAE,kBAAoBA,EAAE,mBAAqB,CAAA,EAC7CA,EAAE,kBAAkBF,CAAU,EAAIC"}
|
||||
Reference in New Issue
Block a user