Mark.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <template>
  2. <div class="my-container">
  3. <mark-header />
  4. <div class="tw-flex tw-gap-1">
  5. <mark-history showSearch showOrder :getHistory="getHistoryTask" />
  6. <mark-body @error="removeBrokenTask" />
  7. <mark-board-track
  8. v-if="store.isTrackMode"
  9. @submit="saveTaskToServer"
  10. @allZeroSubmit="allZeroSubmit"
  11. @unselectiveSubmit="unselectiveSubmit"
  12. />
  13. <mark-board-key-board
  14. v-if="store.shouldShowMarkBoardKeyBoard"
  15. @submit="saveTaskToServer"
  16. @allZeroSubmit="allZeroSubmit"
  17. @unselectiveSubmit="unselectiveSubmit"
  18. />
  19. <mark-board-mouse
  20. v-if="store.shouldShowMarkBoardMouse"
  21. @submit="saveTaskToServer"
  22. @allZeroSubmit="allZeroSubmit"
  23. @unselectiveSubmit="unselectiveSubmit"
  24. />
  25. </div>
  26. </div>
  27. <AnswerModal />
  28. <PaperModal />
  29. <MinimapModal />
  30. <AllPaperModal />
  31. <SheetViewModal />
  32. <SpecialTagModal />
  33. </template>
  34. <script setup lang="ts">
  35. import { onMounted, watch, h } from "vue";
  36. import {
  37. clearMarkTask,
  38. getGroup,
  39. getSetting,
  40. getStatus,
  41. getTask,
  42. saveTask,
  43. updateUISetting,
  44. doUnselectiveType,
  45. } from "@/api/markPage";
  46. import { store } from "@/store/store";
  47. import MarkHeader from "./MarkHeader.vue";
  48. import MarkBody from "./MarkBody.vue";
  49. import { useTimers } from "@/setups/useTimers";
  50. import MarkHistory from "./MarkHistory.vue";
  51. import MarkBoardTrack from "./MarkBoardTrack.vue";
  52. import { Question } from "@/types";
  53. import type { Setting } from "@/types";
  54. import MarkBoardKeyBoard from "./MarkBoardKeyBoard.vue";
  55. import MarkBoardMouse from "./MarkBoardMouse.vue";
  56. import { debounce, isEmpty, isNumber } from "lodash-es";
  57. import { message } from "ant-design-vue";
  58. import AnswerModal from "./AnswerModal.vue";
  59. import PaperModal from "./PaperModal.vue";
  60. import MinimapModal from "./MinimapModal.vue";
  61. import AllPaperModal from "./AllPaperModal.vue";
  62. import SheetViewModal from "./SheetViewModal.vue";
  63. import SpecialTagModal from "./SpecialTagModal.vue";
  64. import { addFileServerPrefixToTask, preDrawImage } from "@/utils/utils";
  65. import { getPaper } from "@/api/jsonMark";
  66. import EventBus from "@/plugins/eventBus";
  67. import { getHistoryTask } from "@/api/markPage";
  68. const { addInterval } = useTimers();
  69. async function updateMarkTask() {
  70. await clearMarkTask();
  71. }
  72. async function updateSetting() {
  73. const settingRes = await getSetting();
  74. // 初次使用时,重置并初始化uisetting
  75. if (isEmpty(settingRes.data.uiSetting)) {
  76. settingRes.data.uiSetting = {
  77. "answer.paper.scale": 1,
  78. "score.board.collapse": false,
  79. "normal.mode": "keyboard",
  80. "score.fontSize.scale": 1,
  81. } as Setting["uiSetting"];
  82. }
  83. store.setting = settingRes.data;
  84. if (store.setting.subject?.answerUrl) {
  85. store.setting.subject.answerUrl =
  86. store.setting.fileServer + store.setting.subject?.answerUrl;
  87. }
  88. if (store.setting.subject?.paperUrl) {
  89. store.setting.subject.paperUrl =
  90. store.setting.fileServer + store.setting.subject?.paperUrl;
  91. }
  92. if (store.setting.subject?.paperUrl && store.isMultiMedia) {
  93. await getPaper(store);
  94. }
  95. }
  96. async function updateStatus() {
  97. const res = await getStatus();
  98. if (res.data.valid) Object.assign(store.status, res.data);
  99. }
  100. async function updateGroups() {
  101. const res = await getGroup();
  102. store.groups = res.data;
  103. }
  104. let preDrawing = false;
  105. async function updateTask() {
  106. const res = await getTask();
  107. if (res.data.libraryId) {
  108. let rawTask = res.data;
  109. const newTask = addFileServerPrefixToTask(rawTask);
  110. store.tasks.push(newTask);
  111. if (!store.historyOpen) {
  112. // 在正评中,才能替换task
  113. if (store.currentTask?.studentId !== store.tasks[0].studentId)
  114. store.currentTask = store.tasks[0];
  115. }
  116. // 如果是评完后,再取到的任务,则此时要更新一下status
  117. if (store.status.totalCount - store.status.markedCount === 0) {
  118. await updateStatus();
  119. }
  120. // prefetch sliceUrls image
  121. if (store.tasks.length > 1) {
  122. // 如果不是当前任务,则先等3秒再去取任务,以免和其他请求争夺网络资源
  123. await new Promise((resolve) => setTimeout(resolve, 3000));
  124. }
  125. try {
  126. preDrawing = true;
  127. if (store.isScanImage) {
  128. await preDrawImage(newTask);
  129. }
  130. } finally {
  131. preDrawing = false;
  132. }
  133. } else {
  134. store.message = res.data.message;
  135. }
  136. }
  137. // 5秒更新一次tasks
  138. addInterval(() => {
  139. // console.log("get task", store.tasks);
  140. // 正在预绘制中,则不要再取任务,以免拖慢当前任务的绘制
  141. if (!preDrawing) {
  142. if (store.tasks.length < (store.setting.prefetchCount ?? 3)) {
  143. // 回看打开时,停止取任务
  144. if (!store.historyOpen)
  145. updateTask().catch((e) => console.log("定时获取任务出错", e));
  146. }
  147. }
  148. }, 5 * 1000);
  149. // 不需要
  150. // addInterval(() => {
  151. // updateStatus();
  152. // }, 5 * 60 * 1000);
  153. onMounted(async () => {
  154. await updateMarkTask();
  155. await updateSetting();
  156. await updateStatus();
  157. await updateGroups();
  158. await updateTask();
  159. });
  160. const __debounceUpdate = debounce(() => {
  161. updateUISetting(store.setting.mode, store.setting.uiSetting).catch((e) =>
  162. console.log("保存设置出错", e)
  163. );
  164. }, 3000);
  165. watch(
  166. () => [store.setting.uiSetting, store.setting.mode],
  167. () => {
  168. __debounceUpdate();
  169. },
  170. { deep: true }
  171. );
  172. // 切换currentTask
  173. watch(
  174. () => store.currentTask,
  175. () => {
  176. // 重置当前选择的quesiton和score
  177. store.currentQuestion = undefined;
  178. store.currentScore = undefined;
  179. }
  180. );
  181. const removeBrokenTask = () => {
  182. store.currentTask = undefined;
  183. if (store.historyOpen) {
  184. // store.currentTask = store.historyTasks[0];
  185. // 避免无限加载
  186. store.message = "加载失败,请重新选择。";
  187. } else {
  188. store.tasks.shift();
  189. }
  190. };
  191. const allZeroSubmit = async () => {
  192. const markResult = store.currentTask?.markResult;
  193. if (!markResult) return;
  194. const { markerScore, scoreList, trackList, specialTagList } = markResult;
  195. markResult.markerScore = 0;
  196. const ss = new Array(store.currentTask?.questionList.length);
  197. markResult.scoreList = ss.fill(0);
  198. markResult.trackList = [];
  199. try {
  200. await saveTaskToServer();
  201. } catch (error) {
  202. console.log("error restore");
  203. } finally {
  204. // console.log({ markerScore, scoreList, trackList });
  205. markResult.markerScore = markerScore;
  206. markResult.scoreList = scoreList;
  207. markResult.trackList = trackList;
  208. markResult.specialTagList = specialTagList;
  209. }
  210. };
  211. const unselectiveSubmit = async () => {
  212. const markResult = store.currentTask?.markResult;
  213. if (!markResult) return;
  214. try {
  215. const res = await doUnselectiveType();
  216. if (res?.data.success) {
  217. void message.success({ content: "未选做处理成功", duration: 3 });
  218. if (!store.historyOpen) {
  219. store.currentTask = undefined;
  220. store.tasks.shift();
  221. store.currentTask = store.tasks[0];
  222. }
  223. if (store.historyOpen) {
  224. EventBus.emit("should-reload-history");
  225. }
  226. await updateStatus();
  227. } else {
  228. void message.error({ content: res?.data.message || "错误", duration: 5 });
  229. }
  230. } catch (error) {
  231. console.log("未选做处理失败", error);
  232. void message.error({ content: "网络异常", duration: 5 });
  233. await new Promise((res) => setTimeout(res, 1500));
  234. window.location.reload();
  235. }
  236. };
  237. const saveTaskToServer = async () => {
  238. if (!store.currentTask) return;
  239. const markResult = store.currentTask.markResult;
  240. if (!markResult) return;
  241. const mkey = "save_task_key";
  242. type SubmitError = {
  243. question: Question;
  244. index: number;
  245. error: string;
  246. };
  247. const errors = [] as Array<SubmitError>;
  248. markResult.scoreList.forEach((score, index) => {
  249. if (!store.currentTask) return;
  250. const question = store.currentTask.questionList[index]!;
  251. let error;
  252. if (!isNumber(score)) {
  253. error = `${question.mainNumber}-${question.subNumber} 没有赋分不能提交。`;
  254. } else if (isNumber(question.maxScore) && score > question.maxScore) {
  255. error = `${question.mainNumber}-${question.subNumber} 赋分大于最高分不能提交。`;
  256. } else if (isNumber(question.minScore) && score < question.minScore) {
  257. error = `${question.mainNumber}-${question.subNumber} 赋分小于最低分不能提交。`;
  258. }
  259. if (error) {
  260. errors.push({ question, index, error });
  261. }
  262. });
  263. if (errors.length !== 0) {
  264. console.log(errors);
  265. const msg = errors.map((v) => h("div", `${v.error}`));
  266. void message.warning({
  267. content: h("span", ["校验失败", ...msg]),
  268. duration: 10,
  269. key: mkey,
  270. });
  271. return;
  272. }
  273. if (
  274. markResult.scoreList.length !== store.currentTask?.questionList.length ||
  275. !markResult.scoreList.every((s) => isNumber(s))
  276. ) {
  277. console.error({ content: "markResult格式不正确,缺少分数", key: mkey });
  278. return;
  279. }
  280. if (!store.isTrackMode) {
  281. markResult.trackList = [];
  282. } else {
  283. const trackScores =
  284. markResult.trackList
  285. .map((q) => Math.round((q.score || 0) * 100))
  286. .reduce((acc, s) => acc + s, 0) / 100;
  287. if (trackScores !== markResult.markerScore) {
  288. void message.error({
  289. content: "轨迹分与总分不一致,请检查。",
  290. duration: 3,
  291. key: mkey,
  292. });
  293. return;
  294. }
  295. }
  296. if (store.setting.forceSpecialTag) {
  297. if (
  298. markResult.trackList.length === 0 &&
  299. markResult.specialTagList.length === 0
  300. ) {
  301. void message.error({
  302. content: "强制标记已开启,请至少使用一个标记。",
  303. duration: 5,
  304. key: mkey,
  305. });
  306. return;
  307. }
  308. }
  309. console.log("save task to server");
  310. void message.loading({ content: "保存评卷任务...", key: mkey });
  311. const res = await saveTask();
  312. if (!res) return;
  313. // 故意不在此处同步等待,因为不必等待
  314. updateStatus().catch((e) => console.log("保存任务后获取status出错", e));
  315. if (res.data.success && store.currentTask) {
  316. void message.success({ content: "保存成功", key: mkey, duration: 2 });
  317. if (!store.historyOpen) {
  318. store.currentTask = undefined;
  319. store.tasks.shift();
  320. store.currentTask = store.tasks[0];
  321. } else {
  322. EventBus.emit("should-reload-history");
  323. }
  324. } else if (res.data.message) {
  325. console.log(res.data.message);
  326. void message.error({ content: res.data.message, key: mkey, duration: 10 });
  327. } else if (!store.currentTask) {
  328. void message.warn({ content: "暂无新任务", key: mkey, duration: 10 });
  329. }
  330. };
  331. </script>
  332. <style scoped>
  333. .my-container {
  334. width: 100%;
  335. overflow: clip;
  336. }
  337. </style>