package com.ruoyi.im.config; import java.util.HashMap; import java.util.Map; import org.json.JSONObject; public class DynamicRequestBodyBuilder { private Map bodyData; private Map 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 config) { if (config != null) { configurationData.putAll(config); } return this; } /** * 批量设置所有字段 */ public DynamicRequestBodyBuilder setAllFields(Map 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 config = (Map) 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 getBodyData() { Map 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 config = new HashMap<>(); config.put("enabled", true); config.put("p2p_chat_banned", true); Map 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); } }