CardPreview.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div :class="classes">
  3. <card-view
  4. v-if="pages.length"
  5. ref="CardView"
  6. class="preview-body"
  7. :pages="pages"
  8. :card-config="cardConfig"
  9. ></card-view>
  10. </div>
  11. </template>
  12. <script>
  13. import CardView from "../components/CardView";
  14. import { cardDetail } from "../api";
  15. import { deepCopy } from "../plugins/utils";
  16. import html2canvas from "html2canvas";
  17. export default {
  18. name: "CardPreview",
  19. components: { CardView },
  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. cardConfig: {},
  26. pages: [],
  27. };
  28. },
  29. computed: {
  30. classes() {
  31. return [
  32. "card-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. this.cardConfig = cardConfig;
  52. this.pages = pages;
  53. this.$nextTick(() => {
  54. this.buildData(cardData);
  55. });
  56. },
  57. async buildData(cardData) {
  58. const cardContentTemp = this.$refs.CardView.getPreviewTemp(
  59. this.$el.outerHTML
  60. );
  61. // console.log(cardContentTemp);
  62. const cardImages = await this.transformImags().catch(() => {});
  63. try {
  64. const model = this.$refs.CardView.getPageModel(cardData);
  65. window.parent &&
  66. window.parent.submitCardTemp &&
  67. window.parent.submitCardTemp(model, cardImages, cardContentTemp);
  68. } catch (error) {
  69. console.dir(error);
  70. window.parent &&
  71. window.parent.submitCardTemp &&
  72. window.parent.submitCardTemp("", "", "");
  73. }
  74. },
  75. async init() {
  76. const detDataRes = await cardDetail(this.cardId);
  77. // 常规卡展示
  78. if (!detDataRes.data || !detDataRes.data.content) {
  79. this.$message.error("很抱歉,当前无题卡数据!");
  80. return;
  81. }
  82. const { cardConfig, pages } = JSON.parse(detDataRes.data.content);
  83. this.cardConfig = cardConfig;
  84. this.pages = pages;
  85. },
  86. async transformPageToImage(pageDom) {
  87. const canvasDom = await html2canvas(pageDom, {
  88. allowTaint: true,
  89. imageTimeout: 3000,
  90. scale: 2,
  91. });
  92. return canvasDom.toDataURL();
  93. },
  94. async transformImags() {
  95. const pageDoms = this.$el.querySelectorAll(".page-box");
  96. let images = [];
  97. for (let i = 0; i < pageDoms.length; i++) {
  98. const pageDom = pageDoms[i];
  99. const imgDataUrl = await this.transformPageToImage(pageDom);
  100. images[i] = imgDataUrl;
  101. }
  102. // console.log(images);
  103. return images;
  104. },
  105. },
  106. };
  107. </script>