exam.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { defineStore } from 'pinia'
  2. import { ref, computed } from 'vue'
  3. // 定义考试信息的类型接口
  4. interface ExamInfo {
  5. id: string | number // 根据实际业务调整 id 类型
  6. examSubjectName: string,
  7. examSubjectCode: string,
  8. [key: string]: any // 允许其他未知属性
  9. }
  10. //考试相关的状态管理
  11. export const useExamStore = defineStore('exam', () => {
  12. //初始化时尝试从localStorage中获取考试信息
  13. const storedExam = localStorage.getItem('current_exam_info')
  14. const initialExam = storedExam ? JSON.parse(storedExam) : null
  15. //显式指定泛型类型为 ExamInfo | null,解决类型推断为 never 的问题
  16. const currentExam = ref<ExamInfo | null>(initialExam)
  17. //计算属性 考试id
  18. const examId = computed(() => currentExam.value?.id)
  19. //计算属性 考试名称
  20. const examName = computed(() => currentExam.value?.examSubjectName)
  21. //计算属性 考试类型
  22. const examType = computed(() => currentExam.value?.examType || 1) //考试类型 1 选择题判分 2 填空题判分 3 作文题判分
  23. //设置考试信息
  24. const setExamInfo = (info: any) => {
  25. currentExam.value = info
  26. // 同步更新 localStorage,保证刷新后数据不丢失
  27. if (info) {
  28. localStorage.setItem('current_exam_info', JSON.stringify(info))
  29. } else {
  30. localStorage.removeItem('current_exam_info')
  31. }
  32. }
  33. const unScanned=ref(0);//未扫描
  34. const examMissNum=ref(0);//缺考人数
  35. const abnormalNum=ref(0);//异常人数
  36. const scannedNum=ref(0);//已上传人数
  37. const setExamInfoCount = (info: any) => {
  38. unScanned.value = info.unScanned;
  39. examMissNum.value = info.examMissNum;
  40. abnormalNum.value = info.abnormalNum;
  41. scannedNum.value = info.scannedNum;
  42. localStorage.setItem('current_exam_info_count', JSON.stringify(info));//当前考试异常数据信息
  43. }
  44. // 清空考试信息
  45. const clearExamInfo = () => {
  46. currentExam.value = null
  47. localStorage.removeItem('current_exam_info')
  48. }
  49. return {
  50. currentExam,
  51. examId,
  52. examName,
  53. examType,
  54. setExamInfo,
  55. clearExamInfo,
  56. setExamInfoCount,
  57. unScanned,
  58. examMissNum,
  59. abnormalNum,
  60. scannedNum
  61. }
  62. })