goupan
2024-04-03 5506e9a45e717ffcb67ec313b5a4e8206d9b3a39
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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");
    }
 
}