index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <div
  3. id="left"
  4. class="left"
  5. ref="modelView"
  6. :style="IMGSHOW ? '' : 'border:none'"
  7. >
  8. <img
  9. :style="modelData.background.css"
  10. v-if="modelData.background.checked"
  11. :src="modelData.background.name"
  12. alt=""
  13. crossorigin="anonymous"
  14. />
  15. <!-- 标题 -->
  16. <vue-draggable-resizable
  17. @dragstop="onDragstop1"
  18. @resizestop="onResizeStop1"
  19. :w="modelData.title.width"
  20. :h="modelData.title.height"
  21. :x="modelData.title.left"
  22. :y="modelData.title.top"
  23. :parent="true"
  24. v-if="modelData.title.checked"
  25. >
  26. <p :style="modelData.title.css">{{ modelData.title.name }}</p>
  27. </vue-draggable-resizable>
  28. <!-- 商品图 -->
  29. <vue-draggable-resizable
  30. @dragstop="onDragstop2"
  31. @resizestop="onResizeStop2"
  32. :w="modelData.goods.width"
  33. :h="modelData.goods.height"
  34. :x="modelData.goods.left"
  35. :y="modelData.goods.top"
  36. :parent="true"
  37. v-if="modelData.goods.checked"
  38. :style="
  39. modelData.goods.name ? '' : 'border: 1px dotted rgba(225,225,225,.4)'
  40. "
  41. >
  42. <img
  43. crossorigin="anonymous"
  44. v-if="modelData.goods.name"
  45. :style="modelData.goods.css"
  46. :src="$methodsTools.splitImgHost(modelData.goods.name)"
  47. alt=""
  48. />
  49. </vue-draggable-resizable>
  50. <!-- 分销码 -->
  51. <vue-draggable-resizable
  52. @dragstop="onDragstop3"
  53. @resizestop="onResizeStop3"
  54. :w="modelData.distribution.width"
  55. :h="modelData.distribution.height"
  56. :x="modelData.distribution.left"
  57. :y="modelData.distribution.top"
  58. :parent="true"
  59. v-if="modelData.distribution.checked"
  60. v-show="IMGSHOW"
  61. style="border: 1px solid skyblue"
  62. >
  63. <!-- <img
  64. :style="modelData.distribution.css"
  65. :src="modelData.distribution.name"
  66. alt=""
  67. /> -->
  68. </vue-draggable-resizable>
  69. <!-- 电子名片码 -->
  70. <vue-draggable-resizable
  71. @dragstop="onDragstop4"
  72. @resizestop="onResizeStop4"
  73. :w="modelData.cardCode.width"
  74. :h="modelData.cardCode.height"
  75. :x="modelData.cardCode.left"
  76. :y="modelData.cardCode.top"
  77. :parent="true"
  78. v-if="modelData.cardCode.checked"
  79. v-show="IMGSHOW"
  80. style="border: 1px solid skyblue"
  81. >
  82. <!-- <img
  83. :style="modelData.cardCode.css"
  84. :src="modelData.cardCode.name"
  85. alt=""
  86. /> -->
  87. </vue-draggable-resizable>
  88. <!-- 广告语 -->
  89. <vue-draggable-resizable
  90. @dragstop="onDragstop5"
  91. @resizestop="onResizeStop5"
  92. :w="modelData.advertise.width"
  93. :h="modelData.advertise.height"
  94. :x="modelData.advertise.left"
  95. :y="modelData.advertise.top"
  96. :parent="true"
  97. v-if="modelData.advertise.checked"
  98. >
  99. <p :style="modelData.advertise.css">{{ modelData.advertise.name }}</p>
  100. </vue-draggable-resizable>
  101. <!-- 机构 -->
  102. <vue-draggable-resizable
  103. @dragstop="onDragstop6"
  104. @resizestop="onResizeStop6"
  105. :w="modelData.mechanism.width"
  106. :h="modelData.mechanism.height"
  107. :x="modelData.mechanism.left"
  108. :y="modelData.mechanism.top"
  109. :parent="true"
  110. v-if="modelData.mechanism.checked"
  111. >
  112. <p :style="modelData.mechanism.css">{{ modelData.mechanism.name }}</p>
  113. </vue-draggable-resizable>
  114. </div>
  115. </template>
  116. <script>
  117. import html2Canvas from "html2canvas";
  118. import VueDraggableResizable from "vue-draggable-resizable";
  119. import "vue-draggable-resizable/dist/VueDraggableResizable.css";
  120. export default {
  121. components: { VueDraggableResizable },
  122. props: {
  123. modelData: {
  124. type: Object,
  125. default: () => {
  126. return {};
  127. },
  128. },
  129. },
  130. data() {
  131. return {
  132. IMGSHOW: true, //截图过程先隐藏二维码窗口
  133. };
  134. },
  135. methods: {
  136. //标签转oss图片路径
  137. changeFile() {
  138. return new Promise((resolve, reject) => {
  139. this.IMGSHOW = false;
  140. this.$nextTick(() => {
  141. html2Canvas(this.$refs.modelView, { useCORS: true, allowTaint: true })
  142. .then((canvas) => {
  143. const jpeg = canvas.toDataURL("image/jpeg", 1.0);
  144. this.$upload
  145. .upload(this.base64ToFile(jpeg), 0)
  146. .then((res) => {
  147. resolve(res);
  148. })
  149. .catch(() => {
  150. reject();
  151. });
  152. })
  153. .catch(() => {
  154. reject();
  155. })
  156. .finally(() => {
  157. this.IMGSHOW = true;
  158. });
  159. });
  160. });
  161. },
  162. //格式转换
  163. base64ToFile(urlData) {
  164. const arr = urlData.split(",");
  165. const mime = arr[0].match(/:(.*?);/)[1];
  166. const bytes = atob(arr[1]);
  167. let n = bytes.length;
  168. const ia = new Uint8Array(n);
  169. while (n--) {
  170. ia[n] = bytes.charCodeAt(n);
  171. }
  172. return new File([ia], "jpeg", { type: mime });
  173. },
  174. onDragstop1(left, top) {
  175. this.backData("title", left, top);
  176. },
  177. onResizeStop1(left, top, width, height) {
  178. this.backData("title", left, top, width, height);
  179. },
  180. onDragstop2(left, top) {
  181. this.backData("goods", left, top);
  182. },
  183. onResizeStop2(left, top, width, height) {
  184. this.backData("goods", left, top, width, height);
  185. },
  186. onDragstop3(left, top) {
  187. this.backData("distribution", left, top);
  188. },
  189. onResizeStop3(left, top, width, height) {
  190. this.backData("distribution", left, top, width, height);
  191. },
  192. onDragstop4(left, top) {
  193. this.backData("cardCode", left, top);
  194. },
  195. onResizeStop4(left, top, width, height) {
  196. this.backData("cardCode", left, top, width, height);
  197. },
  198. onDragstop5(left, top) {
  199. this.backData("advertise", left, top);
  200. },
  201. onResizeStop5(left, top, width, height) {
  202. this.backData("advertise", left, top, width, height);
  203. },
  204. onDragstop6(left, top) {
  205. this.backData("mechanism", left, top);
  206. },
  207. onResizeStop6(left, top, width, height) {
  208. this.backData("mechanism", left, top, width, height);
  209. },
  210. backData(name, left, top, width, height) {
  211. this.$emit("changeModelData", { name, left, top, width, height });
  212. },
  213. },
  214. };
  215. </script>
  216. <style lang="scss" scoped>
  217. #left {
  218. position: relative;
  219. width: 375px;
  220. height: 667px;
  221. border: 1px solid #333;
  222. margin-right: 20px;
  223. flex-shrink: 0;
  224. box-sizing: content-box;
  225. overflow: hidden;
  226. }
  227. .vdr {
  228. border-color: transparent;
  229. }
  230. .active {
  231. border: 1px dashed #000;
  232. }
  233. </style>