1
zj
2025-09-25 5e179f4180fd2dc93623699af614df9a2497ded6
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package com.ruoyi.im.config;
 
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
 
public class DynamicRequestBodyBuilder {
    private Map<String, Object> bodyData;
    private Map<String, Object> configurationData;
 
    public DynamicRequestBodyBuilder() {
        bodyData = new HashMap<>();
        configurationData = new HashMap<>();
    }
 
    /**
     * 设置token字段
     */
    public DynamicRequestBodyBuilder setToken(String token) {
        if (token != null) {
            bodyData.put("token", token);
        }
        return this;
    }
 
    /**
     * 设置enabled配置字段
     */
    public DynamicRequestBodyBuilder setEnabled(boolean enabled) {
        configurationData.put("enabled", enabled);
        return this;
    }
 
    /**
     * 设置p2p_chat_banned配置字段
     */
    public DynamicRequestBodyBuilder setP2pChatBanned(boolean banned) {
        configurationData.put("p2p_chat_banned", banned);
        return this;
    }
 
    /**
     * 设置team_chat_banned配置字段
     */
    public DynamicRequestBodyBuilder setTeamChatBanned(boolean banned) {
        configurationData.put("team_chat_banned", banned);
        return this;
    }
 
    /**
     * 设置chatroom_chat_banned配置字段
     */
    public DynamicRequestBodyBuilder setChatroomChatBanned(boolean banned) {
        configurationData.put("chatroom_chat_banned", banned);
        return this;
    }
 
    /**
     * 设置qchat_chat_banned配置字段
     */
    public DynamicRequestBodyBuilder setQchatChatBanned(boolean banned) {
        configurationData.put("qchat_chat_banned", banned);
        return this;
    }
 
    /**
     * 设置push_enabled_when_desktop_online配置字段
     */
    public DynamicRequestBodyBuilder setPushEnabledWhenDesktopOnline(boolean enabled) {
        configurationData.put("push_enabled_when_desktop_online", enabled);
        return this;
    }
 
    /**
     * 设置need_kick字段
     */
    public DynamicRequestBodyBuilder setNeedKick(boolean needKick) {
        bodyData.put("need_kick", needKick);
        return this;
    }
 
    /**
     * 设置kick_notify_extension字段
     */
    public DynamicRequestBodyBuilder setKickNotifyExtension(String extension) {
        if (extension != null) {
            bodyData.put("kick_notify_extension", extension);
        }
        return this;
    }
 
    /**
     * 批量设置配置项
     */
    public DynamicRequestBodyBuilder setConfiguration(Map<String, Object> config) {
        if (config != null) {
            configurationData.putAll(config);
        }
        return this;
    }
 
    /**
     * 批量设置所有字段
     */
    public DynamicRequestBodyBuilder setAllFields(Map<String, Object> fields) {
        if (fields != null) {
            // 处理顶级字段
            if (fields.containsKey("token")) {
                setToken((String) fields.get("token"));
            }
            if (fields.containsKey("need_kick")) {
                setNeedKick((Boolean) fields.get("need_kick"));
            }
            if (fields.containsKey("kick_notify_extension")) {
                setKickNotifyExtension((String) fields.get("kick_notify_extension"));
            }
 
            // 处理配置字段
            if (fields.containsKey("configuration")) {
                Map<String, Object> config = (Map<String, Object>) fields.get("configuration");
                setConfiguration(config);
            }
        }
        return this;
    }
 
    /**
     * 构建JSON字符串
     */
    public String build() {
        // 只有在有配置数据时才添加configuration字段
        if (!configurationData.isEmpty()) {
            bodyData.put("configuration", new HashMap<>(configurationData));
        }
 
        JSONObject jsonObject = new JSONObject(bodyData);
        return jsonObject.toString();
    }
 
    /**
     * 获取Map形式的数据
     */
    public Map<String, Object> getBodyData() {
        Map<String, Object> result = new HashMap<>(bodyData);
        if (!configurationData.isEmpty()) {
            result.put("configuration", new HashMap<>(configurationData));
        }
        return result;
    }
 
    /**
     * 清空所有已设置的数据
     */
    public void clear() {
        bodyData.clear();
        configurationData.clear();
    }
 
    /**
     * 使用示例
     */
    public static void main(String[] args) {
        // 示例1: 只设置部分字段
        DynamicRequestBodyBuilder builder = new DynamicRequestBodyBuilder();
        String partialBody = builder
                .setToken("custom_token_123")
                .setEnabled(true)
                .build();
        System.out.println("部分字段请求体: " + partialBody);
 
        // 示例2: 使用Map批量设置
        builder.clear();
        Map<String, Object> config = new HashMap<>();
        config.put("enabled", true);
        config.put("p2p_chat_banned", true);
 
        Map<String, Object> fields = new HashMap<>();
        fields.put("token", "batch_token");
        fields.put("need_kick", true);
        fields.put("configuration", config);
 
        String batchBody = builder.setAllFields(fields).build();
        System.out.println("批量设置请求体: " + batchBody);
 
        // 示例3: 完全不设置任何字段
        builder.clear();
        String emptyBody = builder.build();
        System.out.println("空请求体: " + emptyBody);
    }
}