Browse Source

金牌的错题和收藏的做题界面修改

JXDS18FUJT 2 years ago
parent
commit
34cc6b0b34

+ 40 - 7
src/api/modules/collectionAndWrong.ts

@@ -6,24 +6,30 @@ export class CollectionAndWrong {
     getList: string;
     add: string;
     deletes: string;
-    deleteAll:string;
+    deleteAll: string;
+    merge: string;
+    backups: string
   };
   constructor(type: CollectionAndWrongType.type) {
     switch (type) {
       case "collection":
         this.urlList = {
-          getList: "/student/question/collection/list",
+          getList: "/student/question/collection/collectionByUser",
           add: "/student/question/collection/collections",
           deletes: "/student/question/collection/",
-          deleteAll:"/student/question/collection/cancelAll"
+          deleteAll: "/student/question/collection/cancelAll",
+          merge: '/student/question/collection/collections',
+          backups: "/student/question/collection/bakCollections"
         };
         break;
       case "wrong":
         this.urlList = {
-          getList: "/student/question/wrong/list",
+          getList: "/student/question/wrong/wrongByUser",
           add: "/student/question/wrong/wrongs",
           deletes: "/student/question/wrong/",
-          deleteAll:"/student/question/wrong/cancelAll"
+          deleteAll: "/student/question/wrong/cancelAll",
+          merge: '/student/question/wrong/wrongs',
+          backups: "/student/question/wrong/bakWrongs"
         };
         break;
     }
@@ -38,7 +44,7 @@ export class CollectionAndWrong {
       url: this.urlList.getList,
       params,
     });
-    return <CollectionAndWrongType.ListRes>res.data;
+    return <CollectionAndWrongType.localListRes>res.data;
   };
 
   /**
@@ -67,7 +73,7 @@ export class CollectionAndWrong {
     });
     return <CollectionAndWrongType.AddCullectionsRes>res.data;
   };
-  deleteAll = async (data: CollectionAndWrongType.ListParams)=>{
+  deleteAll = async (data: CollectionAndWrongType.ListParams) => {
     const res = await request({
       url: this.urlList.deleteAll,
       method: "delete",
@@ -77,6 +83,33 @@ export class CollectionAndWrong {
 
 
   }
+  merge = async (data: {
+    wrongs?: { id: number, timestamp: number }[],
+    cols?: { id: number, timestamp: number }[],
+    km: number
+  }) => {
+    const res = await request({
+      url: this.urlList.merge,
+      method: "post",
+      data
+    });
+    return res.data;
+
+  }
+  backups = async (data: {
+    wrongs?: { id: number, timestamp: number }[],
+    cols?: { id: number, timestamp: number }[],
+    km: number
+  }) => {
+    const res = await request({
+      url: this.urlList.merge,
+      method: "post",
+      data
+    });
+    return res.data;
+  }
+
+
 }
 
 /**

+ 1 - 1
src/api/request.ts

@@ -3,7 +3,7 @@ import { requestLogger, responseLogger } from "axios-logger";
 import store from "@/store";
 import { Toast } from "vant";
 const request = axios.create({
-	baseURL: "/prod-api" ,
+	baseURL: import.meta.env.MODE === "production" ? "/prod-api" : "/dev-api",
 });
 
 request.interceptors.request.use((config) => {

+ 8 - 5
src/api/types/collectionAndWrong.d.ts

@@ -15,25 +15,28 @@ declare namespace CollectionAndWrongType {
     questionId: number;
   }
   interface listParamse extends Common.Paging{
-    carType: CarType;
-    km: Km;
+    km: number;
 
   }
 
   interface ListParams extends Common.Paging {
-    carType?: CarType;
-    km?: Km;
+    km: number;
   }
 
   interface QuestionRes extends Question {
     id: number;
     userId: number;
   }
-
+ //旧格式
   interface ListRes extends Common.Res {
     rows: QuestionRes[];
     total: number;
   }
+  //本地保存后改为新格式
+  interface localListRes extends Common.Res {
+    data: {id:number,timestamp:number}[];
+   
+  }
 
   type AddCullectionsParams = Question[];
 

+ 1 - 1
src/components/m-wrong-preview/index.vue

@@ -131,7 +131,7 @@ const isVip = store.getters.getIsVip;
 
 //答题模式选择逻辑
 const { answerTypeList, currentType, typeParams } = useTopicMode();
-const vehicle = useRouter().currentRoute.value.query.vehicle as string; //路由qvehicle参数
+const vehicle = useRouter().currentRoute.value.query.vehicle as string; //路由vehicle参数
 const subject = useRouter().currentRoute.value.query.subject as string; //路由subject参数
 //设置操作栏
 /**显示设置栏 */

