exchange.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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: true,
  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. } else {
  270. // fill_area
  271. let options = [];
  272. dom
  273. .querySelector(".head-dynamic-aorb")
  274. .querySelectorAll(".head-dynamic-rect")
  275. .forEach((optionItem, optionIndex) => {
  276. options[optionIndex] = this.getOffsetInfo(optionItem);
  277. });
  278. fill_area.push({
  279. field: "paperType",
  280. index: this.getFillAreaIndex("paperType"),
  281. single: true,
  282. horizontal: true,
  283. items: [
  284. {
  285. main_number: null,
  286. sub_number: null,
  287. options,
  288. recog_info: [],
  289. },
  290. ],
  291. });
  292. }
  293. }
  294. return {
  295. info_area: [headArea],
  296. fill_area,
  297. barcode,
  298. };
  299. },
  300. getFillQuestionInfo(element) {
  301. const dom = this.getPreviewElementById(element.id);
  302. const single = !element.isMultiply;
  303. const horizontal = element.optionDirection === "horizontal";
  304. let fillAreas = [];
  305. dom.querySelectorAll(".group-item").forEach((groupItem) => {
  306. let listInfos = [];
  307. groupItem
  308. .querySelectorAll(".question-item")
  309. .forEach((questionItem, questionIndex) => {
  310. let options = [];
  311. questionItem.childNodes.forEach((optionItem, optionIndex) => {
  312. if (optionIndex)
  313. options[optionIndex - 1] = this.getOffsetInfo(optionItem);
  314. });
  315. listInfos[questionIndex] = {
  316. main_number: element.topicNo,
  317. sub_number: questionItem.firstChild.textContent * 1,
  318. options,
  319. recog_info: [],
  320. };
  321. });
  322. fillAreas.push({
  323. field: "question",
  324. index: this.getFillAreaIndex("question"),
  325. single,
  326. horizontal,
  327. items: listInfos,
  328. });
  329. });
  330. return {
  331. fill_area: fillAreas,
  332. };
  333. },
  334. getFillLineInfo(element) {
  335. const dom = this.getPreviewElementById(element.id);
  336. let sub_numbers = [];
  337. for (
  338. let i = element.startNumber,
  339. len = element.startNumber + element.questionsCount;
  340. i < len;
  341. i++
  342. ) {
  343. sub_numbers.push(i);
  344. }
  345. return {
  346. answer_area: [
  347. {
  348. main_number: element.topicNo,
  349. sub_number: sub_numbers.join(),
  350. area: this.getOffsetInfo(dom),
  351. },
  352. ],
  353. };
  354. },
  355. getExplainInfo(element) {
  356. const dom = this.getPreviewElementById(element.id);
  357. return {
  358. answer_area: [
  359. {
  360. main_number: element.topicNo,
  361. sub_number: element.serialNumber,
  362. area: this.getOffsetInfo(dom),
  363. },
  364. ],
  365. };
  366. },
  367. getCompositionInfo(element) {
  368. const dom = this.getPreviewElementById(element.id);
  369. return {
  370. answer_area: [
  371. {
  372. main_number: element.topicNo,
  373. sub_number: 1,
  374. area: this.getOffsetInfo(dom),
  375. },
  376. ],
  377. };
  378. },
  379. getOffsetInfo(dom) {
  380. const {
  381. width: domW,
  382. height: domH,
  383. left: domL,
  384. top: domT,
  385. } = dom.getBoundingClientRect();
  386. const {
  387. width: pageW,
  388. height: pageH,
  389. left: pageL,
  390. top: pageT,
  391. } = this.curPageOffsetInfo;
  392. // [x,y,w,h]
  393. const infos = [
  394. (domL - pageL) / pageW,
  395. (domT - pageT) / pageH,
  396. domW / pageW,
  397. domH / pageH,
  398. ];
  399. return infos.map((num) => num.toFixed(10) * 1);
  400. },
  401. getPageModel({ cardConfig, pages, answers = {} }) {
  402. let npages = this.parsePageExchange(pages);
  403. return JSON.stringify(
  404. {
  405. version: CARD_VERSION,
  406. cardType: "STANDARD",
  407. cardConfig,
  408. pages: npages,
  409. answers,
  410. },
  411. (k, v) => (k.startsWith("_") ? undefined : v)
  412. );
  413. },
  414. },
  415. };