diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/UiResourceConfiguration.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/UiResourceConfiguration.java new file mode 100644 index 00000000..7d3c70a9 --- /dev/null +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/configuration/UiResourceConfiguration.java @@ -0,0 +1,24 @@ +package org.jetlinks.community.configuration; + +import org.jetlinks.community.resource.ui.UiMenuResourceProvider; +import org.jetlinks.community.resource.ui.UiResourceProvider; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; + +@AutoConfiguration +@ConditionalOnProperty(prefix = "jetlinks.ui", name = "enabled", havingValue = "true", matchIfMissing = true) +public class UiResourceConfiguration { + + + @Bean + public UiResourceProvider uiResourceProvider() { + return new UiResourceProvider(); + } + + @Bean + public UiMenuResourceProvider uiMenuResourceProvider() { + return new UiMenuResourceProvider(); + } + +} diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiMenuResourceProvider.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiMenuResourceProvider.java new file mode 100644 index 00000000..1ab070e7 --- /dev/null +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiMenuResourceProvider.java @@ -0,0 +1,14 @@ +package org.jetlinks.community.resource.ui; + +import lombok.extern.slf4j.Slf4j; +import org.jetlinks.community.resource.ClassPathJsonResourceProvider; + +@Slf4j +public class UiMenuResourceProvider extends ClassPathJsonResourceProvider { + public static final String TYPE = "ui-menus"; + + + public UiMenuResourceProvider() { + super(TYPE, "classpath*:/ui/*/baseMenu.json"); + } +} \ No newline at end of file diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiResourceProvider.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiResourceProvider.java new file mode 100644 index 00000000..174878d1 --- /dev/null +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/resource/ui/UiResourceProvider.java @@ -0,0 +1,83 @@ +package org.jetlinks.community.resource.ui; + +import lombok.Getter; +import lombok.Setter; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.jetlinks.community.resource.Resource; +import org.jetlinks.community.resource.ResourceProvider; +import org.jetlinks.community.resource.SimpleResource; +import org.jetlinks.community.utils.ObjectMappers; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.util.StreamUtils; +import reactor.core.publisher.Flux; + +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +@Slf4j +public class UiResourceProvider implements ResourceProvider { + public static final String TYPE = "ui"; + + private static final ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + + private List cache; + + @Override + public String getType() { + return TYPE; + } + + @Override + public Flux getResources() { + return Flux.fromIterable(cache == null ? cache = read() : cache); + } + + @SneakyThrows + private List read() { + List resources = new ArrayList<>(); + try { + for (org.springframework.core.io.Resource resource : resolver.getResources("classpath*:/ui/*/package.json")) { + try (InputStream stream = resource.getInputStream()) { + String s = StreamUtils.copyToString(stream, StandardCharsets.UTF_8); + Module m = ObjectMappers.parseJson(s, Module.class); + String path = resource.getURL().getPath(); + String[] parts = path.split("/"); + if (parts.length > 2) { + m.setPath(parts[parts.length - 3] + "/" + parts[parts.length - 2]); + resources.add(m.toResource()); + } + } + } + } catch (Throwable e) { + log.warn("load ui resource error", e); + } + return resources; + } + + + @Override + public Flux getResources(Collection id) { + return Flux.empty(); + } + + + @Getter + @Setter + public static class Module { + private String id; + private String name; + private String description; + private String path; + + public SimpleResource toResource() { + id = StringUtils.isBlank(id) ? name : id; + return SimpleResource.of(id, TYPE, ObjectMappers.toJsonString(this)); + } + } +} \ No newline at end of file diff --git a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/web/SystemResourcesController.java b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/web/SystemResourcesController.java index ee6c9157..f33f9665 100644 --- a/jetlinks-components/common-component/src/main/java/org/jetlinks/community/web/SystemResourcesController.java +++ b/jetlinks-components/common-component/src/main/java/org/jetlinks/community/web/SystemResourcesController.java @@ -9,6 +9,7 @@ import org.hswebframework.web.authorization.exception.UnAuthorizedException; import org.jetlinks.community.resource.Resource; import org.jetlinks.community.resource.ResourceManager; import org.jetlinks.community.resource.TypeScriptDeclareResourceProvider; +import org.jetlinks.community.resource.ui.UiResourceProvider; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @@ -17,6 +18,7 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.Collections; +import java.util.HashMap; @RestController @RequestMapping("/system/resources") @@ -26,6 +28,15 @@ public class SystemResourcesController { private final ResourceManager resourceManager; + @GetMapping("/ui") + @SneakyThrows + @Authorize(merge = false) + public Flux getUIResources() { + return resourceManager + .getResources(UiResourceProvider.TYPE) + .map(resource->resource.as(HashMap.class)); + } + @GetMapping("/{type}") @SneakyThrows public Flux getResources(@PathVariable String type) { diff --git a/jetlinks-components/common-component/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/jetlinks-components/common-component/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index c761205a..b66587f1 100644 --- a/jetlinks-components/common-component/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/jetlinks-components/common-component/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1,2 @@ -org.jetlinks.community.configuration.CommonConfiguration \ No newline at end of file +org.jetlinks.community.configuration.CommonConfiguration +org.jetlinks.community.configuration.UiResourceConfiguration \ No newline at end of file