UnisKB/ui/src/views/application/component/TTSModeParamSettingDialog.vue

152 lines
4.5 KiB
Vue
Raw Normal View History

2025-06-03 08:08:49 +00:00
<template>
<el-dialog
align-center
:title="$t('common.paramSetting')"
v-model="dialogVisible"
style="width: 550px"
append-to-body
:close-on-click-modal="false"
:close-on-press-escape="false"
>
<DynamicsForm
v-model="form_data"
:model="form_data"
label-position="top"
require-asterisk-position="right"
:render_data="model_form_field"
ref="dynamicsFormRef"
>
</DynamicsForm>
<template #footer>
<div class="flex-between">
<el-button @click="testPlay" :loading="playLoading">
<AppIcon iconName="app-video-play" class="mr-4"></AppIcon>
2025-06-05 09:28:07 +00:00
{{ $t('views.application.form.voicePlay.listeningTest') }}
2025-06-03 08:08:49 +00:00
</el-button>
<span class="dialog-footer">
<el-button @click.prevent="dialogVisible = false">
{{ $t('common.cancel') }}
</el-button>
<el-button type="primary" @click="submit" :loading="loading">
{{ $t('common.confirm') }}
</el-button>
</span>
</div>
</template>
</el-dialog>
<!-- 先渲染不然不能播放 -->
<audio ref="audioPlayer" controls hidden="hidden"></audio>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { FormField } from '@/components/dynamics-form/type'
2025-06-25 13:58:01 +00:00
import ModelAPI from '@/api/model/model'
2025-06-03 09:29:33 +00:00
import applicationApi from '@/api/application/application'
2025-06-03 08:08:49 +00:00
import DynamicsForm from '@/components/dynamics-form/index.vue'
2025-06-18 12:27:26 +00:00
import { useRoute } from 'vue-router'
const route = useRoute()
2025-06-03 08:08:49 +00:00
const {
2025-06-18 12:27:26 +00:00
params: { id },
} = route as any
2025-06-03 08:08:49 +00:00
const tts_model_id = ref('')
const model_form_field = ref<Array<FormField>>([])
const emit = defineEmits(['refresh'])
const dynamicsFormRef = ref<InstanceType<typeof DynamicsForm>>()
const form_data = ref<any>({})
const dialogVisible = ref(false)
const loading = ref(false)
const playLoading = ref(false)
const getApi = (model_id: string, application_id?: string) => {
return application_id
? applicationApi.getModelParamsForm(application_id, model_id, loading)
2025-06-25 13:58:01 +00:00
: ModelAPI.getModelParamsForm(model_id, loading)
2025-06-03 08:08:49 +00:00
}
const open = (model_id: string, application_id?: string, model_setting_data?: any) => {
form_data.value = {}
tts_model_id.value = model_id
const api = getApi(model_id, application_id)
api.then((ok) => {
model_form_field.value = ok.data
const resp = ok.data
.map((item: any) => ({
2025-06-18 12:27:26 +00:00
[item.field]: item.show_default_value !== false ? item.default_value : undefined,
2025-06-03 08:08:49 +00:00
}))
.reduce((x, y) => ({ ...x, ...y }), {})
// 删除不存在的字段
if (model_setting_data) {
Object.keys(model_setting_data).forEach((key) => {
if (!(key in resp)) {
delete model_setting_data[key]
}
})
}
model_setting_data = { ...resp, ...model_setting_data }
// 渲染动态表单
dynamicsFormRef.value?.render(model_form_field.value, model_setting_data)
})
dialogVisible.value = true
}
const reset_default = (model_id: string, application_id?: string) => {
const api = getApi(model_id, application_id)
api.then((ok) => {
model_form_field.value = ok.data
const model_setting_data = ok.data
.map((item) => ({
2025-06-18 12:27:26 +00:00
[item.field]: item.show_default_value !== false ? item.default_value : undefined,
2025-06-03 08:08:49 +00:00
}))
.reduce((x, y) => ({ ...x, ...y }), {})
emit('refresh', model_setting_data)
})
}
const submit = async () => {
dynamicsFormRef.value?.validate().then(() => {
emit('refresh', form_data.value)
dialogVisible.value = false
})
}
const audioPlayer = ref<HTMLAudioElement | null>(null)
const testPlay = () => {
const data = {
...form_data.value,
2025-06-18 12:27:26 +00:00
tts_model_id: tts_model_id.value,
2025-06-03 08:08:49 +00:00
}
2025-06-13 09:17:01 +00:00
// applicationApi
// .playDemoText(id as string, data, playLoading)
// .then(async (res: any) => {
// if (res.type === 'application/json') {
// const text = await res.text()
// MsgError(text)
// return
// }
// // 创建 Blob 对象
// const blob = new Blob([res], { type: 'audio/mp3' })
2025-06-03 08:08:49 +00:00
2025-06-13 09:17:01 +00:00
// // 创建对象 URL
// const url = URL.createObjectURL(blob)
2025-06-03 08:08:49 +00:00
2025-06-13 09:17:01 +00:00
// // 检查 audioPlayer 是否已经引用了 DOM 元素
// if (audioPlayer.value instanceof HTMLAudioElement) {
// audioPlayer.value.src = url
// audioPlayer.value.play() // 自动播放音频
// } else {
// console.error('audioPlayer.value is not an instance of HTMLAudioElement')
// }
// })
// .catch((err) => {
// console.log('err: ', err)
// })
2025-06-03 08:08:49 +00:00
}
defineExpose({ open, reset_default })
</script>
<style lang="scss" scoped></style>