index.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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="$methodsTools.splitImgHost(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. :lock-aspect-ratio="true"
  63. >
  64. <!-- <img
  65. :style="modelData.distribution.css"
  66. :src="modelData.distribution.name"
  67. alt=""
  68. /> -->
  69. </vue-draggable-resizable>
  70. <vue-draggable-resizable
  71. @dragstop="onDragstop7"
  72. @resizestop="onResizeStop7"
  73. :w="modelData.distributionText.width"
  74. :h="modelData.distributionText.height"
  75. :x="modelData.distributionText.left"
  76. :y="modelData.distributionText.top"
  77. :parent="true"
  78. v-if="modelData.distributionText.checked"
  79. >
  80. <p style="white-space: pre-wrap" :style="modelData.distributionText.css">
  81. {{ modelData.distributionText.name }}
  82. </p>
  83. </vue-draggable-resizable>
  84. <!-- 电子名片码 -->
  85. <vue-draggable-resizable
  86. @dragstop="onDragstop4"
  87. @resizestop="onResizeStop4"
  88. :w="modelData.cardCode.width"
  89. :h="modelData.cardCode.height"
  90. :x="modelData.cardCode.left"
  91. :y="modelData.cardCode.top"
  92. :parent="true"
  93. v-if="modelData.cardCode.checked"
  94. v-show="IMGSHOW"
  95. style="border: 1px solid skyblue"
  96. :lock-aspect-ratio="true"
  97. >
  98. <!-- <img
  99. :style="modelData.cardCode.css"
  100. :src="modelData.cardCode.name"
  101. alt=""
  102. /> -->
  103. </vue-draggable-resizable>
  104. <vue-draggable-resizable
  105. @dragstop="onDragstop8"
  106. @resizestop="onResizeStop8"
  107. :w="modelData.cardCodeText.width"
  108. :h="modelData.cardCodeText.height"
  109. :x="modelData.cardCodeText.left"
  110. :y="modelData.cardCodeText.top"
  111. :parent="true"
  112. v-if="modelData.cardCodeText.checked"
  113. >
  114. <p style="white-space: pre-wrap" :style="modelData.cardCodeText.css">
  115. {{ modelData.cardCodeText.name }}
  116. </p>
  117. </vue-draggable-resizable>
  118. <!-- 广告语 -->
  119. <vue-draggable-resizable
  120. @dragstop="onDragstop5"
  121. @resizestop="onResizeStop5"
  122. :w="modelData.advertise.width"
  123. :h="modelData.advertise.height"
  124. :x="modelData.advertise.left"
  125. :y="modelData.advertise.top"
  126. :parent="true"
  127. v-if="modelData.advertise.checked"
  128. >
  129. <p style="white-space: pre-wrap" :style="modelData.advertise.css">
  130. {{ modelData.advertise.name }}
  131. </p>
  132. </vue-draggable-resizable>
  133. <!-- 机构 -->
  134. <vue-draggable-resizable
  135. @dragstop="onDragstop6"
  136. @resizestop="onResizeStop6"
  137. :w="modelData.mechanism.width"
  138. :h="modelData.mechanism.height"
  139. :x="modelData.mechanism.left"
  140. :y="modelData.mechanism.top"
  141. :parent="true"
  142. v-if="modelData.mechanism.checked"
  143. >
  144. <p :style="modelData.mechanism.css">{{ modelData.mechanism.name }}</p>
  145. </vue-draggable-resizable>
  146. </div>
  147. </template>
  148. <script>
  149. import html2Canvas from "html2canvas";
  150. import VueDraggableResizable from "vue-draggable-resizable";
  151. import "vue-draggable-resizable/dist/VueDraggableResizable.css";
  152. export default {
  153. components: { VueDraggableResizable },
  154. props: {
  155. modelData: {
  156. type: Object,
  157. default: () => {
  158. return {};
  159. },
  160. },
  161. },
  162. data() {
  163. return {
  164. IMGSHOW: true, //截图过程先隐藏二维码窗口
  165. };
  166. },
  167. methods: {
  168. //标签转oss图片路径
  169. changeFile() {
  170. return new Promise((resolve, reject) => {
  171. this.IMGSHOW = false;
  172. this.$nextTick(() => {
  173. html2Canvas(this.$refs.modelView, { useCORS: true, allowTaint: true })
  174. .then((canvas) => {
  175. const jpeg = canvas.toDataURL("image/jpeg", 1.0);
  176. this.$upload
  177. .upload(this.base64ToFile(jpeg), 0)
  178. .then((res) => {
  179. resolve(res);
  180. })
  181. .catch(() => {
  182. reject();
  183. });
  184. })
  185. .catch(() => {
  186. reject();
  187. })
  188. .finally(() => {
  189. this.IMGSHOW = true;
  190. });
  191. });
  192. });
  193. },
  194. //格式转换
  195. base64ToFile(urlData) {
  196. const arr = urlData.split(",");
  197. const mime = arr[0].match(/:(.*?);/)[1];
  198. const bytes = atob(arr[1]);
  199. let n = bytes.length;
  200. const ia = new Uint8Array(n);
  201. while (n--) {
  202. ia[n] = bytes.charCodeAt(n);
  203. }
  204. return new File([ia], "jpeg", { type: mime });
  205. },
  206. onDragstop1(left, top) {
  207. this.backData("title", left, top);
  208. },
  209. onResizeStop1(left, top, width, height) {
  210. this.backData("title", left, top, width, height);
  211. },
  212. onDragstop2(left, top) {
  213. this.backData("goods", left, top);
  214. },
  215. onResizeStop2(left, top, width, height) {
  216. this.backData("goods", left, top, width, height);
  217. },
  218. onDragstop3(left, top) {
  219. this.backData("distribution", left, top);
  220. },
  221. onResizeStop3(left, top, width, height) {
  222. this.backData("distribution", left, top, width, height);
  223. },
  224. onDragstop4(left, top) {
  225. this.backData("cardCode", left, top);
  226. },
  227. onResizeStop4(left, top, width, height) {
  228. this.backData("cardCode", left, top, width, height);
  229. },
  230. onDragstop5(left, top) {
  231. this.backData("advertise", left, top);
  232. },
  233. onResizeStop5(left, top, width, height) {
  234. this.backData("advertise", left, top, width, height);
  235. },
  236. onDragstop6(left, top) {
  237. this.backData("mechanism", left, top);
  238. },
  239. onResizeStop6(left, top, width, height) {
  240. this.backData("mechanism", left, top, width, height);
  241. },
  242. onDragstop7(left, top) {
  243. this.backData("distributionText", left, top);
  244. },
  245. onResizeStop7(left, top, width, height) {
  246. this.backData("distributionText", left, top, width, height);
  247. },
  248. onDragstop8(left, top) {
  249. this.backData("cardCodeText", left, top);
  250. },
  251. onResizeStop8(left, top, width, height) {
  252. this.backData("cardCodeText", left, top, width, height);
  253. },
  254. backData(name, left, top, width, height) {
  255. this.$emit("changeModelData", { name, left, top, width, height });
  256. },
  257. },
  258. };
  259. </script>
  260. <style lang="scss" scoped>
  261. #left {
  262. position: relative;
  263. width: 375px;
  264. height: 667px;
  265. border: 1px solid #333;
  266. margin-right: 20px;
  267. flex-shrink: 0;
  268. box-sizing: content-box;
  269. overflow: hidden;
  270. }
  271. .vdr {
  272. border-color: transparent;
  273. }
  274. .active {
  275. border: 1px dashed #000;
  276. }
  277. </style>