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

205 lines
4.8 KiB

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