推拿小程序前端代码仓库
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.

207 lines
4.3 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. <template>
  2. <view class="page">
  3. <navbar title="邀请好友" leftClick @leftClick="$utils.navigateBack" color="#fff" />
  4. <view class="flex flex-column content">
  5. <view style="width: 698rpx; height: 788rpx; border-radius: 16rpx; overflow: hidden;">
  6. <canvas id="myCanvas" canvas-id="firstCanvas1" type="2d" style="width: 100%; height: 100%;"></canvas>
  7. </view>
  8. <view class="tools">
  9. <button plain class="flex btn" @click="saveImg">
  10. 保存到本地
  11. </button>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import {
  18. mapState
  19. } from 'vuex'
  20. export default {
  21. name: 'Promotion',
  22. computed: {
  23. ...mapState(['userInfo']),
  24. },
  25. data() {
  26. return {
  27. wxCodeImage: '',
  28. baseUrl: 'https://image.hhlm1688.com/',
  29. canvas: {},
  30. }
  31. },
  32. onReady() {
  33. this.fetchQrCode()
  34. },
  35. methods: {
  36. async fetchQrCode() {
  37. try {
  38. // #ifdef MP-WEIXIN
  39. this.wxCodeImage = (await this.$fetch('getInviteCode'))?.url
  40. // #endif
  41. this.draw()
  42. } catch (err) {
  43. }
  44. },
  45. draw() {
  46. uni.showLoading({
  47. title: "拼命绘画中..."
  48. })
  49. wx.createSelectorQuery()
  50. .select('#myCanvas') // 绘制的canvas的id
  51. .fields({
  52. node: true,
  53. size: true
  54. })
  55. .exec(async (res) => {
  56. const canvas = res[0].node
  57. // 渲染上下文
  58. const ctx = canvas.getContext('2d')
  59. // Canvas 画布的实际绘制宽高
  60. const width = res[0].width
  61. const height = res[0].height
  62. // 初始化画布大小
  63. const dpr = wx.getWindowInfo().pixelRatio
  64. //根据dpr调整
  65. // dpr 2 4
  66. // 3 6
  67. console.log("--dpr", dpr)
  68. canvas.width = width * dpr
  69. canvas.height = height * dpr
  70. let Ratio = canvas.width / 698
  71. this.canvas = canvas
  72. ctx.scale(dpr, dpr)
  73. ctx.clearRect(0, 0, width, height)
  74. ctx.fillStyle = '#fff'
  75. ctx.fillRect(0, 0, canvas.width, canvas.height)
  76. ctx.fillStyle = '#000000';
  77. const fontSize = 32 * Ratio / dpr
  78. ctx.font = `${fontSize}px PingFangSC-regular`;
  79. const titleX = 285 * Ratio / dpr
  80. const titleY = 53 * Ratio / dpr
  81. ctx.fillText('邀请好友', titleX, titleY);
  82. const codeX = 216 * Ratio / dpr
  83. const codeY = 645 * Ratio / dpr
  84. // todo: fetch code
  85. ctx.fillText(`邀请码:${'YFY1688'}`, codeX, codeY);
  86. //二维码图片
  87. const coderImage = canvas.createImage()
  88. coderImage.src = this.wxCodeImage
  89. // coderImage.onload = () => {
  90. // const x = 158 * Ratio / dpr
  91. // const y = 188 * Ratio / dpr
  92. // const size = 382 * Ratio / dpr
  93. // ctx.drawImage(coderImage, x, y, size, size)
  94. uni.hideLoading()
  95. // }
  96. })
  97. },
  98. saveImg() {
  99. this.$authorize('scope.writePhotosAlbum').then((res) => {
  100. this.imgApi()
  101. })
  102. },
  103. imgApi() {
  104. wx.canvasToTempFilePath({
  105. x: 0,
  106. y: 0,
  107. width: this.canvas.width,
  108. height: this.canvas.height,
  109. canvas: this.canvas,
  110. success: (res) => {
  111. let tempFilePath = res.tempFilePath;
  112. this.saveImgToPhone(tempFilePath)
  113. },
  114. fail: (err) => {
  115. console.log('--canvasToTempFilePath--fail', err)
  116. }
  117. }, this);
  118. },
  119. saveImgToPhone(image) {
  120. /* 获取图片的信息 */
  121. uni.getImageInfo({
  122. src: image,
  123. success: function(image) {
  124. /* 保存图片到手机相册 */
  125. uni.saveImageToPhotosAlbum({
  126. filePath: image.path,
  127. success: function() {
  128. uni.showModal({
  129. title: '保存成功',
  130. content: '图片已成功保存到相册',
  131. showCancel: false
  132. });
  133. },
  134. complete(res) {
  135. console.log(res);
  136. }
  137. });
  138. }
  139. });
  140. }
  141. }
  142. }
  143. </script>
  144. <style lang="scss" scoped>
  145. .page {
  146. background-color: $uni-bg-color;
  147. min-height: 100vh;
  148. /deep/ .nav-bar__view {
  149. background-image: linear-gradient(#84A73F, #D8FF8F);
  150. }
  151. }
  152. .content {
  153. align-items: flex-start;
  154. padding: 48rpx 26rpx;
  155. }
  156. .image {
  157. width: 100%;
  158. height: 778rpx;
  159. }
  160. .tools {
  161. margin-top: 163rpx;
  162. width: 100%;
  163. padding: 0 56rpx;
  164. box-sizing: border-box;
  165. .btn {
  166. width: 100%;
  167. padding: 29rpx 0;
  168. color: $uni-text-color-inverse;
  169. font-size: 28rpx;
  170. line-height: 40rpx;
  171. border-radius: 49rpx;
  172. border: none;
  173. background-image: linear-gradient(to right, #84A73F, #D8FF8F);
  174. }
  175. }
  176. </style>