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

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