send-stream
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import storage from '@/utils/storage'
|
||||
|
||||
const STORAGE_KEY = 'cosy_voice_profiles'
|
||||
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) {
|
||||
@@ -15,48 +15,158 @@ export const useVoiceCopyStore = defineStore('voiceCopy', {
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
generateId() {
|
||||
return `${Date.now()}_${Math.floor(Math.random() * 1e6)}`
|
||||
},
|
||||
/**
|
||||
* 加载配音列表
|
||||
*/
|
||||
async load() {
|
||||
if (this.loaded) return
|
||||
const list = await storage.getJSON(STORAGE_KEY, [])
|
||||
this.profiles = Array.isArray(list) ? list : []
|
||||
if (!this.activeId && this.profiles.length) this.activeId = this.profiles[0].id
|
||||
this.loaded = true
|
||||
},
|
||||
async persist() {
|
||||
await storage.setJSON(STORAGE_KEY, this.profiles)
|
||||
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) {
|
||||
const id = this.generateId()
|
||||
const name = profile.name || `克隆语音-${this.profiles.length + 1}`
|
||||
const payload = { ...profile, id, name }
|
||||
this.profiles.unshift(payload)
|
||||
this.activeId = id
|
||||
await this.persist()
|
||||
return payload
|
||||
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) {
|
||||
const idx = this.profiles.findIndex(p => p.id === profile.id)
|
||||
if (idx === -1) return await this.add({ ...profile, id: '' })
|
||||
this.profiles[idx] = { ...profile }
|
||||
await this.persist()
|
||||
return this.profiles[idx]
|
||||
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: '', name }
|
||||
const copy = {
|
||||
...profile,
|
||||
id: null,
|
||||
name: name || `${profile.name}-副本`
|
||||
}
|
||||
return await this.add(copy)
|
||||
},
|
||||
|
||||
/**
|
||||
* 删除配音
|
||||
*/
|
||||
async remove(id) {
|
||||
this.profiles = this.profiles.filter(p => p.id !== id)
|
||||
if (this.activeId === id) this.activeId = this.profiles[0]?.id || ''
|
||||
await this.persist()
|
||||
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()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user