2026-04-08 18:09:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* AI 创作全局状态(Pinia Store)
|
|
|
|
|
|
*
|
|
|
|
|
|
* 从 lesingle-aicreate-client/utils/store.js 迁移
|
|
|
|
|
|
* 保留原有字段和方法,适配 Pinia setup 语法
|
|
|
|
|
|
*/
|
|
|
|
|
|
import { defineStore } from 'pinia'
|
2026-04-09 12:52:39 +08:00
|
|
|
|
import { ref } from 'vue'
|
2026-04-08 18:09:05 +08:00
|
|
|
|
|
|
|
|
|
|
export const useAicreateStore = defineStore('aicreate', () => {
|
|
|
|
|
|
// ─── 认证信息 ───
|
|
|
|
|
|
const phone = ref(localStorage.getItem('le_phone') || '')
|
|
|
|
|
|
const orgId = ref(localStorage.getItem('le_orgId') || '')
|
|
|
|
|
|
const appSecret = ref(localStorage.getItem('le_appSecret') || '')
|
|
|
|
|
|
const sessionToken = ref(sessionStorage.getItem('le_sessionToken') || '')
|
|
|
|
|
|
|
|
|
|
|
|
// ─── 创作流程数据 ───
|
|
|
|
|
|
const imageUrl = ref('')
|
|
|
|
|
|
const extractId = ref('')
|
|
|
|
|
|
const characters = ref<any[]>([])
|
|
|
|
|
|
const selectedCharacter = ref<any>(null)
|
|
|
|
|
|
const selectedStyle = ref('')
|
|
|
|
|
|
const storyData = ref<any>(null)
|
|
|
|
|
|
const workId = ref('')
|
|
|
|
|
|
const workDetail = ref<any>(null)
|
|
|
|
|
|
const authRedirectUrl = ref('')
|
|
|
|
|
|
|
2026-04-08 22:58:07 +08:00
|
|
|
|
// ─── Tab 切换状态保存 ───
|
|
|
|
|
|
const lastCreateRoute = ref('')
|
|
|
|
|
|
|
2026-04-08 18:09:05 +08:00
|
|
|
|
// ─── 方法 ───
|
|
|
|
|
|
function setPhone(val: string) {
|
|
|
|
|
|
phone.value = val
|
|
|
|
|
|
localStorage.setItem('le_phone', val)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setOrg(id: string, secret: string) {
|
|
|
|
|
|
orgId.value = id
|
|
|
|
|
|
appSecret.value = secret
|
|
|
|
|
|
localStorage.setItem('le_orgId', id)
|
|
|
|
|
|
localStorage.setItem('le_appSecret', secret)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setSession(id: string, token: string) {
|
|
|
|
|
|
orgId.value = id
|
|
|
|
|
|
sessionToken.value = token
|
|
|
|
|
|
localStorage.setItem('le_orgId', id)
|
|
|
|
|
|
sessionStorage.setItem('le_orgId', id)
|
|
|
|
|
|
sessionStorage.setItem('le_sessionToken', token)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function clearSession() {
|
|
|
|
|
|
sessionToken.value = ''
|
|
|
|
|
|
sessionStorage.removeItem('le_sessionToken')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 22:58:07 +08:00
|
|
|
|
function setLastCreateRoute(path: string) {
|
|
|
|
|
|
lastCreateRoute.value = path
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function clearLastCreateRoute() {
|
|
|
|
|
|
lastCreateRoute.value = ''
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 18:09:05 +08:00
|
|
|
|
function reset() {
|
|
|
|
|
|
imageUrl.value = ''
|
|
|
|
|
|
extractId.value = ''
|
|
|
|
|
|
characters.value = []
|
|
|
|
|
|
selectedCharacter.value = null
|
|
|
|
|
|
selectedStyle.value = ''
|
|
|
|
|
|
storyData.value = null
|
|
|
|
|
|
workId.value = ''
|
|
|
|
|
|
workDetail.value = null
|
2026-04-08 22:58:07 +08:00
|
|
|
|
lastCreateRoute.value = ''
|
2026-04-09 12:52:39 +08:00
|
|
|
|
// 清除所有 localStorage 中的创作相关数据
|
2026-04-08 18:09:05 +08:00
|
|
|
|
localStorage.removeItem('le_workId')
|
2026-04-09 12:52:39 +08:00
|
|
|
|
localStorage.removeItem('le_phone')
|
|
|
|
|
|
localStorage.removeItem('le_orgId')
|
|
|
|
|
|
localStorage.removeItem('le_appSecret')
|
|
|
|
|
|
// 清除 sessionStorage 中的恢复数据
|
|
|
|
|
|
sessionStorage.removeItem('le_recovery')
|
2026-04-08 18:09:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function saveRecoveryState() {
|
|
|
|
|
|
const recovery = {
|
|
|
|
|
|
path: window.location.pathname || '/',
|
|
|
|
|
|
workId: workId.value || localStorage.getItem('le_workId') || '',
|
|
|
|
|
|
imageUrl: imageUrl.value || '',
|
|
|
|
|
|
extractId: extractId.value || '',
|
|
|
|
|
|
selectedStyle: selectedStyle.value || '',
|
|
|
|
|
|
savedAt: Date.now()
|
|
|
|
|
|
}
|
|
|
|
|
|
sessionStorage.setItem('le_recovery', JSON.stringify(recovery))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function restoreRecoveryState() {
|
|
|
|
|
|
const raw = sessionStorage.getItem('le_recovery')
|
|
|
|
|
|
if (!raw) return null
|
|
|
|
|
|
try {
|
|
|
|
|
|
const recovery = JSON.parse(raw)
|
|
|
|
|
|
if (Date.now() - recovery.savedAt > 30 * 60 * 1000) {
|
|
|
|
|
|
sessionStorage.removeItem('le_recovery')
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
if (recovery.workId) workId.value = recovery.workId
|
|
|
|
|
|
if (recovery.imageUrl) imageUrl.value = recovery.imageUrl
|
|
|
|
|
|
if (recovery.extractId) extractId.value = recovery.extractId
|
|
|
|
|
|
if (recovery.selectedStyle) selectedStyle.value = recovery.selectedStyle
|
|
|
|
|
|
sessionStorage.removeItem('le_recovery')
|
|
|
|
|
|
return recovery
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
sessionStorage.removeItem('le_recovery')
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
// 认证
|
|
|
|
|
|
phone, orgId, appSecret, sessionToken, authRedirectUrl,
|
|
|
|
|
|
setPhone, setOrg, setSession, clearSession,
|
|
|
|
|
|
// 创作流程
|
|
|
|
|
|
imageUrl, extractId, characters, selectedCharacter,
|
|
|
|
|
|
selectedStyle, storyData, workId, workDetail,
|
|
|
|
|
|
reset, saveRecoveryState, restoreRecoveryState,
|
2026-04-08 22:58:07 +08:00
|
|
|
|
// Tab 切换状态
|
|
|
|
|
|
lastCreateRoute, setLastCreateRoute, clearLastCreateRoute,
|
2026-04-08 18:09:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|