+ 16 - 37
src/hooks/exercise/list.ts

@@ -21,8 +21,8 @@ export const useSubjectList = (type: ExerciseType.ListType) => {
 			});
 		}
 		else if (type == "localWrong") {
-			let localVuex = JSON.parse(window.localStorage.getItem('vuex')||'{}')
-			let tempUserWrongKey = localVuex.userData.openid+'_用户临时错题_'+subject
+			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 ? "" : []
@@ -34,41 +34,20 @@ export const useSubjectList = (type: ExerciseType.ListType) => {
 			} as TestModelListResType
 		}
 		else if (type == "wrong") {
-			const collectionModel = new CollectionModel(type);
-			const result = await collectionModel.getList({
-				carType: query.vehicle as CollectionAndWrongType.CarType,
-				km: query.name as CollectionAndWrongType.Km,
-				pageNum: 1,
-				pageSize: 50
-			});
-			if (result.rows.length == 0) {
-				return
-			}
-			result.rows.forEach(item => {
-				item.isTrue = null
-				if (typeof item.opts == "string") {
-					item.opts = item.opts.split("-")
-				}
-			})
-			let questionItem: Test.QuestionInfo = result.rows[0]
-			let questionIndex = 0
-			//排序
-			if (query.questionId) {
-				result.rows.forEach((item, index) => {
-					if (item.id == Number(query.questionId)) {
-						questionIndex = index
-						questionItem = item
-					}
-				})
-				result.rows.splice(questionIndex, 1)
-				result.rows.unshift(questionItem)
 
-			}
-			res = {
-				total: result.total,
-				list: result.rows
-			}
+			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) || "[]")
 
+			let ids = userWrongIds.map(item => {
+				return item.id
+			})
+			const result = 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)
@@ -119,10 +98,10 @@ export const useSubjectList = (type: ExerciseType.ListType) => {
 
 		subjectList.value = res.list;
 		subjectTotal.value = res.total;
-		
+
 	});
 
