裂变星小程序-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.

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