裂变星小程序-25.03.04
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
4.1 KiB

  1. <template>
  2. <view class="page">
  3. <navbar title="分享好友" leftClick @leftClick="$utils.navigateBack" />
  4. <view class="flex content">
  5. <view style="width: 598rpx; height: 1063rpx;">
  6. <canvas id="myCanvas" canvas-id="firstCanvas1" type="2d" style="width: 100%; height: 100%;"></canvas>
  7. </view>
  8. <view class="flex btns">
  9. <button class="flex btn btn-back" @click="$utils.navigateBack">返回</button>
  10. <button plain class="flex btn btn-save" @click="saveImg" >保存到相册</button>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import { mapState } from 'vuex'
  17. export default {
  18. data() {
  19. return {
  20. wxCodeImage: null,
  21. canvas: {},
  22. }
  23. },
  24. computed : {
  25. ...mapState(['configList'])
  26. },
  27. onLoad() {
  28. },
  29. onReady() {
  30. this.fetchQrCode()
  31. },
  32. methods: {
  33. async fetchQrCode() {
  34. try {
  35. this.wxCodeImage = await this.$fetch('getQrCode')
  36. this.draw()
  37. } catch (err) {
  38. }
  39. },
  40. draw() {
  41. uni.showLoading({
  42. title: "拼命绘画中..."
  43. })
  44. wx.createSelectorQuery()
  45. .select('#myCanvas') // 绘制的canvas的id
  46. .fields({
  47. node: true,
  48. size: true
  49. })
  50. .exec(async (res) => {
  51. const canvas = res[0].node
  52. // 渲染上下文
  53. const ctx = canvas.getContext('2d')
  54. // Canvas 画布的实际绘制宽高
  55. const width = res[0].width
  56. const height = res[0].height
  57. // 初始化画布大小
  58. const dpr = wx.getWindowInfo().pixelRatio
  59. //根据dpr调整
  60. // dpr 2 4
  61. // 3 6
  62. console.log("--dpr", dpr)
  63. canvas.width = width * dpr
  64. canvas.height = height * dpr
  65. let Ratio = canvas.width / 598
  66. this.canvas = canvas
  67. ctx.scale(dpr, dpr)
  68. ctx.clearRect(0, 0, width, height)
  69. //背景图片
  70. const bgImage = canvas.createImage()
  71. bgImage.src = this.configList.index_lvxing || 'https://image.hhlm1688.com//upload/组3833x_1742803627396.png'
  72. bgImage.onload = () => {
  73. ctx.drawImage(bgImage, 0, 0, width, height)
  74. //二维码图片
  75. const coderImage = canvas.createImage()
  76. coderImage.src = this.wxCodeImage
  77. coderImage.onload = () => {
  78. const x = 197 * Ratio / dpr
  79. const y = 562 * Ratio / dpr
  80. const size = 206 * Ratio / dpr
  81. ctx.drawImage(coderImage, x, y, size, size)
  82. uni.hideLoading()
  83. }
  84. }
  85. })
  86. },
  87. saveImg(){
  88. this.$authorize('scope.writePhotosAlbum').then((res) => {
  89. this.imgApi()
  90. })
  91. },
  92. imgApi() {
  93. wx.canvasToTempFilePath({
  94. x: 0,
  95. y: 0,
  96. width: this.canvas.width,
  97. height: this.canvas.height,
  98. canvas: this.canvas,
  99. success: (res) => {
  100. let tempFilePath = res.tempFilePath;
  101. this.saveImgToPhone(tempFilePath)
  102. },
  103. fail: (err) => {
  104. console.log('--canvasToTempFilePath--fail', err)
  105. }
  106. }, this);
  107. },
  108. saveImgToPhone(image) {
  109. /* 获取图片的信息 */
  110. uni.getImageInfo({
  111. src: image,
  112. success: function(image) {
  113. /* 保存图片到手机相册 */
  114. uni.saveImageToPhotosAlbum({
  115. filePath: image.path,
  116. success: function() {
  117. uni.showModal({
  118. title: '保存成功',
  119. content: '图片已成功保存到相册',
  120. showCancel: false
  121. });
  122. },
  123. complete(res) {
  124. console.log(res);
  125. }
  126. });
  127. }
  128. });
  129. }
  130. },
  131. }
  132. </script>
  133. <style scoped lang="scss">
  134. .page {
  135. background-color: #111317;
  136. height: 100vh;
  137. }
  138. .content {
  139. margin-top: 79rpx;
  140. flex-direction: column;
  141. }
  142. .btns {
  143. justify-content: space-between;
  144. margin-top: 53rpx;
  145. width: 598rpx;
  146. }
  147. .btn {
  148. display: inline-flex;
  149. width: 280rpx;
  150. height: 90rpx;
  151. font-size: 36rpx;
  152. line-height: 1;
  153. color: #FFFFFF;
  154. border-radius: 45rpx;
  155. margin: 0;
  156. &-back {
  157. background-color: #4E5053;
  158. }
  159. &-save {
  160. background-image: linear-gradient(to right, #02DED6, #05D9A2);
  161. }
  162. }
  163. </style>