12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import { useCheckObjectiveStore } from "@/store";
- import { saveStudentObjectiveConfirmData } from "@/api/checkPage";
- import { message } from "ant-design-vue";
- import { useStudent } from "../useStudent";
- export default function useAnswer() {
- const checkObjectiveStore = useCheckObjectiveStore();
- const { updateCurStudent, getNextStudent } = useStudent();
- // 修改答案
- const allowKey = [
- "Delete",
- "Backspace",
- "ArrowLeft",
- "ArrowRight",
- "#",
- "Shift",
- "[A-Za-z]",
- ];
- const allowKeyRef = new RegExp(allowKey.join("|"));
- function onPreventAnswerKey(e: KeyboardEvent) {
- if (!allowKeyRef.test(e.key)) {
- e.preventDefault();
- }
- }
- function changeAnswer(event: Event, question: string, defaultValue?: string) {
- const target = event.target as HTMLInputElement;
- checkObjectiveStore.student.answers =
- checkObjectiveStore.student.answers.map((v) => {
- if (
- v.mainNumber === question.mainNumber &&
- v.subNumber === question.subNumber
- ) {
- v.answer = target?.value.toUpperCase().trim() || defaultValue || "";
- }
- return v;
- });
- }
- // 保存答案
- let loading = false;
- async function saveStudentAnswer() {
- if (!checkObjectiveStore.student) return;
- if (loading) return;
- loading = true;
- const data = {
- studentId: checkObjectiveStore.student.studentId,
- answers: checkObjectiveStore.student.answers
- .map((v) => v.answer || "#")
- .join(","),
- };
- // if (!answers.match(/^(#*,*[A-Z]*)+$/g)) {
- // void message.error("答案只能是#和大写英文字母");
- // return;
- // }
- const res = await saveStudentObjectiveConfirmData(data).catch(() => false);
- loading = false;
- if (!res) {
- void message.error("保存失败,请刷新页面。");
- } else {
- void message.success("保存成功");
- if (!checkObjectiveStore.isMultiStudent) {
- window.close();
- return;
- }
- if (checkObjectiveStore.isLast) {
- await updateCurStudent();
- } else {
- await getNextStudent();
- }
- }
- }
- return {
- onPreventAnswerKey,
- changeAnswer,
- saveStudentAnswer,
- };
- }
|