吉光研途前端代码仓库
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.

190 lines
4.3 KiB

  1. <template>
  2. <view class="page__view">
  3. <!-- 导航栏 -->
  4. <navbar leftClick @leftClick="$utils.navigateBack" bgColor="#4883F9" color="#FFFFFF" />
  5. <!-- height: 1184rpx; -->
  6. <view style="width: 750rpx; height: 890rpx; overflow: hidden;">
  7. <canvas id="myCanvas" canvas-id="firstCanvas1" type="2d" style="width: 100%; height: 100%;"></canvas>
  8. </view>
  9. <view class="flex bottom">
  10. <view class="flex">
  11. <button class="btn" @click="saveImg">保存到手机</button>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. data() {
  19. return {
  20. wxCodeImage: '',
  21. baseUrl: 'https://image.hhlm1688.com/',
  22. canvas: {},
  23. posterData: {},
  24. }
  25. },
  26. async onLoad() {
  27. uni.showLoading({
  28. title: '加载中...'
  29. });
  30. this.posterData = uni.getStorageSync('posterData')
  31. await this.fetchQrCode(this.posterData.path)
  32. uni.hideLoading();
  33. this.draw()
  34. },
  35. methods: {
  36. async fetchQrCode(path) {
  37. try {
  38. this.wxCodeImage = (await this.$fetch('getInviteCode', { path }))?.url
  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 / 750
  68. this.canvas = canvas
  69. ctx.scale(dpr, dpr)
  70. ctx.clearRect(0, 0, width, height)
  71. ctx.fillStyle = '#fff'
  72. ctx.fillRect(0, 0, canvas.width, canvas.height)
  73. // 海报图片
  74. const paperImage = canvas.createImage()
  75. paperImage.src = this.posterData.paperImage
  76. paperImage.onload = () => {
  77. const w = 750 * Ratio / dpr
  78. // const h = 1184 * Ratio / dpr
  79. const h = 890 * Ratio / dpr
  80. ctx.drawImage(paperImage, 0, 0, w, h)
  81. //二维码图片
  82. const coderImage = canvas.createImage()
  83. coderImage.src = this.wxCodeImage
  84. coderImage.onload = () => {
  85. // const x = 539 * Ratio / dpr
  86. const x = 560 * Ratio / dpr
  87. // const y = 987 * Ratio / dpr
  88. const y = 693 * Ratio / dpr
  89. const size = 162 * Ratio / dpr
  90. ctx.drawImage(coderImage, x, y, size, size)
  91. uni.hideLoading()
  92. }
  93. }
  94. })
  95. },
  96. saveImg() {
  97. this.$authorize('scope.writePhotosAlbum').then((res) => {
  98. this.imgApi()
  99. })
  100. },
  101. imgApi() {
  102. uni.showLoading({
  103. title: '保存中...'
  104. });
  105. wx.canvasToTempFilePath({
  106. x: 0,
  107. y: 0,
  108. width: this.canvas.width,
  109. height: this.canvas.height,
  110. canvas: this.canvas,
  111. success: (res) => {
  112. let tempFilePath = res.tempFilePath;
  113. this.saveImgToPhone(tempFilePath)
  114. },
  115. fail: (err) => {
  116. console.log('--canvasToTempFilePath--fail', err)
  117. uni.hideLoading();
  118. }
  119. }, this);
  120. },
  121. saveImgToPhone(image) {
  122. /* 获取图片的信息 */
  123. uni.getImageInfo({
  124. src: image,
  125. success: function(image) {
  126. /* 保存图片到手机相册 */
  127. uni.saveImageToPhotosAlbum({
  128. filePath: image.path,
  129. success: function() {
  130. uni.showModal({
  131. title: '保存成功',
  132. content: '图片已成功保存到相册',
  133. showCancel: false
  134. });
  135. },
  136. complete(res) {
  137. console.log(res);
  138. uni.hideLoading();
  139. }
  140. });
  141. }
  142. });
  143. }
  144. },
  145. }
  146. </script>
  147. <style scoped lang="scss">
  148. .page__view {
  149. background: $uni-bg-color-grey;
  150. }
  151. .bottom {
  152. position: fixed;
  153. left: 0;
  154. bottom: 0;
  155. width: 100vw;
  156. height: 154rpx;
  157. padding-bottom: env(safe-area-inset-bottom);
  158. background: #FFFFFF;
  159. .btn {
  160. padding: 20rpx 77rpx;
  161. font-size: 28rpx;
  162. color: #FFFFFF;
  163. background: #4883F9;
  164. border-radius: 14rpx;
  165. }
  166. }
  167. </style>