Ver Fonte

优化AI生成课缓存逻辑:新增修改存储缓存,编辑模式退出时清空缓存数据,新增模式退出时保留缓存,缓存数据跟查看草稿按钮联动

liyanbo há 1 mês atrás
pai
commit
7d7dd651e0
1 ficheiros alterados com 21 adições e 5 exclusões
  1. 21 5
      src/views/bjdx/course/aiGenerate/aiGengrate.vue

+ 21 - 5
src/views/bjdx/course/aiGenerate/aiGengrate.vue

@@ -75,7 +75,7 @@
               <button
                 v-if="hasDraftCache"
                 class="generate-btn secondary"
-                @click="currentStep = 2"
+                @click="loadDraftAndGotoStep2"
               >
                 查看草稿
               </button>
@@ -510,8 +510,6 @@ const userId = computed(() => userStore.getUser.id)
 
 // 检查是否存在草稿缓存
 const hasDraftCache = computed(() => {
-  // 依赖scriptData,当scriptData变化时重新计算
-  Object.keys(scriptData)
   try {
     const key = `courseScriptData_${userId.value}`
     const cachedData = localStorage.getItem(key)
@@ -552,6 +550,9 @@ const loadScriptDataFromCache = () => {
   return null
 }
 
+// 是否为编辑模式
+const isEditMode = ref(false)
+
 // 脚本数据结构
 const scriptData = reactive(loadScriptDataFromCache() || {
   title: '',
@@ -590,6 +591,7 @@ const saveScriptDataToCache = () => {
   try {
     const key = `courseScriptData_${userId.value}`
     localStorage.setItem(key, JSON.stringify(scriptData))
+    console.log("保存脚本数据到缓存****", key)
   } catch (error) {
     console.error('保存缓存数据失败:', error)
   }
@@ -599,6 +601,16 @@ const saveScriptDataToCache = () => {
 const clearScriptDataCache = () => {
   const key = `courseScriptData_${userId.value}`
   localStorage.removeItem(key)
+  console.log("清除脚本数据缓存****", key)
+}
+
+// 加载草稿并跳转到步骤2
+const loadDraftAndGotoStep2 = () => {
+  const cachedData = loadScriptDataFromCache()
+  if (cachedData) {
+    Object.assign(scriptData, cachedData)
+    currentStep.value = 2
+  }
 }
 
 // 监听脚本数据变化,自动保存到缓存
@@ -613,11 +625,11 @@ watch(() => props.visible, (newVisible, oldVisible) => {
     currentStep.value = props.initialStep
     // 处理脚本数据
     if (props.initialScriptData) {
+      // 标记为编辑模式
+      isEditMode.value = true
       try {
         const parsedData = JSON.parse(props.initialScriptData)
         console.log("草稿json数据:",parsedData)
-        // 清空缓存
-        clearScriptDataCache()
         Object.assign(scriptData, parsedData)
       } catch (error) {
         console.error('解析脚本数据失败:', error)
@@ -628,6 +640,8 @@ watch(() => props.visible, (newVisible, oldVisible) => {
         }
       }
     } else {
+      // 标记为新建模式
+      isEditMode.value = false
       // 没有脚本数据,尝试加载草稿
       const cachedData = loadScriptDataFromCache()
       if (cachedData) {
@@ -641,6 +655,8 @@ watch(() => props.visible, (newVisible, oldVisible) => {
       // 编辑进来的,清空缓存
       clearScriptDataCache()
     }
+    // 重置编辑模式标记
+    isEditMode.value = false
     // 新增进来的,保留缓存
   }
 })