猫妈狗爸伴宠师小程序前端代码
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.

209 lines
4.2 KiB

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