mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
fix: 新增模型页面dict字段错误解析
This commit is contained in:
parent
b226ee5430
commit
6f958ee4a6
2 changed files with 102 additions and 2 deletions
|
|
@ -77,7 +77,10 @@
|
|||
<el-form :model="formData.configJson" label-width="auto" label-position="left" class="custom-form">
|
||||
<div v-for="(row, rowIndex) in chunkedCallInfoFields" :key="rowIndex" class="form-row">
|
||||
<el-form-item v-for="field in row" :key="field.prop" :label="field.label" :prop="field.prop" style="flex: 1;">
|
||||
<el-input v-model="formData.configJson[field.prop]" :placeholder="field.placeholder"
|
||||
<el-input v-if="field.type === 'json-textarea'" v-model="formData.configJson[field.prop]" type="textarea"
|
||||
:rows="3" :placeholder="field.placeholder" class="custom-input-bg">
|
||||
</el-input>
|
||||
<el-input v-else v-model="formData.configJson[field.prop]" :placeholder="field.placeholder"
|
||||
:type="field.type || 'text'" class="custom-input-bg" :show-password="field.type === 'password'">
|
||||
</el-input>
|
||||
</el-form-item>
|
||||
|
|
@ -161,7 +164,7 @@ export default {
|
|||
fields: JSON.parse(item.fields || '[]').map(f => ({
|
||||
label: f.label,
|
||||
prop: f.key,
|
||||
type: f.type === 'password' ? 'password' : 'text',
|
||||
type: f.type === 'dict' ? 'json-textarea' : f.type === 'password' ? 'password' : 'text',
|
||||
placeholder: `请输入${f.key}`
|
||||
}))
|
||||
}))
|
||||
|
|
@ -204,6 +207,24 @@ export default {
|
|||
return;
|
||||
}
|
||||
|
||||
// dict 字段必须解析为 JSON 对象再提交,否则会以字符串入库
|
||||
for (const field of this.providerFields) {
|
||||
if (field.type !== 'json-textarea') continue;
|
||||
const raw = this.formData.configJson[field.prop];
|
||||
if (!raw || typeof raw === 'object') continue;
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
||||
throw new Error('not object');
|
||||
}
|
||||
this.formData.configJson[field.prop] = parsed;
|
||||
} catch (e) {
|
||||
this.$message.error('必须输入字典格式(如 {"key":"value"})');
|
||||
this.saving = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const submitData = {
|
||||
id: this.formData.id || '',
|
||||
modelName: this.formData.modelName || '',
|
||||
|
|
|
|||
79
main/manager-web/tests/modelAddDialog.test.mjs
Normal file
79
main/manager-web/tests/modelAddDialog.test.mjs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import Vue from 'vue';
|
||||
import { parseComponent } from 'vue-template-compiler';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import source from '../src/components/AddModelDialog.vue?raw';
|
||||
|
||||
// The unit-test runner has no Vue SFC plugin. Load the real component's script
|
||||
// with its API and dialog dependencies stubbed; keep Vue's reactivity.
|
||||
const script = parseComponent(source).script.content
|
||||
.replace(/^import .*;$/gm, '')
|
||||
.replace('export default', 'return');
|
||||
const component = new Function('CustomDialog', 'Api', script)({}, {});
|
||||
|
||||
function makeVm() {
|
||||
const vm = new Vue({
|
||||
...component,
|
||||
propsData: { visible: false, modelType: 'TTS' },
|
||||
});
|
||||
vm.$message = { error: vi.fn() };
|
||||
vm.$t = (key) => key;
|
||||
vm.providerFields = [
|
||||
{ label: '音量', prop: 'volume', type: 'json-textarea', placeholder: '请输入volume' },
|
||||
{ label: '密钥', prop: 'api_key', type: 'password', placeholder: '请输入api_key' },
|
||||
];
|
||||
vm.formData.supplier = 'test-provider';
|
||||
return vm;
|
||||
}
|
||||
|
||||
function submitted(vm) {
|
||||
let captured;
|
||||
vm.$on('confirm', (data) => {
|
||||
captured = data;
|
||||
});
|
||||
vm.confirm();
|
||||
return captured;
|
||||
}
|
||||
|
||||
describe('add dialog dict fields', () => {
|
||||
it('parses valid JSON dict input into an object before submit', () => {
|
||||
const vm = makeVm();
|
||||
vm.formData.configJson = { volume: '{"speed":1,"volume":10}', api_key: 'sk-xxx' };
|
||||
|
||||
const data = submitted(vm);
|
||||
|
||||
expect(data.configJson.volume).toEqual({ speed: 1, volume: 10 });
|
||||
expect(data.configJson.api_key).toBe('sk-xxx');
|
||||
expect(vm.$message.error).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('blocks invalid JSON input and does not emit confirm', () => {
|
||||
const vm = makeVm();
|
||||
vm.formData.configJson = { volume: '{bad json', api_key: 'sk-xxx' };
|
||||
|
||||
expect(submitted(vm)).toBeUndefined();
|
||||
expect(vm.$message.error).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it.each(['[1,2]', 'null', '"text"', '1'])(
|
||||
'blocks non-object JSON %s',
|
||||
(value) => {
|
||||
const vm = makeVm();
|
||||
vm.formData.configJson = { volume: value, api_key: 'sk-xxx' };
|
||||
|
||||
expect(submitted(vm)).toBeUndefined();
|
||||
expect(vm.$message.error).toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it('leaves empty strings and existing object values untouched', () => {
|
||||
const vm = makeVm();
|
||||
const obj = { speed: 2 };
|
||||
vm.formData.configJson = { volume: obj, api_key: '' };
|
||||
|
||||
const data = submitted(vm);
|
||||
|
||||
expect(data.configJson.volume).toBe(obj);
|
||||
expect(data.configJson.api_key).toBe('');
|
||||
expect(vm.$message.error).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue