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

186 lines
3.9 KiB

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