fix: 获取web配置从Redis中获取

This commit is contained in:
Bunny 2025-01-08 23:20:23 +08:00
parent 48ebe2a66b
commit 1b9bf67b41
7 changed files with 42 additions and 196 deletions

View File

@ -12,6 +12,16 @@ Pure-admin文档https://pure-admin.github.io/pure-admin-doc
正式线上预览地址http://bunny-web.site/#/welcome 正式线上预览地址http://bunny-web.site/#/welcome
> 如果发现网站打开白屏很有可能是因为更新造成的,因为需要维护删除不需要的功能可能会导致这个情况,但基本不会遇到。
>
> 谷歌方式如下:
>
> ![image-20250108225817891](http://116.196.101.14:9000/docs/image-20250108225817891.png)
>
> Edge方式如下
>
> ![image-20250108225915189](http://116.196.101.14:9000/docs/image-20250108225915189.png)
**打包视频** **打包视频**
https://www.bilibili.com/video/BV1AYm8YSEKY/ https://www.bilibili.com/video/BV1AYm8YSEKY/
@ -1200,6 +1210,8 @@ mvn clean package -Pprod -DskipTests
mvn clean package -Ptest -DskipTests mvn clean package -Ptest -DskipTests
``` ```
**:star: 如果不需要内置文件操作什么的这一步可以忽略**
![image-20241108153705104](http://116.196.101.14:9000/docs/auth/image-20241108153705104.png) ![image-20241108153705104](http://116.196.101.14:9000/docs/auth/image-20241108153705104.png)
这个文件夹是必须的 这个文件夹是必须的

View File

@ -10,6 +10,7 @@ public class RedisUserConstant {
// 过期时间 // 过期时间
public static final Long REDIS_EXPIRATION_TIME = 7L;// 7 /分钟 Redis过期 public static final Long REDIS_EXPIRATION_TIME = 7L;// 7 /分钟 Redis过期
public static final Integer Cookie_EXPIRATION_TIME = 5 * 60 * 60;// cookies 过期时间 5 分钟 public static final Integer Cookie_EXPIRATION_TIME = 5 * 60 * 60;// cookies 过期时间 5 分钟
public static final String WEB_CONFIG_KEY = "webConfig::platformConfig";// web配置
private static final String ADMIN_LOGIN_INFO_PREFIX = "admin::login_info::"; private static final String ADMIN_LOGIN_INFO_PREFIX = "admin::login_info::";
private static final String ADMIN_EMAIL_CODE_PREFIX = "admin::email_code::"; private static final String ADMIN_EMAIL_CODE_PREFIX = "admin::email_code::";

View File

@ -1,121 +0,0 @@
package cn.bunny.dao.vo.configuration;
import com.alibaba.fastjson2.annotation.JSONField;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Schema(name = "WebConfigurationVo对象", title = "前端配置获取返回结果", description = "前端配置获取返回结果")
public class WebConfigurationVo {
@Schema(name = "Version", description = "应用程序的版本")
@JSONField(name = "Version")
private String version;
@Schema(name = "Title", description = "应用程序的标题")
@JSONField(name = "Title")
private String title;
@Schema(name = "Copyright", description = "版权信息")
@JSONField(name = "Copyright")
private String copyright;
@Schema(name = "FixedHeader", description = "头部是否固定")
@JSONField(name = "FixedHeader")
private boolean fixedHeader;
@Schema(name = "HiddenSideBar", description = "侧边栏是否隐藏")
@JSONField(name = "HiddenSideBar")
private boolean hiddenSideBar;
@Schema(name = "MultiTagsCache", description = "是否缓存多个标签")
@JSONField(name = "MultiTagsCache")
private boolean multiTagsCache;
@Schema(name = "KeepAlive", description = "是否持久化")
@JSONField(name = "KeepAlive")
private boolean keepAlive;
@Schema(name = "Locale", description = "语言类型")
@JSONField(name = "Locale")
private String locale;
@Schema(name = "Layout", description = "应用程序的布局")
@JSONField(name = "Layout")
private String layout;
@Schema(name = "Theme", description = "应用程序的主题")
@JSONField(name = "Theme")
private String theme;
@Schema(name = "DarkMode", description = "是否启用深色模式")
@JSONField(name = "DarkMode")
private boolean darkMode;
@Schema(name = "OverallStyle", description = "应用程序的整体样式")
@JSONField(name = "OverallStyle")
private String overallStyle;
@Schema(name = "Grey", description = "是否启用灰色模式")
@JSONField(name = "Grey")
private boolean grey;
@Schema(name = "Weak", description = "色弱模式")
@JSONField(name = "Weak")
private boolean weak;
@Schema(name = "HideTabs", description = "是否隐藏选项卡")
@JSONField(name = "HideTabs")
private boolean hideTabs;
@Schema(name = "HideFooter", description = "是否隐藏页脚")
@JSONField(name = "HideFooter")
private boolean hideFooter;
@Schema(name = "Stretch", description = "是否拉伸显示")
@JSONField(name = "Stretch")
private boolean stretch;
@Schema(name = "SidebarStatus", description = "侧边栏的状态")
@JSONField(name = "SidebarStatus")
private boolean sidebarStatus;
@Schema(name = "EpThemeColor", description = "主题颜色")
@JSONField(name = "EpThemeColor")
private String epThemeColor;
@Schema(name = "ShowLogo", description = "是否显示logo")
@JSONField(name = "ShowLogo")
private boolean showLogo;
@Schema(name = "ShowModel", description = "要显示的模型")
@JSONField(name = "ShowModel")
private String showModel;
@Schema(name = "MenuArrowIconNoTransition", description = "菜单箭头图标是否没有过渡效果")
@JSONField(name = "MenuArrowIconNoTransition")
private boolean menuArrowIconNoTransition;
@Schema(name = "CachingAsyncRoutes", description = "是否缓存异步路由")
@JSONField(name = "CachingAsyncRoutes")
private boolean cachingAsyncRoutes;
@Schema(name = "TooltipEffect", description = "工具提示的效果")
@JSONField(name = "TooltipEffect")
private String tooltipEffect;
@Schema(name = "ResponsiveStorageNameSpace", description = "响应式存储的命名空间")
@JSONField(name = "ResponsiveStorageNameSpace")
private String responsiveStorageNameSpace;
@Schema(name = "MenuSearchHistory", description = "菜单搜索历史")
@JSONField(name = "MenuSearchHistory")
private int menuSearchHistory;
}

View File

@ -2,14 +2,12 @@ package cn.bunny.services.controller;
import cn.bunny.dao.dto.system.configuration.WebConfigurationDto; import cn.bunny.dao.dto.system.configuration.WebConfigurationDto;
import cn.bunny.dao.entity.configuration.WebConfiguration; import cn.bunny.dao.entity.configuration.WebConfiguration;
import cn.bunny.dao.vo.configuration.WebConfigurationVo;
import cn.bunny.dao.vo.result.Result; import cn.bunny.dao.vo.result.Result;
import cn.bunny.dao.vo.result.ResultCodeEnum; import cn.bunny.dao.vo.result.ResultCodeEnum;
import cn.bunny.services.service.ConfigurationService; import cn.bunny.services.service.ConfigurationService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -22,19 +20,17 @@ public class ConfigurationController {
@Autowired @Autowired
private ConfigurationService configurationService; private ConfigurationService configurationService;
@SneakyThrows
@Operation(summary = "读取web配置文件", description = "读取web配置文件") @Operation(summary = "读取web配置文件", description = "读取web配置文件")
@GetMapping("noAuth/webConfig") @GetMapping("noAuth/webConfig")
public WebConfiguration webConfig() { public WebConfiguration webConfig() {
return configurationService.webConfig(); return configurationService.webConfig();
} }
@SneakyThrows
@Operation(summary = "获取修改web配置文件", description = "获取修改web配置文件") @Operation(summary = "获取修改web配置文件", description = "获取修改web配置文件")
@GetMapping("getWebConfig") @GetMapping("getWebConfig")
public Mono<Result<WebConfigurationVo>> getWebConfig() { public Result<WebConfiguration> getWebConfig() {
WebConfigurationVo webConfiguration = configurationService.getWebConfig(); WebConfiguration webConfiguration = configurationService.webConfig();
return Mono.just(Result.success(webConfiguration)); return Result.success(webConfiguration);
} }
@Operation(summary = "更新web配置文件", description = "更新web配置文件") @Operation(summary = "更新web配置文件", description = "更新web配置文件")

View File

@ -2,7 +2,6 @@ package cn.bunny.services.service;
import cn.bunny.dao.dto.system.configuration.WebConfigurationDto; import cn.bunny.dao.dto.system.configuration.WebConfigurationDto;
import cn.bunny.dao.entity.configuration.WebConfiguration; import cn.bunny.dao.entity.configuration.WebConfiguration;
import cn.bunny.dao.vo.configuration.WebConfigurationVo;
public interface ConfigurationService { public interface ConfigurationService {
@ -20,10 +19,4 @@ public interface ConfigurationService {
*/ */
WebConfiguration webConfig(); WebConfiguration webConfig();
/**
* 获取修改web配置文件
*
* @return 要修改前端配置文件
*/
WebConfigurationVo getWebConfig();
} }

View File

@ -1,41 +1,27 @@
package cn.bunny.services.service.impl; package cn.bunny.services.service.impl;
import cn.bunny.common.service.exception.AuthCustomerException; import cn.bunny.common.service.exception.AuthCustomerException;
import cn.bunny.dao.constant.RedisUserConstant;
import cn.bunny.dao.dto.system.configuration.WebConfigurationDto; import cn.bunny.dao.dto.system.configuration.WebConfigurationDto;
import cn.bunny.dao.entity.configuration.WebConfiguration; import cn.bunny.dao.entity.configuration.WebConfiguration;
import cn.bunny.dao.vo.configuration.WebConfigurationVo;
import cn.bunny.dao.vo.result.ResultCodeEnum; import cn.bunny.dao.vo.result.ResultCodeEnum;
import cn.bunny.services.service.ConfigurationService; import cn.bunny.services.service.ConfigurationService;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
@Service @Service
@Transactional
public class ConfigurationServiceImpl implements ConfigurationService { public class ConfigurationServiceImpl implements ConfigurationService {
@Value("${bunny.bashPath}") @Autowired
private String bashPath; private RedisTemplate<String, Object> redisTemplate;
private static @NotNull String getWebConfigString(InputStream inputStream, Path templatePath) throws IOException {
if (inputStream == null) throw new AuthCustomerException(ResultCodeEnum.MISSING_TEMPLATE_FILES);
// 判断web模板文件是否存在不存在进行复制
boolean exists = Files.exists(templatePath);
if (!exists) Files.copy(inputStream, templatePath);
// 将读取文件返回
byte[] bytes = Files.readAllBytes(templatePath);
return new String(bytes);
}
/** /**
* * 更新web配置 * * 更新web配置
@ -43,26 +29,14 @@ public class ConfigurationServiceImpl implements ConfigurationService {
* @param dto 前端配置选项 * @param dto 前端配置选项
*/ */
@Override @Override
@CacheEvict(cacheNames = "webConfig", key = "'platformConfig'", beforeInvocation = true, cacheManager = "cacheManagerWithMouth")
public void updateWebConfiguration(WebConfigurationDto dto) { public void updateWebConfiguration(WebConfigurationDto dto) {
try { redisTemplate.delete(RedisUserConstant.WEB_CONFIG_KEY);
// 系统模板文件位置
Path templatePath = Path.of(bashPath + "/platform-config.json");
// 提交表单转换 // 提交表单转换
WebConfiguration webConfiguration = new WebConfiguration(); WebConfiguration webConfiguration = new WebConfiguration();
BeanUtils.copyProperties(dto, webConfiguration); BeanUtils.copyProperties(dto, webConfiguration);
// 将表单转成存储的类型 redisTemplate.opsForValue().set(RedisUserConstant.WEB_CONFIG_KEY, webConfiguration);
WebConfigurationVo webConfigurationVo = new WebConfigurationVo();
BeanUtils.copyProperties(webConfiguration, webConfigurationVo);
// 将文件写入
String jsonString = JSON.toJSONString(webConfigurationVo);
Files.writeString(templatePath, jsonString);
} catch (IOException e) {
throw new RuntimeException(e);
}
} }
/** /**
@ -71,32 +45,24 @@ public class ConfigurationServiceImpl implements ConfigurationService {
* @return 前端配置文件 * @return 前端配置文件
*/ */
@Override @Override
@Cacheable(cacheNames = "webConfig", key = "'platformConfig'", cacheManager = "cacheManagerWithMouth")
public WebConfiguration webConfig() { public WebConfiguration webConfig() {
// 系统模板文件位置 // 判断Redis中是否存在
Path templatePath = Path.of(bashPath + "/platform-config.json"); Object object = redisTemplate.opsForValue().get(RedisUserConstant.WEB_CONFIG_KEY);
if (object != null) {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/platform-config.json")) { String jsonString = JSON.toJSONString(object);
String string = getWebConfigString(inputStream, templatePath); return JSON.parseObject(jsonString, WebConfiguration.class);
return JSON.parseObject(string, WebConfiguration.class);
} catch (IOException exception) {
throw new AuthCustomerException(exception.getMessage());
} }
}
/**
* 获取修改web配置文件
*
* @return 要修改前端配置文件
*/
@Override
public WebConfigurationVo getWebConfig() {
// 系统模板文件位置
Path templatePath = Path.of(bashPath + "/platform-config.json");
// 不存在从文件中获取
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/platform-config.json")) { try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/platform-config.json")) {
String string = getWebConfigString(inputStream, templatePath); if (inputStream == null) throw new AuthCustomerException(ResultCodeEnum.MISSING_TEMPLATE_FILES);
return JSON.parseObject(string, WebConfigurationVo.class);
// 读取文件返回并存入Redis
byte[] bytes = inputStream.readAllBytes();
WebConfiguration webConfiguration = JSON.parseObject(new String(bytes), WebConfiguration.class);
redisTemplate.opsForValue().set(RedisUserConstant.WEB_CONFIG_KEY, webConfiguration);
return webConfiguration;
} catch (IOException exception) { } catch (IOException exception) {
throw new AuthCustomerException(exception.getMessage()); throw new AuthCustomerException(exception.getMessage());
} }

View File

@ -39,4 +39,3 @@ bunny:
bucket-name: auth-admin bucket-name: auth-admin
backPath: "D:\\MyData\\backup" backPath: "D:\\MyData\\backup"
bashPath: "D:\\MyData\\auth"