♻️ Refactoring code. #1189 服务端设置 直接读取参数配置表,增加统一统一工具
This commit is contained in:
1 parent
a765abcbc9
commit
98e2dea7c6
7 files changed
+112
-30
No files matched your search
+1
-1
@@ -15,7 +15,7 @@
|
||||
* Author: lengleng (wangiegie@gmail.com)
|
||||
*/
|
||||
|
||||
package com.pig4cloud.pigx.common.data.enums;
|
||||
package com.pig4cloud.pigx.common.data.datascope;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
-1
@@ -22,7 +22,6 @@ import com.pig4cloud.pigx.admin.api.entity.SysDeptRelation;
|
||||
import com.pig4cloud.pigx.admin.api.entity.SysRole;
|
||||
import com.pig4cloud.pigx.admin.api.feign.RemoteDataScopeService;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.data.enums.DataScopeTypeEnum;
|
||||
import com.pig4cloud.pigx.common.security.service.PigxUser;
|
||||
import com.pig4cloud.pigx.common.security.util.SecurityUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.pig4cloud.pigx.common.data.resolver;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.pig4cloud.pigx.admin.api.feign.RemoteParamService;
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.SpringContextHolder;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/5/12
|
||||
* <p>
|
||||
* 系统参数配置解析器
|
||||
*/
|
||||
@UtilityClass
|
||||
public class ParamResolver {
|
||||
|
||||
/**
|
||||
* 根据key 查询value 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultVal 默认值
|
||||
* @return value
|
||||
*/
|
||||
public Integer getInt(String key, Integer... defaultVal) {
|
||||
return checkAndGet(key, defaultVal);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 根据key 查询value 配置
|
||||
*
|
||||
* @param key key
|
||||
* @param defaultVal 默认值
|
||||
* @return value
|
||||
*/
|
||||
public String getStr(String key, String... defaultVal) {
|
||||
return checkAndGet(key, defaultVal);
|
||||
}
|
||||
|
||||
|
||||
private <T> T checkAndGet(String key, Object... defaultVal) {
|
||||
// 校验入参是否合法
|
||||
if (StrUtil.isBlank(key) || defaultVal.length > 1) {
|
||||
throw new IllegalArgumentException("参数不合法");
|
||||
}
|
||||
|
||||
RemoteParamService remoteParamService = SpringContextHolder.getBean(RemoteParamService.class);
|
||||
|
||||
String result = remoteParamService.getByKey(key, SecurityConstants.FROM_IN).getData();
|
||||
|
||||
if (StrUtil.isNotBlank(result)) {
|
||||
return (T) result;
|
||||
}
|
||||
|
||||
if (defaultVal != null) {
|
||||
return (T) defaultVal.clone()[0];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.pig4cloud.pigx.admin.api.feign;
|
||||
|
||||
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
|
||||
import com.pig4cloud.pigx.common.core.constant.ServiceNameConstants;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
* @date 2020/5/12
|
||||
* <p>
|
||||
* 查询参数相关
|
||||
*/
|
||||
@FeignClient(contextId = "remoteParamService", value = ServiceNameConstants.UPMS_SERVICE)
|
||||
public interface RemoteParamService {
|
||||
|
||||
/**
|
||||
* 通过key 查询参数配置
|
||||
*
|
||||
* @param key key
|
||||
* @param from 声明成内部调用,避免MQ 等无法调用
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/param/publicValue/{key}")
|
||||
R<String> getByKey(@PathVariable("key") String key, @RequestHeader(SecurityConstants.FROM) String from);
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
com.pig4cloud.pigx.common.feign.PigxFeignAutoConfiguration=\
|
||||
com.pig4cloud.pigx.admin.api.feign.RemoteDataScopeService,\
|
||||
com.pig4cloud.pigx.admin.api.feign.RemoteLogService,\
|
||||
com.pig4cloud.pigx.admin.api.feign.RemoteParamService,\
|
||||
com.pig4cloud.pigx.admin.api.feign.RemoteTenantService,\
|
||||
com.pig4cloud.pigx.admin.api.feign.RemoteTokenService,\
|
||||
com.pig4cloud.pigx.admin.api.feign.RemoteUserService
|
||||
+20
-22
@@ -26,12 +26,9 @@ import com.pig4cloud.pigx.admin.mapper.SysTenantMapper;
|
||||
import com.pig4cloud.pigx.admin.service.*;
|
||||
import com.pig4cloud.pigx.common.core.constant.CacheConstants;
|
||||
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
|
||||
import com.pig4cloud.pigx.common.core.exception.CheckedException;
|
||||
import com.pig4cloud.pigx.common.data.resolver.ParamResolver;
|
||||
import com.pig4cloud.pigx.common.data.tenant.TenantContextHolder;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.apache.commons.configuration.Configuration;
|
||||
import org.apache.commons.configuration.ConfigurationException;
|
||||
import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
@@ -59,6 +56,7 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
||||
private final SysUserRoleService userRoleService;
|
||||
private final SysRoleMenuService roleMenuService;
|
||||
private final SysDictItemService dictItemService;
|
||||
private final SysPublicParamService paramService;
|
||||
private final SysUserService userService;
|
||||
private final SysRoleService roleService;
|
||||
private final SysMenuService menuService;
|
||||
@@ -94,6 +92,7 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
||||
* - sys_dict
|
||||
* - sys_dict_item
|
||||
* - sys_client_details
|
||||
* - sys_public_params
|
||||
*
|
||||
* @param sysTenant 租户实体
|
||||
* @return
|
||||
@@ -103,8 +102,16 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
||||
@CacheEvict(value = CacheConstants.TENANT_DETAILS)
|
||||
public Boolean saveTenant(SysTenant sysTenant) {
|
||||
this.save(sysTenant);
|
||||
// 查询系统默认租户配置参数
|
||||
Integer defaultId = ParamResolver.getInt("TENANT_DEFAULT_ID", 1);
|
||||
String defaultDeptName = ParamResolver.getStr("TENANT_DEFAULT_DEPTNAME", "租户默认部门");
|
||||
String defaultUsername = ParamResolver.getStr("TENANT_DEFAULT_USERNAME", "admin");
|
||||
String defaultPassword = ParamResolver.getStr("TENANT_DEFAULT_PASSWORD", "123456");
|
||||
String defaultRoleCode = ParamResolver.getStr("TENANT_DEFAULT_ROLECODE", "ROLE_ADMIN");
|
||||
String defaultRoleName = ParamResolver.getStr("TENANT_DEFAULT_ROLENAME", "租户默认角色");
|
||||
|
||||
// 查询系统内置字典
|
||||
TenantContextHolder.setTenantId(defaultId);
|
||||
List<SysDict> dictList = new ArrayList<>(dictService.list());
|
||||
// 查询系统内置字典项目
|
||||
List<Integer> dictIdList = dictList.stream().map(SysDict::getId)
|
||||
@@ -116,27 +123,28 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
||||
List<SysMenu> menuList = menuService.list();
|
||||
// 查询客户端配置
|
||||
List<SysOauthClientDetails> clientDetailsList = clientServices.list();
|
||||
// 查询系统参数配置
|
||||
List<SysPublicParam> publicParamList = paramService.list();
|
||||
// 保证插入租户为新的租户
|
||||
TenantContextHolder.setTenantId(sysTenant.getId());
|
||||
Configuration config = getConfig();
|
||||
|
||||
// 插入部门
|
||||
SysDept dept = new SysDept();
|
||||
dept.setName(config.getString("defaultDeptName"));
|
||||
dept.setName(defaultDeptName);
|
||||
dept.setParentId(0);
|
||||
deptService.save(dept);
|
||||
//维护部门关系
|
||||
deptRelationService.insertDeptRelation(dept);
|
||||
// 构造初始化用户
|
||||
SysUser user = new SysUser();
|
||||
user.setUsername(config.getString("defaultUsername"));
|
||||
user.setPassword(ENCODER.encode(config.getString("defaultPassword")));
|
||||
user.setUsername(defaultUsername);
|
||||
user.setPassword(ENCODER.encode(defaultPassword));
|
||||
user.setDeptId(dept.getDeptId());
|
||||
userService.save(user);
|
||||
// 构造新角色
|
||||
SysRole role = new SysRole();
|
||||
role.setRoleCode(config.getString("defaultRoleCode"));
|
||||
role.setRoleName(config.getString("defaultRoleName"));
|
||||
role.setRoleCode(defaultRoleCode);
|
||||
role.setRoleName(defaultRoleName);
|
||||
roleService.save(role);
|
||||
// 用户角色关系
|
||||
SysUserRole userRole = new SysUserRole();
|
||||
@@ -166,6 +174,8 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
||||
|
||||
//插入客户端
|
||||
clientServices.saveBatch(clientDetailsList);
|
||||
// 插入系统配置
|
||||
paramService.saveBatch(publicParamList);
|
||||
return dictItemService.saveBatch(itemList);
|
||||
}
|
||||
|
||||
@@ -188,16 +198,4 @@ public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenant
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
*/
|
||||
private Configuration getConfig() {
|
||||
try {
|
||||
return new PropertiesConfiguration("tenant/tenant.properties");
|
||||
} catch (ConfigurationException e) {
|
||||
throw new CheckedException("获取配置文件失败,", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
# \u79DF\u6237\u521D\u59CB\u5316\u9ED8\u8BA4\u914D\u7F6E
|
||||
defaultUsername=admin
|
||||
defaultPassword=123456
|
||||
defaultRoleCode=ROLE_ADMIN
|
||||
defaultRoleName=\u79DF\u6237\u9ED8\u8BA4\u89D2\u8272
|
||||
defaultDeptName=\u79DF\u6237\u9ED8\u8BA4\u90E8\u95E8
|
||||
Reference in new issue
Block a user