ludc
2023-09-07 c84991603548ca07bdcb589bf61ab230a6fb9f3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.vci.ubcs.system.cache;
 
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
 
/**
 * redis中存储的nacos上配置的超管配置信息
 * @author ludc
 * @date 2023/8/31 13:02
 */
@Component
public class NacosConfigCache {
 
    private static final String NACOS_CONFIG_CACHE = "nacos:config";
 
    private static final String ADMIN_INFO_CODE = "admin:info:";
 
    @Autowired
    private Environment environment;
 
    /**
     * 获取存入redis中
     * nacos上配置的超管信息
     * @return
     */
    public BladeUser getAdminUserInfo() {
        try {
            BladeUser user = CacheUtil.getCache(NACOS_CONFIG_CACHE).get(ADMIN_INFO_CODE,BladeUser.class);
            //从缓存中未获取到超级管理员配置信息,从nacos上重新获取并存入缓存。
            if(Func.isEmpty(user)){
                BladeUser adminUser = new BladeUser();
                adminUser.setTenantId(environment.getProperty("user-info.tenant-id", "000000"));
                adminUser.setUserName(environment.getProperty("user-info.user-name","admin"));
                adminUser.setUserId(Func.toLong(environment.getProperty("user-info.id","0")));
                CacheUtil.getCache(NACOS_CONFIG_CACHE).put(ADMIN_INFO_CODE,adminUser);
                return adminUser;
            }
            return user;
        }catch (Exception e){
            throw new ServiceException("从nacos上获取超级管理员配置失败!");
        }
    }
 
}