CardFreePreview.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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-${page.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 { getPageModel } from "../cardFormatTransform";
  89. import { deepCopy } from "../../../plugins/utils";
  90. const JsBarcode = require("jsbarcode");
  91. export default {
  92. name: "card-free-preview",
  93. components: { TopicElementPreview, PageNumber },
  94. data() {
  95. return {
  96. isPrint: this.$route.params.viewType !== "view",
  97. isFrame: this.$route.params.viewType === "frame",
  98. cardId: this.$route.params.cardId,
  99. cardConfig: {},
  100. pages: [],
  101. IS_COMMON_CARD: false
  102. };
  103. },
  104. computed: {
  105. classes() {
  106. return [
  107. "card-free-preview",
  108. {
  109. "card-print": this.isPrint
  110. }
  111. ];
  112. }
  113. },
  114. mounted() {
  115. if (this.isFrame) {
  116. this.initFrame();
  117. } else {
  118. this.init();
  119. }
  120. },
  121. methods: {
  122. initFrame() {
  123. const cardData = window.parent.cardData;
  124. if (!cardData) return;
  125. const { cardConfig, pages } = deepCopy(cardData);
  126. let fieldInfos = {};
  127. [...cardConfig.requiredFields, ...cardConfig.extendFields]
  128. .filter(item => item.enable)
  129. .map(item => {
  130. fieldInfos[item.code] = "${" + item.code + "}";
  131. });
  132. this.cardConfig = cardConfig;
  133. this.pages = this.appendFieldInfo(pages, fieldInfos);
  134. this.$nextTick(() => {
  135. const cardContentTemp = previewTemp(this.$el.outerHTML);
  136. const model = getPageModel(cardData);
  137. window.parent &&
  138. window.parent.submitCardTemp &&
  139. window.parent.submitCardTemp(cardContentTemp, model);
  140. });
  141. },
  142. async init() {
  143. const detData = await cardDetail(this.cardId);
  144. this.IS_COMMON_CARD = detData.type === "GENERIC";
  145. // 通卡展示
  146. if (this.IS_COMMON_CARD) {
  147. this.$nextTick(() => {
  148. this.initHtmlTemp(detData.htmlContent);
  149. });
  150. return;
  151. }
  152. // 常规卡展示
  153. if (!detData.content) {
  154. this.$message.error("很抱歉,当前题卡还没开始制作!");
  155. return;
  156. }
  157. const { cardConfig, pages } = JSON.parse(detData.content);
  158. const fieldInfos = this.fetchFieldInfos(cardConfig, {});
  159. this.cardConfig = cardConfig;
  160. this.pages = this.appendFieldInfo(pages, fieldInfos);
  161. },
  162. initHtmlTemp(htmlTemp) {
  163. const iframeDom = document.createElement("iframe");
  164. document.getElementById("preview-frame").appendChild(iframeDom);
  165. const wwidth = window.innerWidth - 10;
  166. const wheight = window.innerHeight - 10;
  167. iframeDom.style.cssText = `width: ${wwidth}px;height: ${wheight}px;border:none;outline:none;`;
  168. const iframeDoc = iframeDom.contentDocument;
  169. iframeDoc.open();
  170. iframeDoc.write(htmlTemp);
  171. iframeDoc.close();
  172. },
  173. fetchFieldInfos(cardConfig, stdInfo) {
  174. let fieldInfos = {};
  175. const defContent = "相关信息";
  176. [...cardConfig.requiredFields, ...cardConfig.extendFields]
  177. .filter(item => item.enable)
  178. .map(item => {
  179. fieldInfos[item.code] = stdInfo[item.code] || defContent;
  180. });
  181. return fieldInfos;
  182. },
  183. getBase64Barcode(str) {
  184. const canvas = document.createElement("CANVAS");
  185. JsBarcode(canvas, str, {
  186. width: 2,
  187. height: 30,
  188. displayValue: false,
  189. marginLeft: 20,
  190. marginRight: 20,
  191. marginTop: 0,
  192. marginBottom: 0
  193. });
  194. return canvas.toDataURL();
  195. },
  196. appendFieldInfo(pages, fieldInfos) {
  197. const VALID_ELEMENTS_FOR_EXTERNAL = ["BARCODE", "FILL_FIELD"];
  198. pages.forEach(page => {
  199. page.columns.forEach(column => {
  200. column.elements.forEach(element => {
  201. if (!VALID_ELEMENTS_FOR_EXTERNAL.includes(element.type)) return;
  202. if (element.type === "BARCODE") {
  203. const field = element.fields[0] && element.fields[0].code;
  204. element.content = `data:image/png;base64,${fieldInfos[field]}`;
  205. return;
  206. }
  207. element.fieldInfos = fieldInfos;
  208. });
  209. });
  210. });
  211. return pages;
  212. }
  213. }
  214. };
  215. </script>