-	
+
 	//当前题目内容
 	const currentSubject = computed(() => {
 		console.log(subjectList.value[currentSubjectIndex.value])

+ 40 - 14
src/hooks/exercise/wrong.ts

@@ -32,20 +32,46 @@ export const useSubjectCheck = (currentSubject: ComputedRef<Test.QuestionInfo>,
 
 	/** 收藏当前题目 */
 	const addCurrentQuestion = async () => {
-		const res = await collectionModel.adds([
-			{
-				carType: query.vehicle as CollectionAndWrongType.CarType,
-				km: query.name as CollectionAndWrongType.Km,
-				questionId: currentSubject.value.id,
-			},
-		]);
-		if (res.data == 1) {
+		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) || "[]")
+		if (userCollectIds.some(item => {
+			return item.id == currentSubject.value.id
+
+		})) {
+			// 已经保存
+			Notify({ type: "success", message: "收藏成功" });
 			currentSubject.value.isCollection = true;
+		}
+		else{
+			userCollectIds.push({
+				id:currentSubject.value.id,
+				timestamp:+ new Date()
+			})
+			window.localStorage.setItem(userCollectKey,JSON.stringify(userCollectIds))
 			Notify({ type: "success", message: "收藏成功" });
-		} else {
 			currentSubject.value.isCollection = true;
-			Notify({ type: "primary", message: res.msg });
 		}
+
+		//目前改为本地保存形式,定期同步到服务器
+		// const res = await collectionModel.adds([
+		// 	{
+		// 		carType: query.vehicle as CollectionAndWrongType.CarType,
+		// 		km: query.name as CollectionAndWrongType.Km,
+		// 		questionId: currentSubject.value.id,
+		// 	},
+		// ]);
+		// if (res.data == 1) {
+		// 	currentSubject.value.isCollection = true;
+		// 	Notify({ type: "success", message: "收藏成功" });
+		// } else {
+		// 	currentSubject.value.isCollection = true;
+		// 	Notify({ type: "primary", message: res.msg });
+		// }
+
 	};
 	/**
 	 * 选择答案后进行校验
@@ -120,12 +146,12 @@ export const useSubjectCheck = (currentSubject: ComputedRef<Test.QuestionInfo>,
 
 
 			}
-			else{
+			else {
 				userWrongIds.push({
-					id:currentSubject.value.id,
-					timestamp:+ new Date()
+					id: currentSubject.value.id,
+					timestamp: + new Date()
 				})
-				window.localStorage.setItem(userWrongKey,JSON.stringify(userWrongIds))
+				window.localStorage.setItem(userWrongKey, JSON.stringify(userWrongIds))
 			}
 
 			// if(userWrongIds.some(item=>{

+ 24 - 0
src/model/test.ts

@@ -71,6 +71,30 @@ class TestModel {
 
 		return data;
 	}
+	async getBatchList(params: Test.QuestionInfoByIdsParams) {
+		const res = await test.getQuestionInfoByIds(
+			params
+		);
+
+		const data = {
+			total: res.data.total,
+			list: res.data.rows.map((item) => {
+				return {
+					...item,
+					explain: item.issue,
+					opts: item.opts.split("-").filter((item) => !!item),
+					image: item.image,
+					type: this.getType(item.answer.split("-")),
+					answer: item.answer,
+					userAnswer: this.getUserAnswer(this.getType(item.answer.split("-"))),
+					isTrue: null,
+					isCollection: false,
+				};
+			}),
+		};
+
+		return data;
+	}
 
 	/** 获取免费题目列表 */
 	async getFreeList(params: Test.listParams) {

+ 142 - 33
src/views/collection/index.vue

@@ -50,8 +50,12 @@
 						wrongSyncVisible = false;
 					}
 				"
-				
-				:columns="['合并本机和云端错题', '备份本机收藏到错题', '恢复云端收藏到错题']"></van-picker>
+				@confirm="sendWrongSync"
+				:columns="[
+					{ text: '合并本机和云端错题', value: 0 },
+					{ text: '备份本机收藏到错题', value: 1 },
+					{ text: '恢复云端收藏到错题', value: 2 },
+				]"></van-picker>
 		</van-popup>
 
 		<van-popup position="bottom" v-model:show="collectSyncVisible">
@@ -61,7 +65,12 @@
 						collectSyncVisible = false;
 					}
 				"
-				:columns="['合并本机和云端收藏', '备份本机收藏到云端', '恢复云端收藏到本机']"></van-picker>
+				@confirm="sendCollectSync"
+				:columns="[
+					{ text: '合并本机和云端收藏', value: 0 },
+					{ text: '备份本机收藏到云端', value: 1 },
+					{ text: '恢复云端收藏到本机', value: 2 },
+				]"></van-picker>
 		</van-popup>
 		<!-- <listCom type="wrong"></listCom> -->
 		<!-- <van-swipe ref="swiper" :show-indicators="false" :touchable="false">
@@ -86,27 +95,15 @@ import { Dialog } from "vant";
 import { RouterBus } from "@/hooks";
 const router = useRouter();
 const query = useRoute().query;
