소스 검색

存储年级id为全局变量

丸子 9 달 전
부모
커밋
70c1c67c41
5개의 변경된 파일47개의 추가작업 그리고 6개의 파일을 삭제
  1. 30 0
      src/utils/globalState.js
  2. 4 1
      src/views/AIDevelop.vue
  3. 4 2
      src/views/AIPainting.vue
  4. 4 1
      src/views/AIQuestions.vue
  5. 5 2
      src/views/personalized/Personalized.vue

+ 30 - 0
src/utils/globalState.js

@@ -0,0 +1,30 @@
+// 全局状态管理
+
+// 初始状态
+const state = {
+  gradeId: ''
+}
+
+// 导出状态管理对象
+export const globalState = {
+  // 获取年级ID
+  getGradeId() {
+    return state.gradeId
+  },
+  
+  // 设置年级ID
+  setGradeId(id) {
+    state.gradeId = id
+    // 同时保存到localStorage,以便页面刷新后仍能保持状态
+    localStorage.setItem('selectedGradeId', id)
+  },
+  
+  // 初始化年级ID(从localStorage读取)
+  initGradeId() {
+    const id = localStorage.getItem('selectedGradeId')
+    if (id) {
+      state.gradeId = id
+    }
+    return state.gradeId
+  }
+}

+ 4 - 1
src/views/AIDevelop.vue

@@ -305,6 +305,9 @@ import { CreateDialogue, sendChatMessageStream } from '@/api/questions.js'
 import { teacherList } from '@/api/teachers.js'
 import MarkdownView from '@/components/MarkdownView/index.vue'
 
+// 导入全局状态
+import { globalState } from '@/utils/globalState.js'
+
 
 // 处理选择的默认消息
 const handleSelectMessage = message => {
@@ -1010,7 +1013,7 @@ const handleSendByKeydown = async event => {
 
 onMounted(() => {
   // 获取本地存储中的值
-  gradeId.value = localStorage.getItem('selectedGradeId') || ''
+  gradeId.value = globalState.initGradeId()
   typeId.value = localStorage.getItem('typeId') || ''
   // 视频相关初始化
   initVideoProgress()

+ 4 - 2
src/views/AIPainting.vue

@@ -180,7 +180,8 @@ import {
 
 import { saveRecord } from '@/api/personalized/index.js'
 
-
+// 导入全局状态
+import { globalState } from '@/utils/globalState.js'
 
 // 返回上一页
 const goBack = () => {
@@ -249,7 +250,8 @@ const demoImageList = [demo1, demo2, demo3, demo4]
 const gradeId = ref('')
 // 保存记录
 onMounted(async () => {
-  gradeId.value = localStorage.getItem('selectedGradeId') || ''
+    // 从全局状态初始化年级ID
+  gradeId.value = globalState.initGradeId()
   console.log(gradeId.value);
   try{
     const res = await saveRecord({

+ 4 - 1
src/views/AIQuestions.vue

@@ -84,6 +84,8 @@ import { ref, onMounted, computed,watch } from "vue";
 import { CreateDialogue, sendChatMessageStream } from "@/api/questions.js";
 import { useRouter, useRoute } from "vue-router";
 import { saveRecord } from '@/api/personalized/index.js'
+// 导入全局状态
+import { globalState } from '@/utils/globalState.js'
 
 
 import MarkdownView from "@/components/MarkdownView/index.vue";
@@ -260,7 +262,8 @@ const onCompositionend = () => {
 const gradeId = ref('')
 
 onMounted(async () => {
-  gradeId.value = localStorage.getItem('selectedGradeId') || ''
+   // 从全局状态初始化年级ID
+  gradeId.value = globalState.initGradeId()
   console.log(gradeId.value);
   try{
     const res = await saveRecord({

+ 5 - 2
src/views/personalized/Personalized.vue

@@ -75,6 +75,9 @@ import { ref,onMounted } from "vue";
 import { ArrowLeftBold } from '@element-plus/icons-vue'
 import { getReport } from '@/api/personalized/index.js'
 import { useRouter } from "vue-router";
+// 导入全局状态
+import { globalState } from '@/utils/globalState.js'
+
 const router = useRouter();
 
 // 返回首页
@@ -96,8 +99,8 @@ const comment = ref('')
 const gradeId = ref('')
 // 获取进度
 onMounted(async()=>{
-   // 从localStorage获取年级ID,如果没有使用默认值1
-  gradeId.value = localStorage.getItem('selectedGradeId') || ''
+    // 从全局状态初始化年级ID
+  gradeId.value = globalState.initGradeId()
   try {
     const res = await getReport({ brpNjId: gradeId.value });
     console.log(res);