import { ref, onBeforeMount, computed } from "vue"; import { useRoute, useRouter } from "vue-router"; import testModel from "@/model/test"; import { CollectionModel } from "@/model/collection"; /**获取题目列表 */ export const useSubjectList = (type: ExerciseType.ListType) => { type TestModelListResType = Awaited>; const subjectList = ref([]); //题目列表 const subjectTotal = ref(0); //题目总数 const query = useRoute().query; //路由query参数 const currentSubjectIndex = ref(0); //当前题目下标 const vehicle = useRouter().currentRoute.value.query.vehicle as string; //路由qvehicle参数 const subject = useRouter().currentRoute.value.query.subject as string; //路由subject参数 onBeforeMount(async () => { let res: TestModelListResType; if (type == "normal" || type == "test") { res = await testModel.getList({ ...query, }); } else if (type == "localWrong") { let localVuex = JSON.parse(window.localStorage.getItem('vuex') || '{}') let tempUserWrongKey = localVuex.userData.openid + '_用户临时错题_' + subject let wrongSet: [any] = JSON.parse(window.sessionStorage.getItem(tempUserWrongKey) || "[]") wrongSet.forEach(item => { item.userAnswer = item.questionType !== 3 ? "" : [] item.isTrue = null }) res = { total: wrongSet.length, list: wrongSet } as TestModelListResType } else if (type == "wrong") { let localVuex = JSON.parse(window.localStorage.getItem('vuex') || '{}') let userWrongKey = localVuex.userData.openid + "_用户错题id_" + subject; let userWrongIds: { id: number, timestamp: number }[] = JSON.parse(window.localStorage.getItem(userWrongKey) || "[]") userWrongIds.sort((a,b)=>b.timestamp-a.timestamp) let ids = userWrongIds.map(item => { return item.id }) testModel.getBatchList({ ids: ids }).then(res => { subjectList.value = res.list; subjectTotal.value = res.total; }) // const collectionList.value.push(...result.collectionList) // const questionList.value.push(...result.rows) } else if (type == "collection") { let localVuex = JSON.parse(window.localStorage.getItem('vuex') || '{}') let userCollectKey = localVuex.userData.openid + "_用户收藏id_" + subject; let userCollectIds: { id: number, timestamp: number }[] = JSON.parse(window.localStorage.getItem(userCollectKey) || "[]") userCollectIds.sort((a,b)=>b.timestamp-a.timestamp) let ids = userCollectIds.map(item => { return item.id }) testModel.getBatchList({ ids: ids }).then(res => { subjectList.value = res.list; subjectTotal.value = res.total; }) } else { res = await testModel.getFreeList({ ...query, }); } subjectList.value = res.list; subjectTotal.value = res.total; }); //当前题目内容 const currentSubject = computed(() => { console.log(subjectList.value[currentSubjectIndex.value]) return subjectList.value[currentSubjectIndex.value]; }); return { subjectList, subjectTotal, currentSubject, currentSubjectIndex, }; };