index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <template>
  2. <div>
  3. <Popup
  4. position="bottom"
  5. :style="{width: '80%', height: '88.5%', left: '10%', bottom: '3%', background: 'transparent'}"
  6. v-model="showPoster">
  7. <img
  8. class="poster"
  9. :src="strPostSrc"
  10. alt=""
  11. @click="funPreviewImage">
  12. </Popup>
  13. <div class="hide" id="qr-code"></div>
  14. <canvas class="hide" ref="myCanvas"></canvas>
  15. </div>
  16. </template>
  17. <script>
  18. import { Popup, Notify, Toast, ImagePreview } from 'vant'
  19. import QRCode from 'qrcodejs2'
  20. export default {
  21. name: 'poster',
  22. components: {
  23. Popup
  24. },
  25. props: {
  26. posterBg: {
  27. type: String,
  28. default: ''
  29. },
  30. codeParams: {
  31. type: Object,
  32. default: function () {
  33. return {
  34. partnerImgUrl: '',
  35. partnerName: '',
  36. link: '',
  37. x: 112,
  38. y: 1046,
  39. width: 202,
  40. height: 202
  41. }
  42. }
  43. }
  44. },
  45. data () {
  46. return {
  47. showPoster: false,
  48. strPostSrc: '',
  49. qrcode: null
  50. }
  51. },
  52. methods: {
  53. createQRCode () {
  54. const { link } = this.codeParams
  55. Toast.loading({
  56. message: '海报生成中...',
  57. forbidClick: true,
  58. duration: 0
  59. })
  60. if (!this.qrcode) {
  61. this.qrcode = new QRCode('qr-code', {
  62. width: 212,
  63. height: 212,
  64. text: link,
  65. colorDark: '#000',
  66. colorLight: '#fff'
  67. })
  68. } else {
  69. this.qrcode.clear()
  70. this.qrcode.makeCode(link)
  71. }
  72. const timer = setTimeout(() => {
  73. clearTimeout(timer)
  74. this.createPoster()
  75. }, 500)
  76. },
  77. setImg (params) {
  78. const { isDrewCircle } = params
  79. const img = new Image()
  80. img.crossOrigin = 'anonymous'
  81. img.onload = function () {
  82. if (isDrewCircle) {
  83. params.ctx.beginPath()
  84. params.ctx.arc(params.x + params.width / 2, params.y + params.width / 2, params.width / 2, 0, Math.PI * 2, false)
  85. params.ctx.clip()
  86. }
  87. params.ctx.drawImage(img, params.x, params.y, params.width, params.height)
  88. params.cb()
  89. }
  90. img.src = params.src
  91. },
  92. createPoster () {
  93. const vm = this
  94. const { posterBg } = this
  95. const {
  96. partnerImgUrl,
  97. partnerName,
  98. x,
  99. y,
  100. width,
  101. height,
  102. canvasWidth,
  103. canvasHeight,
  104. partnerLabelY,
  105. partnerImgUrlY
  106. } = this.codeParams
  107. const canvas = vm.$refs.myCanvas
  108. const ctx = canvas.getContext('2d')
  109. const canvas2 = document.getElementById('qr-code').getElementsByTagName('canvas')[0]
  110. const code = {}
  111. code.src = canvas2.toDataURL('image/png', 1)
  112. canvas.width = canvasWidth || 750
  113. canvas.height = canvasHeight || 1446
  114. // 绘制背景
  115. vm.setImg({
  116. ctx,
  117. src: posterBg,
  118. x: 0,
  119. y: 0,
  120. width: canvasWidth || 750,
  121. height: canvasHeight || 1446,
  122. cb () {
  123. if (partnerImgUrl) {
  124. ctx.font = '400 28px/40px STYuanti-Regular'
  125. ctx.fillStyle = '#C9AD8D'
  126. ctx.fillText('推广人:', 236, partnerLabelY || 1128)
  127. ctx.save()
  128. vm.setImg({
  129. ctx,
  130. src: partnerImgUrl,
  131. x: 346,
  132. y: partnerImgUrlY || 1100,
  133. width: 40,
  134. height: 40,
  135. isDrewCircle: true,
  136. cb () {
  137. ctx.restore()
  138. ctx.font = '600 28px/40px STYuanti-Regular'
  139. ctx.fillStyle = '#C9AD8D'
  140. ctx.fillText(partnerName, 398, partnerLabelY || 1130)
  141. // 绘制二维码
  142. vm.setImg({
  143. ctx,
  144. src: code.src,
  145. x: x,
  146. y: y,
  147. width: width,
  148. height: height,
  149. cb () {
  150. vm.strPostSrc = canvas.toDataURL('image/png', 1)
  151. vm.showPoster = true
  152. Toast.clear()
  153. Notify({ type: 'primary', message: '长按图片保存到相册', duration: 3000 })
  154. }
  155. })
  156. }
  157. })
  158. } else {
  159. // 绘制二维码
  160. vm.setImg({
  161. ctx,
  162. src: code.src,
  163. x: x,
  164. y: y,
  165. width: width,
  166. height: height,
  167. cb () {
  168. vm.strPostSrc = canvas.toDataURL('image/png', 1)
  169. vm.showPoster = true
  170. Toast.clear()
  171. Notify({ type: 'primary', message: '长按图片保存到相册', duration: 3000 })
  172. }
  173. })
  174. }
  175. }
  176. })
  177. },
  178. // 图片预览
  179. funPreviewImage () {
  180. const vm = this
  181. if (!vm.strPostSrc) {
  182. return
  183. }
  184. ImagePreview([vm.strPostSrc])
  185. // window.wx.previewImage({
  186. // current: vm.strPostSrc,
  187. // urls: [vm.strPostSrc]
  188. // })
  189. }
  190. }
  191. }
  192. </script>
  193. <style lang="scss" scoped>
  194. .hide {
  195. display: none;
  196. }
  197. .poster {
  198. display: block;
  199. width: 100%;
  200. }
  201. </style>