175 lines
4.4 KiB
JavaScript
175 lines
4.4 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { VoiceService } from '@/api/voice'
|
|
import { message } from 'ant-design-vue'
|
|
|
|
export const useVoiceCopyStore = defineStore('voiceCopy', {
|
|
state: () => ({
|
|
profiles: [],
|
|
activeId: '',
|
|
loaded: false,
|
|
loading: false
|
|
}),
|
|
getters: {
|
|
activeProfile(state) {
|
|
return state.profiles.find(p => p.id === state.activeId) || null
|
|
}
|
|
},
|
|
actions: {
|
|
/**
|
|
* 加载配音列表
|
|
*/
|
|
async load(forceRefresh = false) {
|
|
if(!forceRefresh){
|
|
if (this.loaded && !this.loading) return
|
|
}
|
|
|
|
this.loading = true
|
|
try {
|
|
const res = await VoiceService.getPage({
|
|
pageNo: 1,
|
|
pageSize: 100 // 加载所有数据
|
|
})
|
|
|
|
if (res.code === 0) {
|
|
this.profiles = (res.data.list || []).map((item) => ({
|
|
...item,
|
|
voiceId: item.voiceId || '',
|
|
transcription: item.transcription || '',
|
|
fileUrl: item.fileUrl || ''
|
|
}))
|
|
// 如果有数据且没有选中项,选中第一个
|
|
if (!this.activeId && this.profiles.length > 0) {
|
|
this.activeId = this.profiles[0].id
|
|
}
|
|
this.loaded = true
|
|
} else {
|
|
message.error(res.msg || '加载失败')
|
|
}
|
|
} catch (error) {
|
|
console.error('加载配音列表失败:', error)
|
|
message.error('加载失败,请稍后重试')
|
|
} finally {
|
|
this.loading = false
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 添加配音
|
|
*/
|
|
async add(profile) {
|
|
try {
|
|
const res = await VoiceService.create({
|
|
name: profile.name || `克隆语音-${this.profiles.length + 1}`,
|
|
fileId: profile.fileId,
|
|
autoTranscribe: profile.autoTranscribe || false,
|
|
language: profile.language || 'zh-CN',
|
|
gender: profile.gender || 'female',
|
|
note: profile.note || ''
|
|
})
|
|
|
|
if (res.code === 0) {
|
|
const newProfile = {
|
|
...profile,
|
|
id: res.data
|
|
}
|
|
this.profiles.unshift(newProfile)
|
|
this.activeId = newProfile.id
|
|
await this.load() // 重新加载以获取完整数据
|
|
return newProfile
|
|
} else {
|
|
message.error(res.msg || '创建失败')
|
|
throw new Error(res.msg || '创建失败')
|
|
}
|
|
} catch (error) {
|
|
console.error('添加配音失败:', error)
|
|
throw error
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 更新配音
|
|
*/
|
|
async update(profile) {
|
|
if (!profile.id) {
|
|
return await this.add(profile)
|
|
}
|
|
|
|
try {
|
|
const res = await VoiceService.update({
|
|
id: profile.id,
|
|
name: profile.name,
|
|
language: profile.language,
|
|
gender: profile.gender,
|
|
note: profile.note
|
|
})
|
|
|
|
if (res.code === 0) {
|
|
const index = this.profiles.findIndex(p => p.id === profile.id)
|
|
if (index > -1) {
|
|
// 重新加载以获取最新数据
|
|
await this.load()
|
|
const updated = this.profiles.find(p => p.id === profile.id)
|
|
return updated || profile
|
|
}
|
|
return profile
|
|
} else {
|
|
message.error(res.msg || '更新失败')
|
|
throw new Error(res.msg || '更新失败')
|
|
}
|
|
} catch (error) {
|
|
console.error('更新配音失败:', error)
|
|
throw error
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 复制配音
|
|
*/
|
|
async duplicate(profile, name) {
|
|
const copy = {
|
|
...profile,
|
|
id: null,
|
|
name: name || `${profile.name}-副本`
|
|
}
|
|
return await this.add(copy)
|
|
},
|
|
|
|
/**
|
|
* 删除配音
|
|
*/
|
|
async remove(id) {
|
|
try {
|
|
const res = await VoiceService.delete(id)
|
|
|
|
if (res.code === 0) {
|
|
this.profiles = this.profiles.filter(p => p.id !== id)
|
|
if (this.activeId === id) {
|
|
this.activeId = this.profiles[0]?.id || ''
|
|
}
|
|
} else {
|
|
message.error(res.msg || '删除失败')
|
|
throw new Error(res.msg || '删除失败')
|
|
}
|
|
} catch (error) {
|
|
console.error('删除配音失败:', error)
|
|
throw error
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 选择配音
|
|
*/
|
|
select(id) {
|
|
this.activeId = id
|
|
},
|
|
|
|
/**
|
|
* 刷新数据
|
|
*/
|
|
async refresh() {
|
|
this.loaded = false
|
|
await this.load(true)
|
|
}
|
|
}
|
|
})
|