|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { |
|
|
STORAGE_KEYS, |
|
|
DEFAULT_CONFIG, |
|
|
} from '../../constants/playground.constants'; |
|
|
|
|
|
const MESSAGES_STORAGE_KEY = 'playground_messages'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const saveConfig = (config) => { |
|
|
try { |
|
|
const configToSave = { |
|
|
...config, |
|
|
timestamp: new Date().toISOString(), |
|
|
}; |
|
|
localStorage.setItem(STORAGE_KEYS.CONFIG, JSON.stringify(configToSave)); |
|
|
} catch (error) { |
|
|
console.error('保存配置失败:', error); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const saveMessages = (messages) => { |
|
|
try { |
|
|
const messagesToSave = { |
|
|
messages, |
|
|
timestamp: new Date().toISOString(), |
|
|
}; |
|
|
localStorage.setItem(STORAGE_KEYS.MESSAGES, JSON.stringify(messagesToSave)); |
|
|
} catch (error) { |
|
|
console.error('保存消息失败:', error); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const loadConfig = () => { |
|
|
try { |
|
|
const savedConfig = localStorage.getItem(STORAGE_KEYS.CONFIG); |
|
|
if (savedConfig) { |
|
|
const parsedConfig = JSON.parse(savedConfig); |
|
|
|
|
|
const mergedConfig = { |
|
|
inputs: { |
|
|
...DEFAULT_CONFIG.inputs, |
|
|
...parsedConfig.inputs, |
|
|
}, |
|
|
parameterEnabled: { |
|
|
...DEFAULT_CONFIG.parameterEnabled, |
|
|
...parsedConfig.parameterEnabled, |
|
|
}, |
|
|
showDebugPanel: |
|
|
parsedConfig.showDebugPanel || DEFAULT_CONFIG.showDebugPanel, |
|
|
customRequestMode: |
|
|
parsedConfig.customRequestMode || DEFAULT_CONFIG.customRequestMode, |
|
|
customRequestBody: |
|
|
parsedConfig.customRequestBody || DEFAULT_CONFIG.customRequestBody, |
|
|
}; |
|
|
|
|
|
return mergedConfig; |
|
|
} |
|
|
} catch (error) { |
|
|
console.error('加载配置失败:', error); |
|
|
} |
|
|
|
|
|
return DEFAULT_CONFIG; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const loadMessages = () => { |
|
|
try { |
|
|
const savedMessages = localStorage.getItem(STORAGE_KEYS.MESSAGES); |
|
|
if (savedMessages) { |
|
|
const parsedMessages = JSON.parse(savedMessages); |
|
|
return parsedMessages.messages || null; |
|
|
} |
|
|
} catch (error) { |
|
|
console.error('加载消息失败:', error); |
|
|
} |
|
|
|
|
|
return null; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const clearConfig = () => { |
|
|
try { |
|
|
localStorage.removeItem(STORAGE_KEYS.CONFIG); |
|
|
localStorage.removeItem(STORAGE_KEYS.MESSAGES); |
|
|
} catch (error) { |
|
|
console.error('清除配置失败:', error); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const clearMessages = () => { |
|
|
try { |
|
|
localStorage.removeItem(STORAGE_KEYS.MESSAGES); |
|
|
} catch (error) { |
|
|
console.error('清除消息失败:', error); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const hasStoredConfig = () => { |
|
|
try { |
|
|
return localStorage.getItem(STORAGE_KEYS.CONFIG) !== null; |
|
|
} catch (error) { |
|
|
console.error('检查配置失败:', error); |
|
|
return false; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getConfigTimestamp = () => { |
|
|
try { |
|
|
const savedConfig = localStorage.getItem(STORAGE_KEYS.CONFIG); |
|
|
if (savedConfig) { |
|
|
const parsedConfig = JSON.parse(savedConfig); |
|
|
return parsedConfig.timestamp || null; |
|
|
} |
|
|
} catch (error) { |
|
|
console.error('获取配置时间戳失败:', error); |
|
|
} |
|
|
return null; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const exportConfig = (config, messages = null) => { |
|
|
try { |
|
|
const configToExport = { |
|
|
...config, |
|
|
messages: messages || loadMessages(), |
|
|
exportTime: new Date().toISOString(), |
|
|
version: '1.0', |
|
|
}; |
|
|
|
|
|
const dataStr = JSON.stringify(configToExport, null, 2); |
|
|
const dataBlob = new Blob([dataStr], { type: 'application/json' }); |
|
|
|
|
|
const link = document.createElement('a'); |
|
|
link.href = URL.createObjectURL(dataBlob); |
|
|
link.download = `playground-config-${new Date().toISOString().split('T')[0]}.json`; |
|
|
link.click(); |
|
|
|
|
|
URL.revokeObjectURL(link.href); |
|
|
} catch (error) { |
|
|
console.error('导出配置失败:', error); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const importConfig = (file) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
try { |
|
|
const reader = new FileReader(); |
|
|
reader.onload = (e) => { |
|
|
try { |
|
|
const importedConfig = JSON.parse(e.target.result); |
|
|
|
|
|
if (importedConfig.inputs && importedConfig.parameterEnabled) { |
|
|
|
|
|
if ( |
|
|
importedConfig.messages && |
|
|
Array.isArray(importedConfig.messages) |
|
|
) { |
|
|
saveMessages(importedConfig.messages); |
|
|
} |
|
|
|
|
|
resolve(importedConfig); |
|
|
} else { |
|
|
reject(new Error('配置文件格式无效')); |
|
|
} |
|
|
} catch (parseError) { |
|
|
reject(new Error('解析配置文件失败: ' + parseError.message)); |
|
|
} |
|
|
}; |
|
|
reader.onerror = () => reject(new Error('读取文件失败')); |
|
|
reader.readAsText(file); |
|
|
} catch (error) { |
|
|
reject(new Error('导入配置失败: ' + error.message)); |
|
|
} |
|
|
}); |
|
|
}; |
|
|
|