-const wrongList = ref<CollectionAndWrongType.Question[]>([]);
-const collectionList = ref<CollectionAndWrongType.Question[]>([]);
+const wrongList = ref<{ id: number; timestamp: number }[]>([]);
+const collectionList = ref<{ id: number; timestamp: number }[]>([]);
 onMounted(() => {
-	let wrongModel = new CollectionAndWrong("wrong");
-	wrongModel
-		.getList({
-			km: router.currentRoute.value.query.subject === "1" ? "科目一" : "科目四",
-			carType: router.currentRoute.value.query.vehicle as CollectionAndWrongType.CarType,
-		})
-		.then((res) => {
-			wrongList.value = res.rows;
-		});
-	let collectionModel = new CollectionAndWrong("collection");
-	collectionModel
-		.getList({
-			km: router.currentRoute.value.query.subject === "1" ? "科目一" : "科目四",
-			carType: router.currentRoute.value.query.vehicle as CollectionAndWrongType.CarType,
-		})
-		.then((res) => {
-			collectionList.value = res.rows;
-		});
+	let localVuex = JSON.parse(window.localStorage.getItem("vuex") || "{}");
+	let subject = query.subject;
+	let userWrongKey = localVuex.userData.openid + "_用户错题id_" + subject;
+	let userCollectKey = localVuex.userData.openid + "_用户收藏id_" + subject;
+	wrongList.value = JSON.parse(window.localStorage.getItem(userWrongKey) || "[]");
+	collectionList.value = JSON.parse(window.localStorage.getItem(userCollectKey) || "[]");
 });
 const onClickLeft = () => {
 	router.back();
@@ -126,6 +123,113 @@ const gsMap: {
 	kc: "客车",
 	mtc: "摩托车",
 };
+const sendWrongSync = (column: { text: string; value: number }) => {
+	let collectionModel = new CollectionAndWrong("wrong");
+	let km = Number(query.subject);
+	let localVuex = JSON.parse(window.localStorage.getItem("vuex") || "{}");
+	let subject = query.subject;
+	let userWrongKey = localVuex.userData.openid + "_用户错题id_" + subject;
+	switch (column.value) {
+		case 0:
+			collectionModel
+				.merge({
+					wrongs: wrongList.value,
+					km,
+				})
+				.then((res) => {
+					collectionModel
+						.getList({
+							km,
+						})
+						.then((res) => {
+							wrongList.value = res.data;
+							window.localStorage.setItem(userWrongKey, JSON.stringify(wrongList.value));
+						});
+				});
+			break;
+		case 1:
+			collectionModel
+				.backups({
+					wrongs: wrongList.value,
+					km,
+				})
+				.then((res) => {
+					collectionModel
+						.getList({
+							km,
+						})
+						.then((res) => {
+							wrongList.value = res.data;
+						});
+				});
+			break;
+		case 2:
+			collectionModel
+				.getList({
+					km,
+				})
+				.then((res) => {
+					wrongList.value = res.data;
+					window.localStorage.setItem(userWrongKey, JSON.stringify(wrongList.value));
+				});
+			break;
+	}
+	wrongSyncVisible.value = false;
+};
+const sendCollectSync = (column: { text: string; value: number }) => {
+	let collectionModel = new CollectionAndWrong("collection");
+	let km = Number(query.subject);
+	let localVuex = JSON.parse(window.localStorage.getItem("vuex") || "{}");
+	let subject = query.subject;
+	let userCollectKey = localVuex.userData.openid + "_用户收藏id_" + subject;
+
+	switch (column.value) {
+		case 0:
+			collectionModel
+				.merge({
+					cols: collectionList.value,
+					km,
+				})
+				.then((res) => {
+					collectionModel
+						.getList({
+							km,
+						})
+						.then((res) => {
+							collectionList.value = res.data;
+							window.localStorage.setItem(userCollectKey, JSON.stringify(collectionList.value));
+						});
+				});
+			break;
+		case 1:
+			collectionModel
+				.backups({
+					cols: collectionList.value,
+					km,
+				})
+				.then((res) => {
+					collectionModel
+						.getList({
+							km,
+						})
+						.then((res) => {
+							collectionList.value = res.data;
+						});
+				});
+			break;
+		case 2:
+			collectionModel
+				.getList({
+					km,
+				})
+				.then((res) => {
+					collectionList.value = res.data;
+					window.localStorage.setItem(userCollectKey, JSON.stringify(collectionList.value));
+				});
+			break;
+	}
+	collectSyncVisible.value = false;
+};
 const clearTopicsCollection = (e: PointerEvent) => {
 	e.preventDefault();
 	e.stopPropagation();
@@ -138,19 +242,21 @@ const clearTopicsCollection = (e: PointerEvent) => {
 			// on confirm
 			collectionModel
 				.deleteAll({
-					km: router.currentRoute.value.query.subject === "1" ? "科目一" : "科目四",
-					carType: gsMap[router.currentRoute.value.query.gs] as CollectionAndWrongType.CarType,
+					km: Number(router.currentRoute.value.query.subject),
 				})
 				.then((res) => {
 					Dialog.alert({ message: "清除成功" });
 					let collectionModel = new CollectionAndWrong("collection");
 					collectionModel
 						.getList({
-							km: router.currentRoute.value.query.subject === "1" ? "科目一" : "科目四",
-							carType: router.currentRoute.value.query.vehicle as CollectionAndWrongType.CarType,
+							km: Number(router.currentRoute.value.query.subject),
 						})
 						.then((res) => {
-							collectionList.value = res.rows;
+							let localVuex = JSON.parse(window.localStorage.getItem("vuex") || "{}");
+							let subject = query.subject;
+							let userCollectKey = localVuex.userData.openid + "_用户收藏id_" + subject;
+							window.localStorage.setItem(userCollectKey, "[]");
+							collectionList.value = [];
 						});
 				});
 		})
@@ -170,19 +276,22 @@ const clearTopicsWrong = (e: PointerEvent) => {
 			// on confirm
 			collectionModel
 				.deleteAll({
-					km: router.currentRoute.value.query.subject === "1" ? "科目一" : "科目四",
-					carType: router.currentRoute.value.query.vehicle as CollectionAndWrongType.CarType,
+					km: Number(router.currentRoute.value.query.subject),
 				})
 				.then((res) => {
 					Dialog.alert({ message: "清除成功" });
 					let wrongModel = new CollectionAndWrong("wrong");
+
 					wrongModel
 						.getList({
-							km: router.currentRoute.value.query.subject === "1" ? "科目一" : "科目四",
-							carType: router.currentRoute.value.query.vehicle as CollectionAndWrongType.CarType,
+							km: Number(router.currentRoute.value.query.subject),
 						})
 						.then((res) => {
-							wrongList.value = res.rows;
+							let localVuex = JSON.parse(window.localStorage.getItem("vuex") || "{}");
+							let subject = query.subject;
+							let userWrongKey = localVuex.userData.openid + "_用户错题id_" + subject;
+							window.localStorage.setItem(userWrongKey, "[]");
+							wrongList.value = [];
 						});
 				});
 		})

+ 1 - 1
src/views/mockTest/components/startTest.vue

@@ -79,7 +79,7 @@
 	<!-- 功能选择列表 -->
 	<div class="function-list">
 		<div class="function-item">
-			<m-icon type="shoucanghui" size="25px" />
+			<m-icon :type="currentSubject?.isCollection?'shoucanghuang' : 'shoucanghui'" size="25px" />
 			<span>收藏</span>
 		</div>
 		<!-- <div class="function-item" @click="answerAudioPlay">

+ 1 - 1
vite.config.ts

@@ -30,7 +30,7 @@ export default defineConfig({
 		},
 		proxy: {
 			"/dev-api": {
-				target: "https://jpcj-admin1.zzxcx.net/jpcj-admin",
+				target: "https://jpcj1.zzxcx.net/jpcj-admin",
 				changeOrigin: true,
 				secure: false,
 				rewrite: (path) => path.replace(/^\/dev-api/, ""),