exchange.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import { CARD_VERSION } from "../enumerate";
  2. import { deepCopy } from "../plugins/utils";
  3. const initIndex = {
  4. question: 1,
  5. absent: 1,
  6. breach: 1,
  7. paperType: 1,
  8. examNumber: 1,
  9. selective: 1,
  10. pageNumber: 1,
  11. };
  12. /**
  13. * 格式文档:https://doc.qmth.com.cn/pages/viewpage.action?pageId=19661052
  14. */
  15. export default {
  16. data() {
  17. return {
  18. fillAreaIndex: {
  19. ...initIndex,
  20. },
  21. VALID_ELEMENTS_FOR_EXTERNAL: [
  22. "LOCATOR",
  23. "BARCODE",
  24. "CARD_HEAD",
  25. "FILL_QUESTION",
  26. "FILL_LINE",
  27. "EXPLAIN",
  28. "COMPOSITION",
  29. ],
  30. curPageOffsetInfo: {},
  31. curPageDom: null,
  32. };
  33. },
  34. methods: {
  35. getFillAreaIndex(type) {
  36. return this.fillAreaIndex[type]++;
  37. },
  38. getElementHumpName(cont) {
  39. return cont
  40. .split("_")
  41. .map((item) => item[0] + item.substr(1).toLowerCase())
  42. .join("");
  43. },
  44. getPreviewElementById(id) {
  45. return document.getElementById(`preview-${id}`);
  46. },
  47. parsePageExchange(pages) {
  48. const npages = deepCopy(pages);
  49. // 单页题卡不显示页码涂块
  50. this.curPageOffsetInfo = document
  51. .getElementById(`preview-page-box-0`)
  52. .getBoundingClientRect();
  53. const pageNumberInfo =
  54. pages.length <= 2 ? null : this.getPageNumberInfo();
  55. npages.forEach((page, pindex) => {
  56. this.curPageDom = document.getElementById(`preview-page-box-${pindex}`);
  57. this.curPageOffsetInfo = this.curPageDom.getBoundingClientRect();
  58. let exchange = {
  59. card_type: 2,
  60. page_size: page.pageSize,
  61. page_image: "",
  62. locator: this.getLocatorInfo(this.curPageDom),
  63. fill_locator: [],
  64. check_area: {
  65. black_line: [],
  66. white_line: [],
  67. },
  68. barcode: [],
  69. qrcode: [],
  70. ocr_area: [],
  71. info_area: [],
  72. fill_area: [],
  73. answer_area: [],
  74. extension: {
  75. barcode: [],
  76. fill_area: [],
  77. ocr_area: [],
  78. qrcode: [],
  79. },
  80. };
  81. const elements = [
  82. page.globals,
  83. ...page.columns.map((column) => column.elements),
  84. ];
  85. elements.forEach((elemGroup) => {
  86. elemGroup.forEach((element) => {
  87. if (this.VALID_ELEMENTS_FOR_EXTERNAL.includes(element.type)) {
  88. const funcName = this.getElementHumpName(element.type);
  89. // console.log(funcName);
  90. const info = this[`get${funcName}Info`](element);
  91. Object.entries(info).forEach(([key, vals]) => {
  92. exchange[key] = exchange[key].concat(vals);
  93. });
  94. }
  95. });
  96. });
  97. if (!(pindex % 2) && pageNumberInfo) {
  98. let pnoInfo = deepCopy(pageNumberInfo);
  99. pnoInfo[0].index = this.getFillAreaIndex("pageNumber");
  100. exchange.fill_area = exchange.fill_area.concat(pnoInfo);
  101. }
  102. // 课程代码条码
  103. const extraInfo = this.getExtraInfo(this.curPageDom);
  104. Object.entries(extraInfo).forEach(([key, vals]) => {
  105. exchange.extension[key] = exchange.extension[key].concat(vals);
  106. });
  107. page.exchange = exchange;
  108. });
  109. this.fillAreaIndex = { ...initIndex };
  110. return npages;
  111. },
  112. getExtraInfo(pageDom) {
  113. const info = {
  114. barcode: [],
  115. };
  116. const courseBarDom = pageDom.querySelector(".course-barcode");
  117. if (courseBarDom) {
  118. info.barcode.push({
  119. field: "courseCode",
  120. area: this.getOffsetInfo(courseBarDom),
  121. });
  122. }
  123. return info;
  124. },
  125. getPageNumberInfo() {
  126. const dom = document.getElementById(`preview-page-box-0`);
  127. let options = [];
  128. dom
  129. .querySelector(".page-number-rect-list")
  130. .childNodes.forEach((item, index) => {
  131. options[index] = this.getOffsetInfo(item);
  132. });
  133. // console.log(options);
  134. return [
  135. {
  136. field: "pageNumber",
  137. index: 1,
  138. single: true,
  139. horizontal: true,
  140. items: [
  141. {
  142. main_number: null,
  143. sub_number: null,
  144. options,
  145. recog_info: [],
  146. },
  147. ],
  148. },
  149. ];
  150. },
  151. getLocatorInfo(curPageDom) {
  152. let tops = [];
  153. curPageDom
  154. .querySelector(".page-locator-top")
  155. .childNodes.forEach((item) => {
  156. tops.push(this.getOffsetInfo(item));
  157. });
  158. let bottoms = [];
  159. curPageDom
  160. .querySelector(".page-locator-bottom")
  161. .childNodes.forEach((item) => {
  162. bottoms.push(this.getOffsetInfo(item));
  163. });
  164. return {
  165. top: tops,
  166. bottom: bottoms,
  167. };
  168. },
  169. getCardHeadInfo(element) {
  170. const dom = this.getPreviewElementById(element.id);
  171. const headArea = this.getOffsetInfo(dom);
  172. let fill_area = [];
  173. let barcode = [];
  174. // 学生学号
  175. if (element.examNumberStyle === "FILL") {
  176. // fill_area
  177. let listInfos = [];
  178. dom
  179. .querySelectorAll(".stdno-fill-list")
  180. .forEach((questionItem, questionIndex) => {
  181. let options = [];
  182. questionItem.childNodes.forEach((optionItem, optionIndex) => {
  183. options[optionIndex] = this.getOffsetInfo(optionItem);
  184. });
  185. listInfos[questionIndex] = {
  186. main_number: null,
  187. sub_number: null,
  188. options,
  189. recog_info: [],
  190. };
  191. });
  192. fill_area.push({
  193. field: "examNumber",
  194. index: this.getFillAreaIndex("examNumber"),
  195. single: true,
  196. horizontal: false,
  197. items: listInfos,
  198. });
  199. } else {
  200. // barcode
  201. const stdnoDom =
  202. element.columnNumber <= 2
  203. ? dom.querySelector(".head-stdno").parentNode
  204. : dom.querySelector(".head-stdno");
  205. barcode.push({
  206. field: "examNumber",
  207. area: this.getOffsetInfo(stdnoDom),
  208. });
  209. }
  210. // 缺考涂填
  211. if (element.examAbsent && !element.isSimple) {
  212. fill_area.push({
  213. field: "absent",
  214. index: this.getFillAreaIndex("absent"),
  215. single: true,
  216. horizontal: true,
  217. items: [
  218. {
  219. main_number: null,
  220. sub_number: null,
  221. options: [
  222. this.getOffsetInfo(dom.querySelector(".dynamic-miss-area")),
  223. ],
  224. recog_info: [],
  225. },
  226. ],
  227. });
  228. }
  229. // 违纪标记
  230. if (element.discipline && !element.isSimple) {
  231. fill_area.push({
  232. field: "breach",
  233. index: this.getFillAreaIndex("breach"),
  234. single: true,
  235. horizontal: true,
  236. items: [
  237. {
  238. main_number: null,
  239. sub_number: null,
  240. options: [
  241. this.getOffsetInfo(dom.querySelector(".dynamic-breach-area")),
  242. ],
  243. recog_info: [],
  244. },
  245. ],
  246. });
  247. }
  248. // A/B卷类型
  249. if (element.aOrB && !element.isSimple) {
  250. if (element.paperType === "PRINT") {
  251. // barcode
  252. barcode.push({
  253. field: "paperType",
  254. area: this.getOffsetInfo(
  255. dom.querySelector(".dynamic-aorb-barcode")
  256. ),
  257. });
  258. } else {
  259. // fill_area
  260. let options = [];
  261. dom
  262. .querySelector(".head-dynamic-aorb")
  263. .querySelectorAll(".head-dynamic-rect")
  264. .forEach((optionItem, optionIndex) => {
  265. options[optionIndex] = this.getOffsetInfo(optionItem);
  266. });
  267. fill_area.push({
  268. field: "paperType",
  269. index: this.getFillAreaIndex("paperType"),
  270. single: true,
  271. horizontal: true,
  272. items: [
  273. {
  274. main_number: null,
  275. sub_number: null,
  276. options,
  277. recog_info: [],
  278. },
  279. ],
  280. });
  281. }
  282. }
  283. return {
  284. info_area: [headArea],
  285. fill_area,
  286. barcode,
  287. };
  288. },
  289. getFillQuestionInfo(element) {
  290. const dom = this.getPreviewElementById(element.id);
  291. const single = !element.isMultiply;
  292. const horizontal = element.optionDirection === "horizontal";
  293. let fillAreas = [];
  294. dom.querySelectorAll(".group-item").forEach((groupItem) => {
  295. let listInfos = [];
  296. groupItem
  297. .querySelectorAll(".question-item")
  298. .forEach((questionItem, questionIndex) => {
  299. let options = [];
  300. questionItem.childNodes.forEach((optionItem, optionIndex) => {
  301. if (optionIndex)
  302. options[optionIndex - 1] = this.getOffsetInfo(optionItem);
  303. });
  304. listInfos[questionIndex] = {
  305. main_number: element.topicNo,
  306. sub_number: questionItem.firstChild.textContent * 1,
  307. options,
  308. recog_info: [],
  309. };
  310. });
  311. fillAreas.push({
  312. field: "question",
  313. index: this.getFillAreaIndex("question"),
  314. single,
  315. horizontal,
  316. items: listInfos,
  317. });
  318. });
  319. return {
  320. fill_area: fillAreas,
  321. };
  322. },
  323. getFillLineInfo(element) {
  324. const dom = this.getPreviewElementById(element.id);
  325. let sub_numbers = [];
  326. for (
  327. let i = element.startNumber,
  328. len = element.startNumber + element.questionsCount;
  329. i < len;
  330. i++
  331. ) {
  332. sub_numbers.push(i);
  333. }
  334. return {
  335. answer_area: [
  336. {
  337. main_number: element.topicNo,
  338. sub_number: sub_numbers.join(),
  339. area: this.getOffsetInfo(dom),
  340. },
  341. ],
  342. };
  343. },
  344. getExplainInfo(element) {
  345. const dom = this.getPreviewElementById(element.id);
  346. return {
  347. answer_area: [
  348. {
  349. main_number: element.topicNo,
  350. sub_number: element.serialNumber,
  351. area: this.getOffsetInfo(dom),
  352. },
  353. ],
  354. };
  355. },
  356. getCompositionInfo(element) {
  357. const dom = this.getPreviewElementById(element.id);
  358. return {
  359. answer_area: [
  360. {
  361. main_number: element.topicNo,
  362. sub_number: 1,
  363. area: this.getOffsetInfo(dom),
  364. },
  365. ],
  366. };
  367. },
  368. getOffsetInfo(dom) {
  369. const {
  370. width: domW,
  371. height: domH,
  372. left: domL,
  373. top: domT,
  374. } = dom.getBoundingClientRect();
  375. const {
  376. width: pageW,
  377. height: pageH,
  378. left: pageL,
  379. top: pageT,
  380. } = this.curPageOffsetInfo;
  381. // [x,y,w,h]
  382. const infos = [
  383. (domL - pageL) / pageW,
  384. (domT - pageT) / pageH,
  385. domW / pageW,
  386. domH / pageH,
  387. ];
  388. return infos.map((num) => num.toFixed(10) * 1);
  389. },
  390. getPageModel({ cardConfig, pages, answers = {} }) {
  391. let npages = this.parsePageExchange(pages);
  392. return JSON.stringify(
  393. {
  394. version: CARD_VERSION,
  395. cardType: "STANDARD",
  396. cardConfig,
  397. pages: npages,
  398. answers,
  399. },
  400. (k, v) => (k.startsWith("_") ? undefined : v)
  401. );
  402. },
  403. },
  404. };