作者 RuoYi

添加获取当前的环境配置方法

@@ -5,7 +5,10 @@ import org.springframework.beans.BeansException; @@ -5,7 +5,10 @@ import org.springframework.beans.BeansException;
5 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 5 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
6 import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 6 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
7 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 7 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  8 +import org.springframework.context.ApplicationContext;
  9 +import org.springframework.context.ApplicationContextAware;
8 import org.springframework.stereotype.Component; 10 import org.springframework.stereotype.Component;
  11 +import com.ruoyi.common.utils.StringUtils;
9 12
10 /** 13 /**
11 * spring工具类 方便在非spring管理环境中获取bean 14 * spring工具类 方便在非spring管理环境中获取bean
@@ -13,17 +16,25 @@ import org.springframework.stereotype.Component; @@ -13,17 +16,25 @@ import org.springframework.stereotype.Component;
13 * @author ruoyi 16 * @author ruoyi
14 */ 17 */
15 @Component 18 @Component
16 -public final class SpringUtils implements BeanFactoryPostProcessor 19 +public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
17 { 20 {
18 /** Spring应用上下文环境 */ 21 /** Spring应用上下文环境 */
19 private static ConfigurableListableBeanFactory beanFactory; 22 private static ConfigurableListableBeanFactory beanFactory;
20 23
  24 + private static ApplicationContext applicationContext;
  25 +
21 @Override 26 @Override
22 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException 27 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
23 { 28 {
24 SpringUtils.beanFactory = beanFactory; 29 SpringUtils.beanFactory = beanFactory;
25 } 30 }
26 31
  32 + @Override
  33 + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
  34 + {
  35 + SpringUtils.applicationContext = applicationContext;
  36 + }
  37 +
27 /** 38 /**
28 * 获取对象 39 * 获取对象
29 * 40 *
@@ -111,4 +122,25 @@ public final class SpringUtils implements BeanFactoryPostProcessor @@ -111,4 +122,25 @@ public final class SpringUtils implements BeanFactoryPostProcessor
111 { 122 {
112 return (T) AopContext.currentProxy(); 123 return (T) AopContext.currentProxy();
113 } 124 }
  125 +
  126 + /**
  127 + * 获取当前的环境配置,无配置返回null
  128 + *
  129 + * @return 当前的环境配置
  130 + */
  131 + public static String[] getActiveProfiles()
  132 + {
  133 + return applicationContext.getEnvironment().getActiveProfiles();
  134 + }
  135 +
  136 + /**
  137 + * 获取当前的环境配置,当有多个环境配置时,只获取第一个
  138 + *
  139 + * @return 当前的环境配置
  140 + */
  141 + public static String getActiveProfile()
  142 + {
  143 + final String[] activeProfiles = getActiveProfiles();
  144 + return StringUtils.isNotEmpty(activeProfiles) ? activeProfiles[0] : null;
  145 + }
114 } 146 }
@@ -63,20 +63,30 @@ export function addDateRange (params = {}, dateRange) { @@ -63,20 +63,30 @@ export function addDateRange (params = {}, dateRange) {
63 } 63 }
64 64
65 // 回显数据字典 65 // 回显数据字典
66 -export function selectDictLabel(datas = [], value = '') {  
67 - if (!value) return '-';  
68 - const dataArr = datas.filter(item => item.dictValue === value.toString());  
69 - return dataArr.length ? dataArr[0].dictLabel : 'Error Dict'; 66 +export function selectDictLabel(datas, value) {
  67 + var actions = [];
  68 + Object.keys(datas).some((key) => {
  69 + if (datas[key].dictValue == ('' + value)) {
  70 + actions.push(datas[key].dictLabel);
  71 + return true;
  72 + }
  73 + })
  74 + return actions.join('');
70 } 75 }
71 76
72 // 回显数据字典(字符串数组) 77 // 回显数据字典(字符串数组)
73 -export function selectDictLabels(datas = [], value = '', separator = ',') {  
74 - const actions = [];  
75 - const temp = value.split(separator).filter(item => item);  
76 - temp.forEach((_, index) => {  
77 - actions.push(selectDictLabel(datas, temp[index])); 78 +export function selectDictLabels(datas, value, separator) {
  79 + var actions = [];
  80 + var currentSeparator = undefined === separator ? "," : separator;
  81 + var temp = value.split(currentSeparator);
  82 + Object.keys(value.split(currentSeparator)).some((val) => {
  83 + Object.keys(datas).some((key) => {
  84 + if (datas[key].dictValue == ('' + temp[val])) {
  85 + actions.push(datas[key].dictLabel + currentSeparator);
  86 + }
78 }) 87 })
79 - return actions.join(separator); 88 + })
  89 + return actions.join('').substring(0, actions.join('').length - 1);
80 } 90 }
81 91
82 // 通用下载方法 92 // 通用下载方法
@@ -114,19 +124,22 @@ export function praseStrEmpty(str) { @@ -114,19 +124,22 @@ export function praseStrEmpty(str) {
114 * @param {*} children 孩子节点字段 默认 'children' 124 * @param {*} children 孩子节点字段 默认 'children'
115 * @param {*} rootId 根Id 默认 0 125 * @param {*} rootId 根Id 默认 0
116 */ 126 */
117 -export function handleTree(data = [], id = 'id', parentId = 'parentId', children = 'children', rootId = 0) { 127 +export function handleTree(data, id, parentId, children, rootId) {
  128 + id = id || 'id'
  129 + parentId = parentId || 'parentId'
  130 + children = children || 'children'
  131 + rootId = rootId || 0
118 //对源数据深度克隆 132 //对源数据深度克隆
119 const cloneData = JSON.parse(JSON.stringify(data)) 133 const cloneData = JSON.parse(JSON.stringify(data))
120 //循环所有项 134 //循环所有项
121 const treeData = cloneData.filter(father => { 135 const treeData = cloneData.filter(father => {
122 - const branchArr = cloneData.filter(child => { 136 + let branchArr = cloneData.filter(child => {
123 //返回每一项的子级数组 137 //返回每一项的子级数组
124 return father[id] === child[parentId] 138 return father[id] === child[parentId]
125 }); 139 });
126 - branchArr.length && (father.children = branchArr); 140 + branchArr.length > 0 ? father.children = branchArr : '';
127 //返回第一层 141 //返回第一层
128 return father[parentId] === rootId; 142 return father[parentId] === rootId;
129 }); 143 });
130 - return treeData !== '' ? treeData : data; 144 + return treeData != '' ? treeData : data;
131 } 145 }
132 -