exchange.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. // 考生承诺书
  108. const uAreas = this.getUndertakingInfo(this.curPageDom);
  109. exchange.info_area.push(...uAreas);
  110. page.exchange = exchange;
  111. });
  112. this.fillAreaIndex = { ...initIndex };
  113. return npages;
  114. },
  115. getUndertakingInfo(pageDom) {
  116. const areas = [];
  117. const undertakingDom = pageDom.querySelector(".elem-undertaking");
  118. if (undertakingDom) {
  119. areas.push(this.getOffsetInfo(undertakingDom));
  120. }
  121. return areas;
  122. },
  123. getExtraInfo(pageDom) {
  124. const info = {
  125. barcode: [],
  126. };
  127. const courseBarDom = pageDom.querySelector(".course-barcode");
  128. if (courseBarDom) {
  129. info.barcode.push({
  130. field: "courseCode",
  131. area: this.getOffsetInfo(courseBarDom),
  132. });
  133. }
  134. return info;
  135. },
  136. getPageNumberInfo() {
  137. const dom = document.getElementById(`preview-page-box-0`);
  138. let options = [];
  139. dom
  140. .querySelector(".page-number-rect-list")
  141. .childNodes.forEach((item, index) => {
  142. options[index] = this.getOffsetInfo(item);
  143. });
  144. // console.log(options);
  145. return [
  146. {
  147. field: "pageNumber",
  148. index: 1,
  149. single: true,
  150. horizontal: true,
  151. items: [
  152. {
  153. main_number: null,
  154. sub_number: null,
  155. options,
  156. recog_info: [],
  157. },
  158. ],
  159. },
  160. ];
  161. },
  162. getLocatorInfo(curPageDom) {
  163. let tops = [];
  164. curPageDom
  165. .querySelector(".page-locator-top")
  166. .childNodes.forEach((item) => {
  167. tops.push(this.getOffsetInfo(item));
  168. });
  169. let bottoms = [];
  170. curPageDom
  171. .querySelector(".page-locator-bottom")
  172. .childNodes.forEach((item) => {
  173. bottoms.push(this.getOffsetInfo(item));
  174. });
  175. return {
  176. top: tops,
  177. bottom: bottoms,
  178. };
  179. },
  180. getCardHeadInfo(element) {
  181. const dom = this.getPreviewElementById(element.id);
  182. const headArea = this.getOffsetInfo(dom);
  183. let fill_area = [];
  184. let barcode = [];
  185. // 学生学号
  186. if (element.examNumberStyle === "FILL") {
  187. // fill_area
  188. let listInfos = [];
  189. dom
  190. .querySelectorAll(".stdno-fill-list")
  191. .forEach((questionItem, questionIndex) => {
  192. let options = [];
  193. questionItem.childNodes.forEach((optionItem, optionIndex) => {
  194. options[optionIndex] = this.getOffsetInfo(optionItem);
  195. });
  196. listInfos[questionIndex] = {
  197. main_number: null,
  198. sub_number: null,
  199. options,
  200. recog_info: [],
  201. };
  202. });
  203. fill_area.push({
  204. field: "examNumber",
  205. index: this.getFillAreaIndex("examNumber"),
  206. single: false,
  207. horizontal: false,
  208. items: listInfos,
  209. });
  210. } else {
  211. // barcode
  212. const stdnoDom =
  213. element.columnNumber <= 2
  214. ? dom.querySelector(".head-stdno").parentNode
  215. : dom.querySelector(".head-stdno");
  216. barcode.push({
  217. field: "examNumber",
  218. area: this.getOffsetInfo(stdnoDom),
  219. });
  220. }
  221. // 缺考涂填
  222. if (element.examAbsent && !element.isSimple) {
  223. fill_area.push({
  224. field: "absent",
  225. index: this.getFillAreaIndex("absent"),
  226. single: true,
  227. horizontal: true,
  228. items: [
  229. {
  230. main_number: null,
  231. sub_number: null,
  232. options: [
  233. this.getOffsetInfo(dom.querySelector(".dynamic-miss-area")),
  234. ],
  235. recog_info: [],
  236. },
  237. ],
  238. });
  239. }
  240. // 违纪标记
  241. if (element.discipline && !element.isSimple) {
  242. fill_area.push({
  243. field: "breach",
  244. index: this.getFillAreaIndex("breach"),
  245. single: true,
  246. horizontal: true,
  247. items: [
  248. {
  249. main_number: null,
  250. sub_number: null,
  251. options: [
  252. this.getOffsetInfo(dom.querySelector(".dynamic-breach-area")),
  253. ],
  254. recog_info: [],
  255. },
  256. ],
  257. });
  258. }
  259. // A/B卷类型
  260. if (element.aOrB && !element.isSimple) {
  261. // if (element.paperType === "PRINT") {
  262. // // barcode
  263. // barcode.push({
  264. // field: "paperType",
  265. // area: this.getOffsetInfo(
  266. // dom.querySelector(".dynamic-aorb-barcode")
  267. // ),
  268. // });
  269. // }
  270. if (element.paperType === "FILL") {
  271. // fill_area
  272. let options = [];
  273. dom
  274. .querySelector(".head-dynamic-aorb")
  275. .querySelectorAll(".head-dynamic-rect")
  276. .forEach((optionItem, optionIndex) => {
  277. options[optionIndex] = this.getOffsetInfo(optionItem);
  278. });
  279. fill_area.push({
  280. field: "paperType",
  281. index: this.getFillAreaIndex("paperType"),
  282. single: false,
  283. horizontal: true,
  284. items: [
  285. {
  286. main_number: null,
  287. sub_number: null,
  288. options,
  289. recog_info: [],
  290. },
  291. ],
  292. });
  293. }
  294. }
  295. return {
  296. info_area: [headArea],
  297. fill_area,
  298. barcode,
  299. };
  300. },
  301. getFillQuestionInfo(element) {
  302. const dom = this.getPreviewElementById(element.id);
  303. const single = !element.isMultiply;
  304. const horizontal = element.optionDirection === "horizontal";
  305. let fillAreas = [];
  306. dom.querySelectorAll(".group-item").forEach((groupItem) => {
  307. let listInfos = [];
  308. groupItem
  309. .querySelectorAll(".question-item")
  310. .forEach((questionItem, questionIndex) => {
  311. let options = [];
  312. questionItem.childNodes.forEach((optionItem, optionIndex) => {
  313. if (optionIndex)
  314. options[optionIndex - 1] = this.getOffsetInfo(optionItem);
  315. });
  316. listInfos[questionIndex] = {
  317. main_number: element.topicNo,
  318. sub_number: questionItem.firstChild.textContent * 1,
  319. options,
  320. recog_info: [],
  321. };
  322. });
  323. fillAreas.push({
  324. field: "question",
  325. index: this.getFillAreaIndex("question"),
  326. single,
  327. horizontal,
  328. items: listInfos,
  329. });
  330. });
  331. return {
  332. fill_area: fillAreas,
  333. };
  334. },
  335. getFillLineInfo(element) {
  336. const dom = this.getPreviewElementById(element.id);
  337. let sub_numbers = [];
  338. for (
  339. let i = element.startNumber,
  340. len = element.startNumber + element.questionsCount;
  341. i < len;
  342. i++
  343. ) {
  344. sub_numbers.push(i);
  345. }
  346. return {
  347. answer_area: [
  348. {
  349. main_number: element.topicNo,
  350. sub_number: sub_numbers.join(),
  351. area: this.getOffsetInfo(dom),
  352. },
  353. ],
  354. };
  355. },
  356. getExplainInfo(element) {
  357. const dom = this.getPreviewElementById(element.id);
  358. return {
  359. answer_area: [
  360. {
  361. main_number: element.topicNo,
  362. sub_number: element.serialNumber,
  363. area: this.getOffsetInfo(dom),
  364. },
  365. ],
  366. };
  367. },
  368. getCompositionInfo(element) {
  369. const dom = this.getPreviewElementById(element.id);
  370. return {
  371. answer_area: [
  372. {
  373. main_number: element.topicNo,
  374. sub_number: 1,
  375. area: this.getOffsetInfo(dom),
  376. },
  377. ],
  378. };
  379. },
  380. getOffsetInfo(dom) {
  381. const {
  382. width: domW,
  383. height: domH,
  384. left: domL,
  385. top: domT,
  386. } = dom.getBoundingClientRect();
  387. const {
  388. width: pageW,
  389. height: pageH,
  390. left: pageL,
  391. top: pageT,
  392. } = this.curPageOffsetInfo;
  393. // [x,y,w,h]
  394. const infos = [
  395. (domL - pageL) / pageW,
  396. (domT - pageT) / pageH,
  397. domW / pageW,
  398. domH / pageH,
  399. ];
  400. return infos.map((num) => num.toFixed(10) * 1);
  401. },
  402. getPageModel({ cardConfig, pages, answers = {} }) {
  403. let npages = this.parsePageExchange(pages);
  404. return JSON.stringify(
  405. {
  406. version: CARD_VERSION,
  407. cardType: "STANDARD",
  408. cardConfig,
  409. pages: npages,
  410. answers,
  411. },
  412. (k, v) => (k.startsWith("_") ? undefined : v)
  413. );
  414. },
  415. },
  416. };