draw-poster.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <view class="content">
  3. <view style="display: flex;justify-content: center;align-items: center;margin: 10rpx;">
  4. <image @error="draw" :show-menu-by-longpress="true" :src="posterUrl" style="margin: 10rpx;width: 100%;height: 1200rpx;" />
  5. </view>
  6. <view v-if="posterUrl" @click="$mpi.savePhoto(posterUrl)" class="center" style="margin-top: 50rpx;">
  7. <view class="cu-btn flex text-lg btn-bg-color radius" style="padding: 46rpx 0;width: 90%;">
  8. 保存图片
  9. </view>
  10. </view>
  11. <r-canvas ref="rCanvas"></r-canvas>
  12. <view class="qrimg" v-if="$isNotEmpty(val)">
  13. <tki-qrcode :size="300" ref="qrcode" :val="val" :show="false" :showLoading="false" @result="qrR" />
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. import rCanvas from "@/pagesB/comps/r-canvas/r-canvas.vue"
  19. import tkiQrcode from "@/pagesB/comps/tki-qrcode/tki-qrcode.vue"
  20. export default {
  21. components: {
  22. tkiQrcode,
  23. rCanvas
  24. },
  25. data() {
  26. return {
  27. //背景图
  28. bgImg: 'https://guosen-bucket-ldt.obs.cn-south-1.myhuaweicloud.com:443/aa85f1da8bd84ad2944fe88dd4ef7045-hyRO9DPsxdFr399bc3fe4c0fb52273af5c8f694e1356.png',
  29. //二维码内容
  30. val: 'https://ldt.guosen-fumao.cn/wapp/qrcode?promotionCode=',
  31. //二维码地址
  32. qrUrl: '',
  33. //海报地址
  34. posterUrl: ''
  35. }
  36. },
  37. onLoad() {
  38. // if (this.$cache.get('posterUrl')) {
  39. // this.posterUrl = this.$cache.get('posterUrl')
  40. // return
  41. // }
  42. this.draw()
  43. },
  44. methods: {
  45. draw(){
  46. this.val=this.val + this.vuex_userId
  47. this.$nextTick(function() {
  48. this.$refs.qrcode._makeCode()
  49. })
  50. },
  51. qrR(res) {
  52. this.$dialog.showLoading('海报生成中')
  53. this.qrUrl = res
  54. this.drawPoster()
  55. },
  56. async drawPoster() {
  57. //获取用户信息
  58. let userInfo = (await this.$api.loginUser.detail({
  59. id: this.vuex_userId
  60. })).data
  61. let avatar = await this.$mpi.downloadFile(userInfo.avatar)
  62. this.$nextTick(async () => {
  63. const w = 354
  64. const h = 600
  65. // 初始化
  66. await this.$refs.rCanvas.init({
  67. canvas_id: "rCanvas",
  68. canvas_width: w,
  69. canvas_height: h,
  70. background_color: "#fff",
  71. hidden: true
  72. })
  73. // 画图
  74. await this.$refs.rCanvas.drawImage({
  75. url: this.bgImg,
  76. x: 0,
  77. y: 0,
  78. w,
  79. h: (h - 140),
  80. }).catch(err_msg => {
  81. uni.showToast({
  82. title: err_msg,
  83. icon: "none"
  84. })
  85. })
  86. //画二维码
  87. await this.$refs.rCanvas.drawImage({
  88. url: this.qrUrl,
  89. x: (w - 115),
  90. y: (h - 120),
  91. w: 100,
  92. h: 100
  93. }).catch(err_msg => {
  94. uni.showToast({
  95. title: err_msg,
  96. icon: "none"
  97. })
  98. })
  99. //画头像
  100. await this.$refs.rCanvas.drawImage({
  101. url: avatar,
  102. x: 26,
  103. y: (h - 120),
  104. w: 36,
  105. h: 36,
  106. border_radius: 20
  107. }).catch(err_msg => {
  108. uni.showToast({
  109. title: err_msg,
  110. icon: "none"
  111. })
  112. })
  113. // 画文字
  114. await this.$refs.rCanvas.drawText({
  115. text: userInfo.nickName,
  116. x: 70,
  117. y: (h - 96),
  118. font_color: "#000",
  119. font_size: 16,
  120. font_weight: 800,
  121. font_family: 'PingFang-SC-Heavy'
  122. }).catch(err_msg => {
  123. uni.showToast({
  124. title: err_msg,
  125. icon: "none"
  126. })
  127. })
  128. await this.$refs.rCanvas.drawText({
  129. text: "我是联兑通合伙人",
  130. max_width: 0,
  131. x: 30,
  132. y: (h - 52),
  133. font_color: "#333333",
  134. font_size: 14,
  135. font_weight: 300
  136. }).catch(err_msg => {
  137. uni.showToast({
  138. title: err_msg,
  139. icon: "none"
  140. })
  141. })
  142. await this.$refs.rCanvas.drawText({
  143. text: "欢迎加入联兑通",
  144. max_width: 0,
  145. x: 30,
  146. y: (h - 32),
  147. font_color: "#333333",
  148. font_size: 14,
  149. font_weight: 300
  150. }).catch(err_msg => {
  151. uni.showToast({
  152. title: err_msg,
  153. icon: "none"
  154. })
  155. })
  156. // 生成海报
  157. await this.$refs.rCanvas.draw((res) => {
  158. this.posterUrl = res.tempFilePath
  159. this.$cache.put('posterUrl', this.posterUrl)
  160. uni.hideLoading()
  161. })
  162. })
  163. }
  164. }
  165. }
  166. </script>
  167. <style>
  168. </style>