前端
This commit is contained in:
62
frontend/app/web-gold/src/stores/voiceCopy.js
Normal file
62
frontend/app/web-gold/src/stores/voiceCopy.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import storage from '@/utils/storage'
|
||||
|
||||
const STORAGE_KEY = 'cosy_voice_profiles'
|
||||
|
||||
export const useVoiceCopyStore = defineStore('voiceCopy', {
|
||||
state: () => ({
|
||||
profiles: [],
|
||||
activeId: '',
|
||||
loaded: false,
|
||||
}),
|
||||
getters: {
|
||||
activeProfile(state) {
|
||||
return state.profiles.find(p => p.id === state.activeId) || null
|
||||
}
|
||||
},
|
||||
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)
|
||||
},
|
||||
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
|
||||
},
|
||||
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]
|
||||
},
|
||||
async duplicate(profile, name) {
|
||||
const copy = { ...profile, id: '', 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()
|
||||
},
|
||||
select(id) {
|
||||
this.activeId = id
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user