CardPreview.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <div :class="classes">
  3. <div class="preview-frame" id="preview-frame" v-if="IS_COMMON_CARD"></div>
  4. <div class="preview-body" v-else>
  5. <template v-for="(page, pageNo) in pages">
  6. <div
  7. :class="[
  8. 'page-box',
  9. `page-box-${cardConfig.pageSize}`,
  10. `page-box-${pageNo % 2}`
  11. ]"
  12. :key="pageNo"
  13. >
  14. <div
  15. :class="['page-locators', `page-locators-${page.locators.length}`]"
  16. >
  17. <ul
  18. class="page-locator-group"
  19. v-for="(locator, iind) in page.locators"
  20. :key="iind"
  21. >
  22. <li
  23. v-for="(elem, eindex) in locator"
  24. :key="eindex"
  25. :id="elem.id"
  26. ></li>
  27. </ul>
  28. </div>
  29. <!-- inner edit area -->
  30. <div class="page-main-inner">
  31. <div
  32. :class="['page-main', `page-main-${page.columns.length}`]"
  33. :style="{ margin: `0 -${page.columnGap / 2}px` }"
  34. >
  35. <div
  36. class="page-column"
  37. v-for="(column, columnNo) in page.columns"
  38. :key="columnNo"
  39. :style="{ padding: `0 ${page.columnGap / 2}px` }"
  40. >
  41. <div
  42. class="page-column-main"
  43. :id="[`column-${pageNo}-${columnNo}`]"
  44. >
  45. <div class="page-column-body" v-if="column.elements.length">
  46. <topic-element-preview
  47. class="page-column-element"
  48. v-for="element in column.elements"
  49. :key="element.id"
  50. :data="element"
  51. ></topic-element-preview>
  52. </div>
  53. <div class="page-column-body" v-else>
  54. <div
  55. class="page-column-forbid-area"
  56. v-if="cardConfig.showForbidArea"
  57. >
  58. <p>该区域严禁作答</p>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. <!-- outer edit area -->
  66. <div class="page-main-outer">
  67. <page-number
  68. type="rect"
  69. :total="pages.length"
  70. :current="pageNo + 1"
  71. ></page-number>
  72. <page-number
  73. type="text"
  74. :total="pages.length"
  75. :current="pageNo + 1"
  76. ></page-number>
  77. </div>
  78. </div>
  79. </template>
  80. </div>
  81. </div>
  82. </template>
  83. <script>
  84. import TopicElementPreview from "../components/TopicElementPreview";
  85. import PageNumber from "../components/PageNumber";
  86. import { cardDetail } from "../api";
  87. import previewTemp from "../previewTemp";
  88. import exchangeMixins from "../mixins/exchange";
  89. import { deepCopy } from "../plugins/utils";
  90. const JsBarcode = require("jsbarcode");
  91. export default {
  92. name: "card-preview",
  93. components: { TopicElementPreview, PageNumber },
  94. mixins: [exchangeMixins],
  95. data() {
  96. return {
  97. isPrint: this.$route.params.viewType !== "view",
  98. isFrame: this.$route.params.viewType === "frame",
  99. cardId: this.$route.params.cardId,
  100. cardConfig: {},
  101. pages: [],
  102. IS_COMMON_CARD: false
  103. };
  104. },
  105. computed: {
  106. classes() {
  107. return [
  108. "card-preview",
  109. {
  110. "card-print": this.isPrint
  111. }
  112. ];
  113. }
  114. },
  115. mounted() {
  116. if (this.isFrame) {
  117. this.initFrame();
  118. } else {
  119. this.init();
  120. }
  121. },
  122. methods: {
  123. initFrame() {
  124. const cardData = window.parent.cardData;
  125. if (!cardData) return;
  126. const { cardConfig, pages } = deepCopy(cardData);
  127. let fieldInfos = {};
  128. [...cardConfig.requiredFields, ...cardConfig.extendFields]
  129. .filter(item => item.enable)
  130. .map(item => {
  131. fieldInfos[item.code] = "${" + item.code + "}";
  132. });
  133. if (cardConfig.examNumberStyle === "PRINT") {
  134. fieldInfos.examNumber = "data:image/png;base64,${examNumber}";
  135. fieldInfos.examNumberStr = "${examNumberStr}";
  136. }
  137. if (cardConfig.aOrB && cardConfig.paperType === "PRINT") {
  138. fieldInfos.paperType = "data:image/png;base64,${paperType}";
  139. fieldInfos.paperTypeName = "${paperTypeName}";
  140. }
  141. this.cardConfig = cardConfig;
  142. this.pages = this.appendFieldInfo(pages, fieldInfos);
  143. this.$nextTick(() => {
  144. const cardContentTemp = previewTemp(this.$el.outerHTML);
  145. const model = this.getPageModel(cardData);
  146. window.parent &&
  147. window.parent.submitCardTemp &&
  148. window.parent.submitCardTemp(cardContentTemp, model);
  149. });
  150. },
  151. async init() {
  152. const detData = await cardDetail(this.cardId);
  153. this.IS_COMMON_CARD = detData.type === "GENERIC";
  154. // 通卡展示
  155. if (this.IS_COMMON_CARD) {
  156. this.$nextTick(() => {
  157. this.initHtmlTemp(detData.htmlContent);
  158. });
  159. return;
  160. }
  161. // 常规卡展示
  162. if (!detData.content) {
  163. this.$message.error("很抱歉,当前题卡还没开始制作!");
  164. return;
  165. }
  166. const { cardConfig, pages } = JSON.parse(detData.content);
  167. const fieldInfos = this.fetchFieldInfos(cardConfig, {});
  168. this.cardConfig = cardConfig;
  169. this.pages = this.appendFieldInfo(pages, fieldInfos);
  170. },
  171. initHtmlTemp(htmlTemp) {
  172. const iframeDom = document.createElement("iframe");
  173. document.getElementById("preview-frame").appendChild(iframeDom);
  174. const wwidth = window.innerWidth - 10;
  175. const wheight = window.innerHeight - 10;
  176. iframeDom.style.cssText = `width: ${wwidth}px;height: ${wheight}px;border:none;outline:none;`;
  177. const iframeDoc = iframeDom.contentDocument;
  178. iframeDoc.open();
  179. iframeDoc.write(htmlTemp);
  180. iframeDoc.close();
  181. },
  182. fetchFieldInfos(cardConfig, stdInfo) {
  183. let fieldInfos = {};
  184. const defContent = "相关信息";
  185. const defNumber = "123456789";
  186. [...cardConfig.requiredFields, ...cardConfig.extendFields]
  187. .filter(item => item.enable)
  188. .map(item => {
  189. fieldInfos[item.code] = stdInfo[item.code] || defContent;
  190. });
  191. if (cardConfig.examNumberStyle === "PRINT") {
  192. fieldInfos.examNumber = this.getBase64Barcode(
  193. stdInfo["examNumber"] || defNumber
  194. );
  195. fieldInfos.examNumberStr = stdInfo["examNumber"] || defNumber;
  196. }
  197. if (cardConfig.aOrB && cardConfig.paperType === "PRINT") {
  198. fieldInfos.paperType = this.getBase64Barcode(
  199. stdInfo["paperType"] || defNumber
  200. );
  201. fieldInfos.paperTypeName = stdInfo["paperTypeName"] || "A";
  202. }
  203. return fieldInfos;
  204. },
  205. getBase64Barcode(str) {
  206. const canvas = document.createElement("CANVAS");
  207. JsBarcode(canvas, str, {
  208. width: 2,
  209. height: 30,
  210. displayValue: false,
  211. marginLeft: 20,
  212. marginRight: 20,
  213. marginTop: 0,
  214. marginBottom: 0
  215. });
  216. return canvas.toDataURL();
  217. },
  218. appendFieldInfo(pages, fieldInfos) {
  219. pages.forEach((page, pageNo) => {
  220. if (pageNo % 2) return;
  221. const cardHeadElement = page.columns[0].elements[0];
  222. if (cardHeadElement.type === "CARD_HEAD") {
  223. cardHeadElement.fieldInfos = fieldInfos;
  224. }
  225. });
  226. return pages;
  227. }
  228. }
  229. };
  230. </script>