CardFreePreview.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <div :class="classes">
  3. <card-free-view
  4. v-if="pages.length"
  5. ref="CardFreeView"
  6. class="preview-body"
  7. :pages="pages"
  8. :card-config="cardConfig"
  9. ></card-free-view>
  10. </div>
  11. </template>
  12. <script>
  13. import CardFreeView from "../../../../card/modules/free/components/CardFreeView";
  14. import { cardDetail } from "../api";
  15. import { deepCopy } from "@/plugins/utils";
  16. const JsBarcode = require("jsbarcode");
  17. export default {
  18. name: "card-free-preview",
  19. components: { CardFreeView },
  20. data() {
  21. return {
  22. isPrint: this.$route.params.viewType !== "view",
  23. isFrame: this.$route.params.viewType === "frame",
  24. cardId: this.$route.params.cardId,
  25. pages: [],
  26. cardConfig: {}
  27. };
  28. },
  29. computed: {
  30. classes() {
  31. return [
  32. "card-free-preview",
  33. {
  34. "card-print": this.isPrint
  35. }
  36. ];
  37. }
  38. },
  39. mounted() {
  40. if (this.isFrame) {
  41. this.initFrame();
  42. } else {
  43. this.init();
  44. }
  45. },
  46. methods: {
  47. initFrame() {
  48. const cardData = window.parent.cardData;
  49. if (!cardData) return;
  50. const { cardConfig, pages } = deepCopy(cardData);
  51. let fieldInfos = {};
  52. [...cardConfig.requiredFields, ...cardConfig.extendFields]
  53. .filter(item => item.enable)
  54. .map(item => {
  55. fieldInfos[item.code] = "${" + item.code + "}";
  56. });
  57. this.cardConfig = cardConfig;
  58. this.pages = this.appendFieldInfo(pages, fieldInfos);
  59. this.$nextTick(() => {
  60. const cardContentTemp = this.$refs.CardFreeView.getPreviewTemp(
  61. this.$el.outerHTML
  62. );
  63. const model = this.$refs.CardFreeView.getPageModel(cardData);
  64. window.parent &&
  65. window.parent.submitCardTemp &&
  66. window.parent.submitCardTemp(cardContentTemp, model);
  67. });
  68. },
  69. async init() {
  70. const detData = await cardDetail(this.cardId);
  71. if (!detData.content) {
  72. this.$message.error("很抱歉,当前题卡还没开始制作!");
  73. return;
  74. }
  75. const { cardConfig, pages } = JSON.parse(detData.content);
  76. const fieldInfos = this.fetchFieldInfos(cardConfig, {});
  77. this.cardConfig = cardConfig;
  78. this.pages = this.appendFieldInfo(pages, fieldInfos);
  79. },
  80. fetchFieldInfos(cardConfig, stdInfo) {
  81. let fieldInfos = {};
  82. const defContent = "相关信息";
  83. [...cardConfig.requiredFields, ...cardConfig.extendFields]
  84. .filter(item => item.enable)
  85. .map(item => {
  86. fieldInfos[item.code] = stdInfo[item.code] || defContent;
  87. });
  88. return fieldInfos;
  89. },
  90. getBase64Barcode(str) {
  91. const canvas = document.createElement("CANVAS");
  92. JsBarcode(canvas, str, {
  93. width: 2,
  94. height: 30,
  95. displayValue: false,
  96. marginLeft: 20,
  97. marginRight: 20,
  98. marginTop: 0,
  99. marginBottom: 0
  100. });
  101. return canvas.toDataURL();
  102. },
  103. appendFieldInfo(pages, fieldInfos) {
  104. const VALID_ELEMENTS_FOR_EXTERNAL = ["BARCODE", "FILL_FIELD"];
  105. pages.forEach(page => {
  106. page.columns.forEach(column => {
  107. column.elements.forEach(element => {
  108. if (!VALID_ELEMENTS_FOR_EXTERNAL.includes(element.type)) return;
  109. if (element.type === "BARCODE") {
  110. const field = element.fields[0] && element.fields[0].code;
  111. element.content = `data:image/png;base64,${fieldInfos[field]}`;
  112. return;
  113. }
  114. element.fieldInfos = fieldInfos;
  115. });
  116. });
  117. });
  118. return pages;
  119. }
  120. }
  121. };
  122. </script>