exchange.js 11 KB

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