懒羊羊
2023-08-30 1ac2bc1590406d9babec036e154d8d08f34a6aa1
提交 | 用户 | 时间
1ac2bc 1 package cn.stylefeng.guns.base.consts;
2
3 import cn.stylefeng.guns.base.enums.CommonStatus;
4 import cn.stylefeng.guns.base.sms.AliyunSmsProperties;
5 import cn.stylefeng.roses.core.util.ToolUtil;
6 import lombok.extern.slf4j.Slf4j;
7
8 import java.io.File;
9 import java.util.Map;
10 import java.util.concurrent.ConcurrentHashMap;
11
12 import static cn.stylefeng.guns.base.consts.ConfigConstant.SYSTEM_CONSTANT_PREFIX;
13 import static cn.stylefeng.roses.core.util.ToolUtil.getTempPath;
14
15 /**
16  * 系统常量的容器
17  *
18  * @author fengshuonan
19  * @Date 2019-06-20 13:37
20  */
21 @Slf4j
22 public class ConstantsContext {
23
24     private static final String TIPS_END = ",若想忽略此提示,请在开发管理->系统配置->参数配置,设置相关参数!";
25
26     /**
27      * 所有的常量,可以增删改查
28      */
29     private static Map<String, Object> CONSTNTS_HOLDER = new ConcurrentHashMap<>();
30
31     /**
32      * 添加系统常量
33      */
34     public static void putConstant(String key, Object value) {
35         if (ToolUtil.isOneEmpty(key, value)) {
36             return;
37         }
38         CONSTNTS_HOLDER.put(key, value);
39     }
40
41     /**
42      * 删除常量
43      */
44     public static void deleteConstant(String key) {
45         if (ToolUtil.isOneEmpty(key)) {
46             return;
47         }
48
49         //如果是系统常量
50         if (!key.startsWith(SYSTEM_CONSTANT_PREFIX)) {
51             CONSTNTS_HOLDER.remove(key);
52         }
53     }
54
55     /**
56      * 获取系统常量
57      */
58     public static Map<String, Object> getConstntsMap() {
59         return CONSTNTS_HOLDER;
60     }
61
62     /**
63      * 获取是否为演示环境
64      */
65     public static Boolean getDemoEnvFlag() {
66         String gunsKaptchaOpen = (String) CONSTNTS_HOLDER.get("GUNS_DEMO_OPEN");
67         if (CommonStatus.ENABLE.getCode().equalsIgnoreCase(gunsKaptchaOpen)) {
68             return true;
69         } else {
70             return false;
71         }
72     }
73
74     /**
75      * 获取验证码开关
76      */
77     public static Boolean getKaptchaOpen() {
78         String gunsKaptchaOpen = (String) CONSTNTS_HOLDER.get("GUNS_KAPTCHA_OPEN");
79         if (CommonStatus.ENABLE.getCode().equalsIgnoreCase(gunsKaptchaOpen)) {
80             return true;
81         } else {
82             return false;
83         }
84     }
85
86     /**
87      * 获取短信的配置
88      */
89     public static AliyunSmsProperties getAliyunSmsProperties() {
90         String gunsSmsAccesskeyId = (String) CONSTNTS_HOLDER.get("GUNS_SMS_ACCESSKEY_ID");
91         String gunsSmsAccesskeySecret = (String) CONSTNTS_HOLDER.get("GUNS_SMS_ACCESSKEY_SECRET");
92         String gunsSmsSignName = (String) CONSTNTS_HOLDER.get("GUNS_SMS_SIGN_NAME");
93         String gunsSmsLoginTemplateCode = (String) CONSTNTS_HOLDER.get("GUNS_SMS_LOGIN_TEMPLATE_CODE");
94         String gunsSmsInvalidateMinutes = (String) CONSTNTS_HOLDER.get("GUNS_SMS_INVALIDATE_MINUTES");
95
96         AliyunSmsProperties aliyunSmsProperties = new AliyunSmsProperties();
97         aliyunSmsProperties.setAccessKeyId(gunsSmsAccesskeyId);
98         aliyunSmsProperties.setAccessKeySecret(gunsSmsAccesskeySecret);
99         aliyunSmsProperties.setSignName(gunsSmsSignName);
100         aliyunSmsProperties.setLoginTemplateCode(gunsSmsLoginTemplateCode);
101         aliyunSmsProperties.setInvalidateMinutes(Integer.valueOf(gunsSmsInvalidateMinutes));
102         return aliyunSmsProperties;
103     }
104
105     /**
106      * 获取管理系统名称
107      */
108     public static String getSystemName() {
109         String systemName = (String) CONSTNTS_HOLDER.get("GUNS_SYSTEM_NAME");
110         if (ToolUtil.isEmpty(systemName)) {
111             log.error("系统常量存在空值!常量名称:GUNS_SYSTEM_NAME,采用默认名称:Guns快速开发平台" + TIPS_END);
112             return "Guns快速开发平台";
113         } else {
114             return systemName;
115         }
116     }
117
118     /**
119      * 获取管理系统名称
120      */
121     public static String getDefaultPassword() {
122         String defaultPassword = (String) CONSTNTS_HOLDER.get("GUNS_DEFAULT_PASSWORD");
123         if (ToolUtil.isEmpty(defaultPassword)) {
124             log.error("系统常量存在空值!常量名称:GUNS_DEFAULT_PASSWORD,采用默认密码:111111" + TIPS_END);
125             return "111111";
126         } else {
127             return defaultPassword;
128         }
129     }
130
131     /**
132      * 获取管理系统名称
133      */
134     public static String getOAuth2UserPrefix() {
135         String oauth2Prefix = (String) CONSTNTS_HOLDER.get("GUNS_OAUTH2_PREFIX");
136         if (ToolUtil.isEmpty(oauth2Prefix)) {
137             log.error("系统常量存在空值!常量名称:GUNS_OAUTH2_PREFIX,采用默认值:oauth2" + TIPS_END);
138             return "oauth2";
139         } else {
140             return oauth2Prefix;
141         }
142     }
143
144     /**
145      * 获取顶部导航条是否开启
146      */
147     public static Boolean getDefaultAdvert() {
148         String gunsDefaultAdvert = (String) CONSTNTS_HOLDER.get("GUNS_DEFAULT_ADVERT");
149         if (ToolUtil.isEmpty(gunsDefaultAdvert)) {
150             log.error("系统常量存在空值!常量名称:GUNS_DEFAULT_ADVERT,采用默认值:true" + TIPS_END);
151             return true;
152         } else {
153             if (CommonStatus.ENABLE.getCode().equalsIgnoreCase(gunsDefaultAdvert)) {
154                 return true;
155             } else {
156                 return false;
157             }
158         }
159     }
160
161     /**
162      * 获取系统发布的版本号(防止css和js的缓存)
163      */
164     public static String getReleaseVersion() {
165         String systemReleaseVersion = (String) CONSTNTS_HOLDER.get("GUNS_SYSTEM_RELEASE_VERSION");
166         if (ToolUtil.isEmpty(systemReleaseVersion)) {
167             log.error("系统常量存在空值!常量名称:GUNS_SYSTEM_RELEASE_VERSION,采用默认值:guns" + TIPS_END);
168             return ToolUtil.getRandomString(8);
169         } else {
170             return systemReleaseVersion;
171         }
172     }
173
174     /**
175      * 获取文件上传路径(用于头像和富文本编辑器)
176      */
177     public static String getFileUploadPath() {
178         String gunsFileUploadPath = (String) CONSTNTS_HOLDER.get("GUNS_FILE_UPLOAD_PATH");
179         if (ToolUtil.isEmpty(gunsFileUploadPath)) {
180             log.error("系统常量存在空值!常量名称:GUNS_FILE_UPLOAD_PATH,采用默认值:系统tmp目录" + TIPS_END);
181             return getTempPath();
182         } else {
183             //判断有没有结尾符
184             if (!gunsFileUploadPath.endsWith(File.separator)) {
185                 gunsFileUploadPath = gunsFileUploadPath + File.separator;
186             }
187
188             //判断目录存不存在
189             File file = new File(gunsFileUploadPath);
190             if (!file.exists()) {
191                 boolean mkdirs = file.mkdirs();
192                 if (mkdirs) {
193                     return gunsFileUploadPath;
194                 } else {
195                     log.error("系统常量存在空值!常量名称:GUNS_FILE_UPLOAD_PATH,采用默认值:系统tmp目录" + TIPS_END);
196                     return getTempPath();
197                 }
198             } else {
199                 return gunsFileUploadPath;
200             }
201         }
202     }
203
204     /**
205      * 用于存放bpmn文件
206      */
207     public static String getBpmnFileUploadPath() {
208         String bpmnFileUploadPath = (String) CONSTNTS_HOLDER.get("GUNS_BPMN_FILE_UPLOAD_PATH");
209         if (ToolUtil.isEmpty(bpmnFileUploadPath)) {
210             log.error("系统常量存在空值!常量名称:GUNS_BPMN_FILE_UPLOAD_PATH,采用默认值:系统tmp目录" + TIPS_END);
211             return getTempPath();
212         } else {
213             //判断有没有结尾符
214             if (!bpmnFileUploadPath.endsWith(File.separator)) {
215                 bpmnFileUploadPath = bpmnFileUploadPath + File.separator;
216             }
217
218             //判断目录存不存在
219             File file = new File(bpmnFileUploadPath);
220             if (!file.exists()) {
221                 boolean mkdirs = file.mkdirs();
222                 if (mkdirs) {
223                     return bpmnFileUploadPath;
224                 } else {
225                     log.error("系统常量存在空值!常量名称:GUNS_BPMN_FILE_UPLOAD_PATH,采用默认值:系统tmp目录" + TIPS_END);
226                     return getTempPath();
227                 }
228             } else {
229                 return bpmnFileUploadPath;
230             }
231         }
232     }
233
234     /**
235      * 获取系统地密钥
236      */
237     public static String getJwtSecret() {
238         String systemReleaseVersion = (String) CONSTNTS_HOLDER.get("GUNS_JWT_SECRET");
239         if (ToolUtil.isEmpty(systemReleaseVersion)) {
240             String randomSecret = ToolUtil.getRandomString(32);
241             CONSTNTS_HOLDER.put("GUNS_JWT_SECRET", randomSecret);
242             log.error("jwt密钥存在空值!常量名称:GUNS_JWT_SECRET,采用默认值:随机字符串->" + randomSecret + TIPS_END);
243             return randomSecret;
244         } else {
245             return systemReleaseVersion;
246         }
247     }
248
249     /**
250      * 获取系统地密钥过期时间(单位:秒)
251      */
252     public static Long getJwtSecretExpireSec() {
253         Long defaultSecs = 86400L;
254         String systemReleaseVersion = (String) CONSTNTS_HOLDER.get("GUNS_JWT_SECRET_EXPIRE");
255         if (ToolUtil.isEmpty(systemReleaseVersion)) {
256             log.error("jwt密钥存在空值!常量名称:GUNS_JWT_SECRET_EXPIRE,采用默认值:1天" + TIPS_END);
257             CONSTNTS_HOLDER.put("GUNS_JWT_SECRET_EXPIRE", String.valueOf(defaultSecs));
258             return defaultSecs;
259         } else {
260             try {
261                 return Long.valueOf(systemReleaseVersion);
262             } catch (NumberFormatException e) {
263                 log.error("jwt密钥过期时间不是数字!常量名称:GUNS_JWT_SECRET_EXPIRE,采用默认值:1天" + TIPS_END);
264                 CONSTNTS_HOLDER.put("GUNS_JWT_SECRET_EXPIRE", String.valueOf(defaultSecs));
265                 return defaultSecs;
266             }
267         }
268     }
269
270     /**
271      * 获取token的header标识
272      */
273     public static String getTokenHeaderName() {
274         String tokenHeaderName = (String) CONSTNTS_HOLDER.get("GUNS_TOKEN_HEADER_NAME");
275         if (ToolUtil.isEmpty(tokenHeaderName)) {
276             String defaultName = "Authorization";
277             CONSTNTS_HOLDER.put("GUNS_TOKEN_HEADER_NAME", defaultName);
278             log.error("获取token的header标识为空!常量名称:GUNS_TOKEN_HEADER_NAME,采用默认值:" + defaultName + TIPS_END);
279             return defaultName;
280         } else {
281             return tokenHeaderName;
282         }
283     }
284
285     /**
286      * 获取租户是否开启的标识,默认是关的
287      */
288     public static Boolean getTenantOpen() {
289         String tenantOpen = (String) CONSTNTS_HOLDER.get("GUNS_TENANT_OPEN");
290         if (ToolUtil.isEmpty(tenantOpen)) {
291             log.error("系统常量存在空值!常量名称:GUNS_TENANT_OPEN,采用默认值:DISABLE" + TIPS_END);
292             return false;
293         } else {
294             return CommonStatus.ENABLE.getCode().equalsIgnoreCase(tenantOpen);
295         }
296     }
297
298 }