refactor: 增加ui resource接口

This commit is contained in:
zhouhao 2025-05-20 15:31:49 +08:00
parent 80ed50211a
commit 520ca24fed
5 changed files with 134 additions and 1 deletions

View File

@ -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();
}
}

View File

@ -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");
}
}

View File

@ -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<Resource> cache;
@Override
public String getType() {
return TYPE;
}
@Override
public Flux<Resource> getResources() {
return Flux.fromIterable(cache == null ? cache = read() : cache);
}
@SneakyThrows
private List<Resource> read() {
List<Resource> 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<Resource> getResources(Collection<String> 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));
}
}
}

View File

@ -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<Object> getUIResources() {
return resourceManager
.getResources(UiResourceProvider.TYPE)
.map(resource->resource.as(HashMap.class));
}
@GetMapping("/{type}")
@SneakyThrows
public Flux<String> getResources(@PathVariable String type) {

View File

@ -1 +1,2 @@
org.jetlinks.community.configuration.CommonConfiguration
org.jetlinks.community.configuration.CommonConfiguration
org.jetlinks.community.configuration.UiResourceConfiguration