50f1bdba38
- toggleFavoriteScript: POST /epicScript/favorite/toggle - getFavoriteScriptPage: GET /epicScript/favorite/page - checkFavoriteScript: GET /epicScript/favorite/check - Added to default export object
253 lines
7.1 KiB
JavaScript
253 lines
7.1 KiB
JavaScript
import { get, post, put, del } from './request.js'
|
||
|
||
export const getScriptList = async () => {
|
||
return get('/epicScript/listAll')
|
||
}
|
||
|
||
export const getScriptPage = async ({ pageNum = 1, pageSize = 10 }) => {
|
||
return get('/epicScript/page', { pageNum, pageSize })
|
||
}
|
||
|
||
export const getScriptById = async (id) => {
|
||
return get('/epicScript/detail', { id })
|
||
}
|
||
|
||
export const getInspirationRecommendations = async () => {
|
||
return get('/epicScript/inspiration/recommendations')
|
||
}
|
||
|
||
export const getRandomInspirations = async (size = 3) => {
|
||
return get('/epicScript/inspiration/random', { size })
|
||
}
|
||
|
||
export const generateFromInspiration = async (payload) => {
|
||
return post('/epicScript/inspiration/generate', payload)
|
||
}
|
||
|
||
export const createScript = async (scriptData) => {
|
||
return post('/epicScript/create', transformToBackendFormat(scriptData))
|
||
}
|
||
|
||
export const updateScript = async (scriptData) => {
|
||
return put('/epicScript/update', transformToBackendFormat(scriptData))
|
||
}
|
||
|
||
export const selectScript = async (id) => {
|
||
return put('/epicScript/select', { id })
|
||
}
|
||
|
||
export const deleteScript = async (id) => {
|
||
return del('/epicScript/delete', { id })
|
||
}
|
||
|
||
const textOrFallback = (value, fallback) => {
|
||
return value && String(value).trim() ? String(value).trim() : fallback
|
||
}
|
||
|
||
export const buildCharacterInfo = (profile = {}) => {
|
||
const hobbies = Array.isArray(profile.hobbies) && profile.hobbies.length
|
||
? profile.hobbies.join('、')
|
||
: '未设置'
|
||
|
||
const parts = [
|
||
`昵称:${textOrFallback(profile.nickname, '未设置')}`,
|
||
`性别:${textOrFallback(profile.gender, '未设置')}`,
|
||
`MBTI:${textOrFallback(profile.mbti, '未设置')}`,
|
||
`星座:${textOrFallback(profile.zodiac, '未设置')}`,
|
||
`职业:${textOrFallback(profile.profession, '未设置')}`,
|
||
`兴趣:${hobbies}`
|
||
]
|
||
|
||
if (profile.future?.vision) parts.push(`未来愿景:${profile.future.vision}`)
|
||
if (profile.future?.ideal) parts.push(`理想生活:${profile.future.ideal}`)
|
||
return parts.join('\n')
|
||
}
|
||
|
||
export const buildLifeEventsSummary = (events = [], profile = {}) => {
|
||
const parts = []
|
||
const addMemory = (label, memory) => {
|
||
if (memory?.text) {
|
||
parts.push(`【${label}】${memory.date || '未知日期'}:${memory.text}`)
|
||
}
|
||
}
|
||
|
||
addMemory('童年记忆', profile.childhood)
|
||
addMemory('高光时刻', profile.joy)
|
||
addMemory('低谷经历', profile.low)
|
||
|
||
events.forEach((event) => {
|
||
const tag = Array.isArray(event.tags) && event.tags.length ? ` #${event.tags.join(' #')}` : ''
|
||
parts.push(`【人生事件】${event.time || event.date || '未知日期'} ${event.title || '未命名'}${tag}:${event.content || event.description || ''}`)
|
||
})
|
||
|
||
return parts.join('\n')
|
||
}
|
||
|
||
const transformToBackendFormat = (frontendData) => {
|
||
const {
|
||
id,
|
||
title,
|
||
theme,
|
||
style,
|
||
length,
|
||
content,
|
||
isSelected,
|
||
character,
|
||
events,
|
||
plotJson,
|
||
conversationId,
|
||
parentScriptId,
|
||
revisionIndex,
|
||
revisionOf,
|
||
useSocialInsights
|
||
} = frontendData
|
||
|
||
const scriptTitle = title || theme || '我的人生剧本'
|
||
const characterInfo = frontendData.characterInfo || buildCharacterInfo(character || {})
|
||
const lifeEventsSummary = frontendData.lifeEventsSummary || buildLifeEventsSummary(events || [], character || {})
|
||
|
||
return {
|
||
id,
|
||
title: scriptTitle,
|
||
theme,
|
||
style,
|
||
length,
|
||
plotIntro: frontendData.plotIntro || '',
|
||
plotTurning: frontendData.plotTurning || '',
|
||
plotClimax: frontendData.plotClimax || '',
|
||
plotEnding: frontendData.plotEnding || '',
|
||
plotJson: {
|
||
...(content ? { fullContent: content } : {}),
|
||
...(plotJson || {}),
|
||
...(conversationId ? { conversationId } : {}),
|
||
...(parentScriptId ? { parentScriptId } : {}),
|
||
...(revisionIndex ? { revisionIndex } : {}),
|
||
...(revisionOf ? { revisionOf } : {})
|
||
},
|
||
isSelected,
|
||
characterInfo,
|
||
lifeEventsSummary,
|
||
useSocialInsights
|
||
}
|
||
}
|
||
|
||
export const transformToFrontendFormat = (backendData) => {
|
||
if (!backendData) return null
|
||
|
||
const {
|
||
id,
|
||
userId,
|
||
title,
|
||
theme,
|
||
style,
|
||
length,
|
||
plotIntro,
|
||
plotTurning,
|
||
plotClimax,
|
||
plotEnding,
|
||
plotJson,
|
||
isSelected,
|
||
createTime,
|
||
updateTime,
|
||
conversationId,
|
||
currentVersionMessageId
|
||
} = backendData
|
||
|
||
let content = ''
|
||
if (plotJson?.fullContent) {
|
||
content = plotJson.fullContent
|
||
} else {
|
||
const parts = []
|
||
if (plotIntro) parts.push(`【序幕:低谷回响】\n${plotIntro}`)
|
||
if (plotTurning) parts.push(`【转折:契机出现】\n${plotTurning}`)
|
||
if (plotClimax) parts.push(`【高潮:命运抉择】\n${plotClimax}`)
|
||
if (plotEnding) parts.push(`【结局:新的开始】\n${plotEnding}`)
|
||
content = parts.join('\n\n')
|
||
}
|
||
|
||
const plainContent = content.replace(/[#>*_`-]/g, '').trim()
|
||
const summary = plainContent ? plainContent.slice(0, 90) : (theme || '一段正在生成的人生爽文。')
|
||
|
||
return {
|
||
id,
|
||
userId,
|
||
title: title || theme || '未命名剧本',
|
||
theme: theme || '',
|
||
style: style || '',
|
||
length: length || 'medium',
|
||
content,
|
||
summary,
|
||
plotJson,
|
||
isSelected: Boolean(isSelected),
|
||
createTime,
|
||
updateTime,
|
||
date: createTime ? String(createTime).slice(0, 10) : new Date().toLocaleDateString(),
|
||
mode: plotJson?.mode || 'custom',
|
||
prompt: plotJson?.prompt || '',
|
||
conversationId: conversationId || plotJson?.conversationId || '',
|
||
parentScriptId: plotJson?.parentScriptId || '',
|
||
revisionIndex: plotJson?.currentRevisionIndex || plotJson?.revisionIndex || 0,
|
||
revisionOf: plotJson?.revisionOf || '',
|
||
wordCount: content ? content.length : 0,
|
||
currentVersionMessageId: currentVersionMessageId || ''
|
||
}
|
||
}
|
||
|
||
export const transformListToFrontend = (backendList) => {
|
||
if (!Array.isArray(backendList)) return []
|
||
return backendList.map(transformToFrontendFormat)
|
||
}
|
||
|
||
/**
|
||
* 切换剧本收藏状态(收藏/取消收藏)
|
||
* @param {string} scriptId 剧本ID
|
||
* @returns {Promise<{success: boolean, isFavorited: boolean}>}
|
||
*/
|
||
export const toggleFavoriteScript = async (scriptId) => {
|
||
const res = await post('/epicScript/favorite/toggle', { scriptId })
|
||
return {
|
||
success: true,
|
||
isFavorited: res.data?.isFavorited ?? false,
|
||
favoriteTime: res.data?.favoriteTime ?? null
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取收藏剧本列表
|
||
* @param {number} current 当前页
|
||
* @param {number} size 每页大小
|
||
*/
|
||
export const getFavoriteScriptPage = async (current = 1, size = 10) => {
|
||
return get('/epicScript/favorite/page', { current, size })
|
||
}
|
||
|
||
/**
|
||
* 检查剧本是否已收藏
|
||
* @param {string} scriptId 剧本ID
|
||
* @returns {Promise<boolean>}
|
||
*/
|
||
export const checkFavoriteScript = async (scriptId) => {
|
||
const res = await get('/epicScript/favorite/check', { scriptId })
|
||
return res.data?.isFavorited ?? false
|
||
}
|
||
|
||
export default {
|
||
getScriptList,
|
||
getScriptPage,
|
||
getScriptById,
|
||
getInspirationRecommendations,
|
||
getRandomInspirations,
|
||
generateFromInspiration,
|
||
createScript,
|
||
updateScript,
|
||
selectScript,
|
||
deleteScript,
|
||
buildCharacterInfo,
|
||
buildLifeEventsSummary,
|
||
transformToFrontendFormat,
|
||
transformListToFrontend,
|
||
toggleFavoriteScript,
|
||
getFavoriteScriptPage,
|
||
checkFavoriteScript,
|
||
}
|