fix: 修复导入、导出错误

This commit is contained in:
ChenZhaoYu 2023-03-14 08:28:43 +08:00
parent cf788b190a
commit 3a1466bace
1 changed files with 17 additions and 11 deletions

View File

@ -61,7 +61,7 @@ const modalMode = ref('')
const tempModifiedItem = ref<any>({}) const tempModifiedItem = ref<any>({})
// 使Modal, tempPromptKey, // 使Modal, tempPromptKey,
const changeShowModal = (mode: string, selected = { key: '', value: '' }) => { const changeShowModal = (mode: 'add' | 'modify' | 'local_import', selected = { key: '', value: '' }) => {
if (mode === 'add') { if (mode === 'add') {
tempPromptKey.value = '' tempPromptKey.value = ''
tempPromptValue.value = '' tempPromptValue.value = ''
@ -103,7 +103,7 @@ const addPromptTemplate = () => {
} }
promptList.value.unshift({ key: tempPromptKey.value, value: tempPromptValue.value } as never) promptList.value.unshift({ key: tempPromptKey.value, value: tempPromptValue.value } as never)
message.success(t('common.addSuccess')) message.success(t('common.addSuccess'))
changeShowModal('') changeShowModal('add')
} }
const modifyPromptTemplate = () => { const modifyPromptTemplate = () => {
@ -132,7 +132,7 @@ const modifyPromptTemplate = () => {
promptList.value = [{ key: tempPromptKey.value, value: tempPromptValue.value }, ...tempList] as never promptList.value = [{ key: tempPromptKey.value, value: tempPromptValue.value }, ...tempList] as never
message.success(t('common.editSuccess')) message.success(t('common.editSuccess'))
changeShowModal('') changeShowModal('modify')
} }
const deletePromptTemplate = (row: { key: string; value: string }) => { const deletePromptTemplate = (row: { key: string; value: string }) => {
@ -187,11 +187,9 @@ const importPromptTemplate = () => {
promptList.value.unshift({ key: i[key], value: i[value] } as never) promptList.value.unshift({ key: i[key], value: i[value] } as never)
} }
message.success(t('common.importSuccess')) message.success(t('common.importSuccess'))
changeShowModal('')
} }
catch { catch {
message.error('JSON 格式错误,请检查 JSON 格式') message.error('JSON 格式错误,请检查 JSON 格式')
changeShowModal('')
} }
} }
@ -213,17 +211,25 @@ const exportPromptTemplate = () => {
const downloadPromptTemplate = async () => { const downloadPromptTemplate = async () => {
try { try {
importLoading.value = true importLoading.value = true
await fetch(downloadURL.value) const response = await fetch(downloadURL.value)
.then(response => response.json()) const jsonData = await response.json()
.then((jsonData) => { if ('key' in jsonData[0] && 'value' in jsonData[0])
tempPromptValue.value = JSON.stringify(jsonData) tempPromptValue.value = JSON.stringify(jsonData)
}).then(() => { if ('act' in jsonData[0] && 'prompt' in jsonData[0]) {
importPromptTemplate() const newJsonData = jsonData.map((item: { act: string; prompt: string }) => {
return {
key: item.act,
value: item.prompt,
}
}) })
tempPromptValue.value = JSON.stringify(newJsonData)
}
importPromptTemplate()
downloadURL.value = '' downloadURL.value = ''
} }
catch { catch {
message.error(t('store.downloadError')) message.error(t('store.downloadError'))
downloadURL.value = ''
} }
finally { finally {
importLoading.value = false importLoading.value = false