feat: 功能优化
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { ref } from 'vue'
|
||||
import { ref, computed, watch } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import localforage from 'localforage'
|
||||
import { UserPromptApi } from '@/api/userPrompt'
|
||||
@@ -10,11 +10,14 @@ export const usePromptStore = defineStore('prompt', () => {
|
||||
// 存储提示词相关的视频信息
|
||||
const currentVideoInfo = ref(null)
|
||||
|
||||
// 存储提示词列表
|
||||
// 存储提示词列表(自建 + 收藏)
|
||||
const promptList = ref([])
|
||||
const promptListLoading = ref(false)
|
||||
const promptListError = ref(null)
|
||||
|
||||
// 缓存标记:记录是否已加载过
|
||||
const hasLoaded = ref(false)
|
||||
|
||||
// 设置提示词
|
||||
function setPrompt(prompt, videoInfo = null) {
|
||||
currentPrompt.value = prompt
|
||||
@@ -27,15 +30,29 @@ export const usePromptStore = defineStore('prompt', () => {
|
||||
currentVideoInfo.value = null
|
||||
}
|
||||
|
||||
// 加载提示词列表
|
||||
async function loadPromptList(userId) {
|
||||
if (!userId) {
|
||||
console.warn('用户未登录,无法加载提示词')
|
||||
return
|
||||
/**
|
||||
* 加载用户可用提示词列表(自建 + 收藏的智能体)
|
||||
* @param {Object} options
|
||||
* @param {boolean} options.force - 是否强制刷新
|
||||
*/
|
||||
async function loadPromptList(options = {}) {
|
||||
const { force = false } = options
|
||||
|
||||
// 如果已有数据且不强制刷新,直接返回缓存
|
||||
if (hasLoaded.value && !force && promptList.value.length > 0) {
|
||||
return promptList.value
|
||||
}
|
||||
|
||||
// 如果已有数据且不在加载中,直接返回缓存数据
|
||||
if (promptList.value.length > 0 && !promptListLoading.value) {
|
||||
// 防止重复请求
|
||||
if (promptListLoading.value) {
|
||||
await new Promise(resolve => {
|
||||
const unwatch = watch(promptListLoading, (loading) => {
|
||||
if (!loading) {
|
||||
unwatch()
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
return promptList.value
|
||||
}
|
||||
|
||||
@@ -43,17 +60,16 @@ export const usePromptStore = defineStore('prompt', () => {
|
||||
promptListError.value = null
|
||||
|
||||
try {
|
||||
const response = await UserPromptApi.getUserPromptPage({
|
||||
pageNo: 1,
|
||||
pageSize: 100,
|
||||
status: undefined
|
||||
})
|
||||
// 调用新接口:获取自建 + 收藏的提示词
|
||||
const response = await UserPromptApi.getMyPromptList()
|
||||
|
||||
if (response?.data?.list) {
|
||||
promptList.value = response.data.list
|
||||
if (response?.data) {
|
||||
promptList.value = response.data
|
||||
} else {
|
||||
promptList.value = []
|
||||
}
|
||||
|
||||
hasLoaded.value = true
|
||||
return promptList.value
|
||||
} catch (error) {
|
||||
console.error('加载提示词列表失败:', error)
|
||||
@@ -68,10 +84,8 @@ export const usePromptStore = defineStore('prompt', () => {
|
||||
function addPromptToList(prompt) {
|
||||
const existingIndex = promptList.value.findIndex(p => p.id === prompt.id)
|
||||
if (existingIndex >= 0) {
|
||||
// 更新已存在的提示词
|
||||
promptList.value[existingIndex] = prompt
|
||||
} else {
|
||||
// 添加新提示词
|
||||
promptList.value.unshift(prompt)
|
||||
}
|
||||
}
|
||||
@@ -92,25 +106,35 @@ export const usePromptStore = defineStore('prompt', () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新提示词列表
|
||||
async function refreshPromptList(userId) {
|
||||
promptList.value = [] // 清空缓存,强制重新加载
|
||||
return await loadPromptList(userId)
|
||||
// 刷新提示词列表(强制重新加载)
|
||||
async function refreshPromptList() {
|
||||
hasLoaded.value = false
|
||||
return await loadPromptList({ force: true })
|
||||
}
|
||||
|
||||
// 根据ID获取提示词
|
||||
function getPromptById(id) {
|
||||
return promptList.value.find(p => p.id === id)
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
currentPrompt,
|
||||
currentVideoInfo,
|
||||
promptList,
|
||||
promptListLoading,
|
||||
promptListError,
|
||||
hasLoaded,
|
||||
|
||||
// Actions
|
||||
setPrompt,
|
||||
clearPrompt,
|
||||
loadPromptList,
|
||||
addPromptToList,
|
||||
removePromptFromList,
|
||||
updatePromptInList,
|
||||
refreshPromptList
|
||||
refreshPromptList,
|
||||
getPromptById
|
||||
}
|
||||
}, {
|
||||
persist: {
|
||||
|
||||
Reference in New Issue
Block a user