【7.1.6】【theme】更新获取主题的接口

pull/25/head
fengshuonan 2022-01-11 10:52:07 +08:00
parent 76f38094b3
commit 1a55b4db8b
10 changed files with 407 additions and 0 deletions

View File

@ -67,4 +67,9 @@ public interface SystemCachesConstants {
*/
String ROLE_DATA_SCOPE_CACHE_PREFIX = "role_data_scope:";
/**
*
*/
String SYSTEM_THEME_CACHE_PREFIX = "system_cache:";
}

View File

@ -61,5 +61,26 @@
<artifactId>file-business</artifactId>
<version>${roses.version}</version>
</dependency>
<!--缓存api-->
<!--默认主题查询到会缓存-->
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>cache-api</artifactId>
<version>${roses.version}</version>
</dependency>
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>cache-sdk-memory</artifactId>
<version>${roses.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.stylefeng.roses</groupId>
<artifactId>cache-sdk-redis</artifactId>
<version>${roses.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,49 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.system.modular.theme.cache;
import cn.hutool.cache.impl.TimedCache;
import cn.stylefeng.roses.kernel.cache.memory.AbstractMemoryCacheOperator;
import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants;
import cn.stylefeng.roses.kernel.system.modular.theme.pojo.DefaultTheme;
/**
*
*
* @author fengshuonan
* @date 2022/1/11 9:37
*/
public class ThemeMemoryCache extends AbstractMemoryCacheOperator<DefaultTheme> {
public ThemeMemoryCache(TimedCache<String, DefaultTheme> timedCache) {
super(timedCache);
}
@Override
public String getCommonKeyPrefix() {
return SystemCachesConstants.SYSTEM_THEME_CACHE_PREFIX;
}
}

View File

@ -0,0 +1,49 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.system.modular.theme.cache;
import cn.stylefeng.roses.kernel.cache.redis.AbstractRedisCacheOperator;
import cn.stylefeng.roses.kernel.system.api.constants.SystemCachesConstants;
import cn.stylefeng.roses.kernel.system.modular.theme.pojo.DefaultTheme;
import org.springframework.data.redis.core.RedisTemplate;
/**
*
*
* @author fengshuonan
* @date 2022/1/11 9:37
*/
public class ThemeRedisCache extends AbstractRedisCacheOperator<DefaultTheme> {
public ThemeRedisCache(RedisTemplate<String, DefaultTheme> redisTemplate) {
super(redisTemplate);
}
@Override
public String getCommonKeyPrefix() {
return SystemCachesConstants.SYSTEM_THEME_CACHE_PREFIX;
}
}

View File

@ -0,0 +1,40 @@
package cn.stylefeng.roses.kernel.system.modular.theme.controller;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.system.api.pojo.theme.SysThemeRequest;
import cn.stylefeng.roses.kernel.system.modular.theme.pojo.DefaultTheme;
import cn.stylefeng.roses.kernel.system.modular.theme.service.SysThemeService;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* API
*
* @author fengshuonan
* @date 2022/1/10 18:27
*/
@RestController
@ApiResource(name = "主题开放接口的API")
public class SysThemeApiController {
@Resource
private SysThemeService sysThemeService;
/**
* Guns
*
* @author fengshuonan
* @date 2022/1/10 18:29
*/
@PostResource(name = "获取当前Guns管理系统的主题数据", path = "/theme/currentThemeInfo", requiredPermission = false, requiredLogin = false)
public ResponseData currentThemeInfo(SysThemeRequest sysThemeParam) {
DefaultTheme defaultTheme = sysThemeService.currentThemeInfo(sysThemeParam);
return new SuccessResponseData(defaultTheme);
}
}

View File

@ -0,0 +1,87 @@
package cn.stylefeng.roses.kernel.system.modular.theme.factory;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.system.modular.theme.pojo.DefaultTheme;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
/**
* 使
*
* @author fengshuonan
* @date 2022/1/11 9:30
*/
@Data
public class DefaultThemeFactory {
/**
*
*
* @author fengshuonan
* @date 2022/1/11 9:31
*/
public static DefaultTheme getSystemDefaultTheme() {
DefaultTheme defaultTheme = new DefaultTheme();
defaultTheme.setGunsMgrBeiUrl("https://beian.miit.gov.cn/");
defaultTheme.setGunsMgrBeiNo("京ICP备001-1");
defaultTheme.setGunsMgrFavicon("1479753047148322818");
defaultTheme.setGunsMgrFooterText("stylefeng开源技术 javaguns.com");
defaultTheme.setGunsMgrLogo("1479753047148322818");
defaultTheme.setGunsMgrName("Guns Tech.");
defaultTheme.setGunsMgrLoginBackgroundImg("1479751422149074948");
return defaultTheme;
}
/**
* jsonObject
*
* @author fengshuonan
* @date 2022/1/11 9:31
*/
public static DefaultTheme parseDefaultTheme(JSONObject jsonObject) {
// 初始化主题参数
DefaultTheme defaultTheme = new DefaultTheme();
// 存放没有被set值的key列表
ArrayList<String> noneSetKeys = new ArrayList<>();
// 遍历Key值为对应的key属性赋值
for (String jsonObjectKey : jsonObject.keySet()) {
String propertyName = StrUtil.toCamelCase(jsonObjectKey);
try {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, DefaultTheme.class);
Method writeMethod = propertyDescriptor.getWriteMethod();
writeMethod.invoke(defaultTheme, jsonObject.getString(jsonObjectKey));
} catch (IntrospectionException | InvocationTargetException | IllegalAccessException e) {
noneSetKeys.add(jsonObjectKey);
}
}
// 遍历没有被set值的key放到单独的map中
HashMap<String, String> otherInfos = new HashMap<>();
for (String noneSetKey : noneSetKeys) {
String propertyName = StrUtil.toCamelCase(noneSetKey);
String value = jsonObject.getString(noneSetKey);
otherInfos.put(propertyName, value);
}
defaultTheme.setOtherConfigs(otherInfos);
return defaultTheme;
}
public static void main(String[] args) throws Exception {
DefaultTheme defaultTheme = new DefaultTheme();
PropertyDescriptor propertyDescriptor = new PropertyDescriptor("abc", DefaultTheme.class);
Method writeMethod = propertyDescriptor.getWriteMethod();
writeMethod.invoke(defaultTheme, "123123");
}
}

View File

@ -0,0 +1,56 @@
package cn.stylefeng.roses.kernel.system.modular.theme.pojo;
import lombok.Data;
import java.util.Map;
/**
* Guns
*
* @author fengshuonan
* @date 2022/1/10 18:30
*/
@Data
public class DefaultTheme {
/**
*
*/
private String gunsMgrName;
/**
*
*/
private String gunsMgrLoginBackgroundImg;
/**
* LOGO
*/
private String gunsMgrLogo;
/**
* Icon
*/
private String gunsMgrFavicon;
/**
*
*/
private String gunsMgrFooterText;
/**
*
*/
private String gunsMgrBeiNo;
/**
*
*/
private String gunsMgrBeiUrl;
/**
*
*/
private Map<String, String> otherConfigs;
}

View File

@ -4,6 +4,7 @@ import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.system.api.pojo.theme.SysThemeDTO;
import cn.stylefeng.roses.kernel.system.api.pojo.theme.SysThemeRequest;
import cn.stylefeng.roses.kernel.system.modular.theme.entity.SysTheme;
import cn.stylefeng.roses.kernel.system.modular.theme.pojo.DefaultTheme;
import com.baomidou.mybatisplus.extension.service.IService;
/**
@ -63,4 +64,13 @@ public interface SysThemeService extends IService<SysTheme> {
* @date 2021/12/17 17:06
*/
void updateThemeStatus(SysThemeRequest sysThemeRequest);
/**
*
*
* @author fengshuonan
* @date 2022/1/10 18:30
*/
DefaultTheme currentThemeInfo(SysThemeRequest sysThemeParam);
}

