From 915f4cac5e166de4213c61ec1c418b6bff2e5cb2 Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Wed, 21 May 2025 09:55:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E9=80=9A=E7=94=A8dict=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E5=AD=97=E6=AE=B5=E8=BF=9B=E8=A1=8C=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/ModelEditDialog.vue | 98 ++++++++----------- 1 file changed, 42 insertions(+), 56 deletions(-) diff --git a/main/manager-web/src/components/ModelEditDialog.vue b/main/manager-web/src/components/ModelEditDialog.vue index 35307d42..406284a5 100644 --- a/main/manager-web/src/components/ModelEditDialog.vue +++ b/main/manager-web/src/components/ModelEditDialog.vue @@ -66,13 +66,12 @@
- - - - - + +
@@ -116,7 +115,7 @@ export default { pendingProviderType: null, pendingModelData: null, dynamicCallInfoFields: [], - variablesJson: '', + fieldJsonMap: {}, // 用于存储JSON字段的字符串形式 form: { id: "", modelType: "", @@ -178,12 +177,9 @@ export default { docLink: "", remark: "", sort: 0, - configJson: {}, - variablesJson: {}, + configJson: {} }; - this.dynamicCallInfoFields.forEach(field => { - this.$set(this.form.configJson, field.prop, ''); - }); + this.fieldJsonMap = {}; }, resetProviders() { this.providers = []; @@ -209,17 +205,14 @@ export default { handleSave() { this.saving = true; // 开始保存加载 - // 确保 variables 是字典 - if (typeof this.form.configJson.variables === 'string') { - const parsed = this.validateVariablesJson(this.form.configJson.variables); - if (parsed === null) { - this.saving = false; - return; + // 处理所有JSON字段 + Object.keys(this.fieldJsonMap).forEach(key => { + const parsed = this.validateJson(this.fieldJsonMap[key]); + if (parsed !== null) { + this.form.configJson[key] = parsed; } - this.form.configJson.variables = parsed; - } + }); - const provideCode = this.form.configJson.type; const formData = { id: this.modelData.id, modelCode: this.form.modelCode, @@ -229,13 +222,11 @@ export default { docLink: this.form.docLink, remark: this.form.remark, sort: this.form.sort || 0, - configJson: { - ...this.form.configJson, - } + configJson: { ...this.form.configJson } }; this.$emit("save", { - provideCode, + provideCode: this.form.configJson.type, formData, done: () => { this.saving = false; // 保存完成后回调 @@ -256,7 +247,6 @@ export default { value: String(item.providerCode) })); this.providersLoaded = true; - this.allProvidersData = data; if (this.pendingProviderType) { @@ -271,7 +261,7 @@ export default { this.dynamicCallInfoFields = JSON.parse(provider.fields || '[]').map(f => ({ label: f.label, prop: f.key, - type: f.key === 'variables' ? 'json-textarea' : (f.type === 'password' ? 'password' : 'text'), + type: f.type === 'dict' ? 'json-textarea' : (f.type === 'password' ? 'password' : 'text'), placeholder: `请输入${f.label}` })); @@ -288,7 +278,10 @@ export default { this.dynamicCallInfoFields.forEach(field => { if (!configJson.hasOwnProperty(field.prop)) { configJson[field.prop] = ''; - } else if (field.prop !== 'variables' && typeof configJson[field.prop] !== 'string') { + } else if (field.type === 'json-textarea') { + this.$set(this.fieldJsonMap, field.prop, this.formatJson(configJson[field.prop])); + configJson[field.prop] = this.ensureObject(configJson[field.prop]); + } else if (typeof configJson[field.prop] !== 'string') { configJson[field.prop] = String(configJson[field.prop]); } }); @@ -303,42 +296,43 @@ export default { docLink: model.docLink, remark: model.remark, sort: Number(model.sort) || 0, - configJson: { - ...configJson - } + configJson: { ...configJson } }; - // 读出时的JSON转换 - if (this.form.configJson.variables) { - this.variablesJson = JSON.stringify(this.form.configJson.variables, null, 2) + }, + handleJsonChange(field, value) { + const parsed = this.validateJson(value); + if (parsed !== null) { + this.form.configJson[field] = parsed; } }, - - // 输入监听 - handleVariablesChange(value) { - const parsed = this.validateVariablesJson(value); - if (parsed !== null) this.form.configJson.variables = parsed; - }, - - // 数据校验 - validateVariablesJson(value) { + validateJson(value) { try { const parsed = JSON.parse(value); - // 校验是否为非空字典 if (typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed)) { return parsed; } this.$message.error({ - message: '必须输入字典格式(如 {"key":"value"}),保存则保留原数据', + message: '必须输入字典格式(如 {"key":"value"}),保存则使用原数据', showClose: true }); return null; } catch (e) { this.$message.error({ - message: 'JSON格式错误(如 {"key":"value"}),保存则保留原数据', + message: 'JSON格式错误(如 {"key":"value"}),保存则使用原数据', showClose: true }); return null; } + }, + formatJson(obj) { + try { + return JSON.stringify(obj, null, 2); + } catch { + return ''; + } + }, + ensureObject(value) { + return typeof value === 'object' ? value : {}; } } }; @@ -364,7 +358,6 @@ export default { justify-content: center; } - .custom-close-btn { position: absolute; top: 20px; @@ -500,21 +493,14 @@ export default { height: 32px; } - .custom-form .el-form-item { margin-bottom: 20px; } - -.custom-input-bg .el-input__inner { - height: 32px; -} - - .custom-form .el-form-item__label { color: #3d4566; font-weight: normal; text-align: right; padding-right: 20px; } - + \ No newline at end of file