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

184 lines
3.9 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  1. <template>
  2. <view class="page">
  3. <view class="flex-colt content">
  4. <view style="width: 750rpx; height: 1125rpx;">
  5. <canvas id="myCanvas" canvas-id="firstCanvas1" type="2d" style="width: 100%; height: 100%;"></canvas>
  6. </view>
  7. <view class="flex-rowc btns">
  8. <button plain class="flex-rowc btn btn-save" @click="saveImg" >保存海报</button>
  9. </view>
  10. </view>
  11. </view>
  12. </template>
  13. <script setup>
  14. import { ref, reactive, computed } from 'vue'
  15. import { onLoad } from '@dcloudio/uni-app'
  16. import { useStore } from "vuex"
  17. import authorize from '@/utils/authorize.js'
  18. const store = useStore();
  19. const configList = computed(() => {
  20. return store.getters.configList
  21. })
  22. const url = ref()
  23. const canvasData = reactive({
  24. width: null,
  25. height: null,
  26. canvas: null
  27. })
  28. const savePath = ref()
  29. const draw = () => {
  30. uni.showLoading({
  31. title: "拼命绘画中..."
  32. })
  33. wx.createSelectorQuery()
  34. .select('#myCanvas') // 绘制的canvas的id
  35. .fields({
  36. node: true,
  37. size: true
  38. })
  39. .exec(async (res) => {
  40. const _canvas = res[0].node
  41. // 渲染上下文
  42. const ctx = _canvas.getContext('2d')
  43. // Canvas 画布的实际绘制宽高
  44. const width = res[0].width
  45. const height = res[0].height
  46. // 初始化画布大小
  47. const dpr = wx.getWindowInfo().pixelRatio
  48. //根据dpr调整
  49. // dpr 2 4
  50. // 3 6
  51. console.log("--dpr", dpr)
  52. _canvas.width = width * dpr
  53. _canvas.height = height * dpr
  54. let Ratio = _canvas.width / 750
  55. canvasData.canvas = _canvas
  56. ctx.scale(dpr, dpr)
  57. ctx.clearRect(0, 0, width, height)
  58. canvasData.width = _canvas.width
  59. canvasData.height = _canvas.height
  60. //背景图片
  61. const bgImage = _canvas.createImage()
  62. bgImage.src = configList.value.background_poster.paramValueImage
  63. bgImage.onload = () => {
  64. ctx.drawImage(bgImage, 0, 0, width, height)
  65. //二维码图片
  66. const coderImage = _canvas.createImage()
  67. coderImage.src = url.value
  68. coderImage.onload = () => {
  69. const x = 270 * Ratio / dpr
  70. const y = 672 * Ratio / dpr
  71. const size = 197 * Ratio / dpr
  72. ctx.drawImage(coderImage, x, y, size, size)
  73. uni.hideLoading()
  74. setTimeout(() => {
  75. wx.canvasToTempFilePath({
  76. x: 0,
  77. y: 0,
  78. width: _canvas.width,
  79. height: _canvas.height,
  80. canvas: _canvas,
  81. success: (res) => {
  82. let tempFilePath = res.tempFilePath;
  83. savePath.value = tempFilePath
  84. console.log('--savePath', savePath.value)
  85. // saveImgToPhone(tempFilePath)
  86. },
  87. fail: (err) => {
  88. console.log('--canvasToTempFilePath--fail', err)
  89. }
  90. });
  91. })
  92. }
  93. }
  94. })
  95. }
  96. const saveImg = () => {
  97. authorize('scope.writePhotosAlbum').then((res) => {
  98. saveImgToPhone(savePath.value)
  99. })
  100. }
  101. const saveImgToPhone = (image) => {
  102. /* 获取图片的信息 */
  103. uni.getImageInfo({
  104. src: image,
  105. success: function(image) {
  106. /* 保存图片到手机相册 */
  107. uni.saveImageToPhotosAlbum({
  108. filePath: image.path,
  109. success: function() {
  110. uni.showModal({
  111. title: '保存成功',
  112. content: '图片已成功保存到相册',
  113. showCancel: false
  114. });
  115. },
  116. complete(res) {
  117. console.log(res);
  118. }
  119. });
  120. }
  121. });
  122. }
  123. onLoad((options) => {
  124. // url.value = options.url
  125. // 暂时用配置项
  126. url.value = configList.value.share_qr_url.paramValueImage
  127. draw()
  128. })
  129. </script>
  130. <style scoped lang="scss">
  131. .page {
  132. background-color: #FFFFFF;
  133. width: 100vw;
  134. min-height: 100vh;
  135. .content {
  136. padding-top: 19rpx;
  137. }
  138. .btns {
  139. width: 100%;
  140. margin-top: 7rpx;
  141. }
  142. .btn {
  143. display: inline-block;
  144. border: none;
  145. width: 594rpx;
  146. height: auto;
  147. padding: 27rpx 0;
  148. border-radius: 94rpx;
  149. background-color: #FFBF60;
  150. color: #FFFFFF;
  151. font-size: 30rpx;
  152. line-height: 40rpx;
  153. }
  154. }
  155. </style>