View File

@ -3,8 +3,10 @@ package cn.stylefeng.roses.kernel.system.modular.theme.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.file.api.FileInfoApi;
import cn.stylefeng.roses.kernel.file.api.pojo.request.SysFileInfoRequest;
@ -12,6 +14,7 @@ import cn.stylefeng.roses.kernel.file.api.pojo.response.SysFileInfoResponse;
import cn.stylefeng.roses.kernel.file.modular.service.SysFileInfoService;
import cn.stylefeng.roses.kernel.rule.enums.YesOrNotEnum;
import cn.stylefeng.roses.kernel.system.api.ThemeServiceApi;
import cn.stylefeng.roses.kernel.system.api.constants.SystemConstants;
import cn.stylefeng.roses.kernel.system.api.exception.SystemModularException;
import cn.stylefeng.roses.kernel.system.api.exception.enums.theme.SysThemeExceptionEnum;
import cn.stylefeng.roses.kernel.system.api.pojo.theme.SysThemeDTO;
@ -20,8 +23,10 @@ import cn.stylefeng.roses.kernel.system.modular.theme.entity.SysTheme;
import cn.stylefeng.roses.kernel.system.modular.theme.entity.SysThemeTemplate;
import cn.stylefeng.roses.kernel.system.modular.theme.entity.SysThemeTemplateField;
import cn.stylefeng.roses.kernel.system.modular.theme.enums.FieldTypeEnum;
import cn.stylefeng.roses.kernel.system.modular.theme.factory.DefaultThemeFactory;
import cn.stylefeng.roses.kernel.system.modular.theme.mapper.SysThemeMapper;
import cn.stylefeng.roses.kernel.system.modular.theme.pojo.AntdvFileInfo;
import cn.stylefeng.roses.kernel.system.modular.theme.pojo.DefaultTheme;
import cn.stylefeng.roses.kernel.system.modular.theme.service.SysThemeService;
import cn.stylefeng.roses.kernel.system.modular.theme.service.SysThemeTemplateFieldService;
import cn.stylefeng.roses.kernel.system.modular.theme.service.SysThemeTemplateService;
@ -31,6 +36,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -47,6 +53,7 @@ import java.util.stream.Collectors;
* @date 2021/12/17 16:17
*/
@Service
@Slf4j
public class SysThemeServiceImpl extends ServiceImpl<SysThemeMapper, SysTheme> implements SysThemeService, ThemeServiceApi {
@Resource
@ -61,10 +68,14 @@ public class SysThemeServiceImpl extends ServiceImpl<SysThemeMapper, SysTheme> i
@Resource
private FileInfoApi fileInfoApi;
@Resource(name = "themeCacheApi")
private CacheOperatorApi<DefaultTheme> themeCacheApi;
@Override
public void add(SysThemeRequest sysThemeRequest) {
// 查询模板状态
SysThemeTemplate sysThemeTemplate = sysThemeTemplateService.getById(sysThemeRequest.getTemplateId());
// 判断模板启用状态:如果为禁用状态不允许使用
if (YesOrNotEnum.N.getCode().equals(sysThemeTemplate.getStatusFlag().toString())) {
throw new SystemModularException(SysThemeExceptionEnum.THEME_TEMPLATE_IS_DISABLE);
@ -218,6 +229,30 @@ public class SysThemeServiceImpl extends ServiceImpl<SysThemeMapper, SysTheme> i
this.updateById(sysTheme);
}
@Override
public DefaultTheme currentThemeInfo(SysThemeRequest sysThemeParam) {
// 获取缓存中是否有默认主题
DefaultTheme defaultTheme = themeCacheApi.get(SystemConstants.THEME_GUNS_PLATFORM);
if (defaultTheme != null) {
return defaultTheme;
}
// 查询系统中激活的主题
DefaultTheme result = null;
try {
result = this.querySystemTheme();
} catch (Exception e) {
log.error("获取当前系统主题出错", e);
return DefaultThemeFactory.getSystemDefaultTheme();
}
// 缓存系统中激活的主题
themeCacheApi.put(SystemConstants.THEME_GUNS_PLATFORM, result);
return result;
}
/**
*
*
@ -231,4 +266,44 @@ public class SysThemeServiceImpl extends ServiceImpl<SysThemeMapper, SysTheme> i
}
return sysTheme;
}
/**
*
*
* @author fengshuonan
* @date 2022/1/11 9:44
*/
private DefaultTheme querySystemTheme() {
// 查询编码为GUNS_PLATFORM的主题模板id
LambdaQueryWrapper<SysThemeTemplate> sysThemeTemplateLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysThemeTemplateLambdaQueryWrapper.eq(SysThemeTemplate::getTemplateCode, SystemConstants.THEME_GUNS_PLATFORM);
SysThemeTemplate sysThemeTemplate = this.sysThemeTemplateService.getOne(sysThemeTemplateLambdaQueryWrapper, false);
if (sysThemeTemplate == null) {
log.error("当前系统主题模板编码GUNS_PLATFORM不存在请检查数据库数据是否正常");
return DefaultThemeFactory.getSystemDefaultTheme();
}
// 查找改模板激活的主题,如果没有就返回默认主题
LambdaQueryWrapper<SysTheme> sysThemeLambdaQueryWrapper = new LambdaQueryWrapper<>();
sysThemeLambdaQueryWrapper.eq(SysTheme::getTemplateId, sysThemeTemplate.getTemplateId());
sysThemeLambdaQueryWrapper.eq(SysTheme::getStatusFlag, YesOrNotEnum.Y.getCode());
sysThemeLambdaQueryWrapper.orderByDesc(BaseEntity::getCreateTime);
SysTheme sysTheme = this.getOne(sysThemeLambdaQueryWrapper, false);
if (sysTheme == null) {
log.error("当前系统主题模板编码为GUNS_PLATFORM的主题不存在请检查数据库数据是否正常");
return DefaultThemeFactory.getSystemDefaultTheme();
}
// 解析主题中的json字符串
String themeValue = sysTheme.getThemeValue();
if (StrUtil.isBlank(themeValue)) {
JSONObject jsonObject = JSONObject.parseObject(themeValue);
return DefaultThemeFactory.parseDefaultTheme(jsonObject);
} else {
return DefaultThemeFactory.getSystemDefaultTheme();
}
}
}

View File

@ -34,6 +34,8 @@ import cn.stylefeng.roses.kernel.system.modular.role.cache.RoleDataScopeMemoryCa
import cn.stylefeng.roses.kernel.system.modular.role.cache.RoleMemoryCache;
import cn.stylefeng.roses.kernel.system.modular.role.cache.RoleResourceMemoryCache;
import cn.stylefeng.roses.kernel.system.modular.role.entity.SysRole;
import cn.stylefeng.roses.kernel.system.modular.theme.cache.ThemeMemoryCache;
import cn.stylefeng.roses.kernel.system.modular.theme.pojo.DefaultTheme;
import cn.stylefeng.roses.kernel.system.modular.user.cache.SysUserMemoryCache;
import cn.stylefeng.roses.kernel.system.modular.user.cache.UserOrgMemoryCache;
import cn.stylefeng.roses.kernel.system.modular.user.cache.UserRoleMemoryCache;
@ -130,4 +132,17 @@ public class GunsSystemCacheAutoConfiguration {
return new RoleDataScopeMemoryCache(roleCache);
}
/**
*
*
* @author fengshuonan
* @date 2021/7/31 17:59
*/
@Bean
@ConditionalOnMissingBean(name = "themeCacheApi")
public CacheOperatorApi<DefaultTheme> themeCacheApi() {
TimedCache<String, DefaultTheme> themeCache = CacheUtil.newTimedCache(Long.MAX_VALUE);
return new ThemeMemoryCache(themeCache);
}
}