推拿小程序前端代码仓库
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.

248 lines
5.5 KiB

3 months ago
  1. <template>
  2. <view class="promotion">
  3. <navbar title="二维码"
  4. bgColor="rgb(235, 51, 0)"
  5. color="#fff"
  6. leftClick @leftClick="$utils.navigateBack" />
  7. <image
  8. class="image"
  9. :src="imagePath" mode="aspectFill"></image>
  10. <canvas id="myCanvas" type="2d" canvas-id="firstCanvas1"></canvas>
  11. <view class="btn"
  12. v-if="imagePath"
  13. @click="preservationImg(imagePath)">
  14. 保存到相册
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import { mapState } from 'vuex'
  20. import index from '../../uni_modules/uv-ui-tools'
  21. export default {
  22. name: 'Promotion',
  23. computed: {
  24. ...mapState(['userInfo', 'promotionUrl']),
  25. },
  26. data() {
  27. return {
  28. url: '',
  29. title: '',
  30. baseUrl: 'https://image.hhlm1688.com/',
  31. canvas: {},
  32. imagePath: 'https://image.hhlm1688.com/2025-03-06/32fbb8e2-160b-4cbf-9a49-a72de231de20.png',
  33. // imagePath: 'https://image.hhlm1688.com/2025-02-27/2fe7e417-54ad-4911-b9ba-84ac7d48e66d.png',
  34. // imagePath: 'https://image.hhlm1688.com/2025-02-26/6539d2fa-558d-47db-9681-ecffec5b6c5d.png',
  35. index : 0,
  36. }
  37. },
  38. onShow() {
  39. let that = this;
  40. // that.draw()
  41. // if(!that.promotionUrl){
  42. // that.getQrCode()
  43. // uni.showLoading({
  44. // title: "拼命绘画中..."
  45. // })
  46. // }
  47. // that.$store.commit('getUserInfo')
  48. },
  49. methods: {
  50. getQrCode() {
  51. // uni.showLoading({
  52. // title: "拼命绘画中..."
  53. // })
  54. let that = this;
  55. that.$api('getInviteCode', res => {
  56. if (res.code == 200) {
  57. that.url = res.result.url
  58. that.title = res.result.name
  59. // that.imagePath = that.$config.aliOss.url + res.result.url
  60. // that.$store.commit('setPromotionUrl', that.imagePath)
  61. that.draw()
  62. uni.hideLoading()
  63. }
  64. })
  65. },
  66. draw() {
  67. uni.showLoading({
  68. title: "拼命绘画中..."
  69. })
  70. let that = this;
  71. wx.createSelectorQuery()
  72. .select('#myCanvas') // 绘制的canvas的id
  73. .fields({
  74. node: true,
  75. size: true
  76. })
  77. .exec((res) => {
  78. console.log("----------1")
  79. const canvas = res[0].node
  80. // 渲染上下文
  81. const ctx = canvas.getContext('2d')
  82. // Canvas 画布的实际绘制宽高
  83. const width = res[0].width
  84. const height = res[0].height
  85. console.log(width, height);
  86. // 初始化画布大小
  87. const dpr = wx.getWindowInfo().pixelRatio
  88. //根据dpr调整
  89. // dpr 2 4
  90. // 3 6
  91. let Ratio = dpr * 2
  92. canvas.width = width * dpr
  93. canvas.height = height * dpr
  94. that.canvas = canvas
  95. ctx.scale(dpr, dpr)
  96. ctx.clearRect(0, 0, width, height)
  97. // ctx.fillStyle = '#fff'
  98. // ctx.fillRect(0, 0, canvas.width, canvas.height)
  99. //图片
  100. const bgImage = canvas.createImage()
  101. bgImage.src = that.configList.qr_code
  102. //二维码图片
  103. const coderImage = canvas.createImage()
  104. coderImage.src = '../static/logo.png'
  105. // coderImage.src = that.baseUrl + that.url
  106. let size = 0;
  107. console.log("----------2")
  108. bgImage.onload = () => {
  109. canvas.width = bgImage.width
  110. canvas.height = bgImage.height
  111. ctx.drawImage(bgImage,
  112. 0, 0, bgImage.width, bgImage.height)
  113. size = Math.floor(canvas.width / 3)
  114. ctx.drawImage(coderImage,
  115. canvas.width / 2 - (size / 2), canvas.height * 0.46, size, size)
  116. that.canvasToTempFilePath()
  117. }
  118. console.log("----------3")
  119. coderImage.onload = () => {
  120. ctx.drawImage(coderImage,
  121. canvas.width / 2 - (size / 2), canvas.height * 0.46, size, size)
  122. that.canvasToTempFilePath()
  123. }
  124. })
  125. },
  126. canvasToTempFilePath(){
  127. let that = this;
  128. that.index++
  129. if(that.index < 2){
  130. return
  131. }
  132. // 绘制完成后存储路径
  133. setTimeout(() => {
  134. wx.canvasToTempFilePath({
  135. x: 0,
  136. y: 0,
  137. width: that.canvas.width,
  138. height: that.canvas.height,
  139. canvas : that.canvas,
  140. success: (res) => {
  141. var tempFilePath = res.tempFilePath;
  142. that.imagePath = tempFilePath
  143. that.$store.commit('setPromotionUrl', res.tempFilePath)
  144. uni.hideLoading()
  145. that.load = false
  146. }
  147. });
  148. }, 200);
  149. },
  150. back() {
  151. uni.navigateBack(-1)
  152. },
  153. async preservationImg(img) {
  154. await this.$authorize('scope.writePhotosAlbum')
  155. this.imgApi(img);
  156. },
  157. imgApi(image) {
  158. /* 获取图片的信息 */
  159. uni.getImageInfo({
  160. src: image,
  161. success: function(image) {
  162. /* 保存图片到手机相册 */
  163. uni.saveImageToPhotosAlbum({
  164. filePath: image.path,
  165. success: function() {
  166. uni.showModal({
  167. title: '保存成功',
  168. content: '图片已成功保存到相册',
  169. showCancel: false
  170. });
  171. },
  172. complete(res) {
  173. console.log(res);
  174. }
  175. });
  176. }
  177. });
  178. },
  179. }
  180. }
  181. </script>
  182. <style lang="scss" scoped>
  183. .promotion {
  184. width: 100%;
  185. height: 100vh;
  186. background-color: $uni-color;
  187. }
  188. .image{
  189. width: 100%;
  190. height: calc(100vh - 300rpx);
  191. }
  192. #myCanvas {
  193. position: fixed;
  194. left: 100%;
  195. /* visibility: hidden */
  196. /* visibility: hidden; */
  197. /* margin-top: 100rpx; */
  198. width: 750rpx;
  199. height: calc(100vh - 120rpx);
  200. /* line-height: 20px; */
  201. background-color: rgba(255, 255, 255, 1);
  202. text-align: center;
  203. }
  204. .btn{
  205. position: fixed;
  206. // top: 58vh;
  207. bottom: 0;
  208. margin: 0;
  209. width: 750rpx;
  210. height: 100rpx;
  211. padding: 0;
  212. display: flex;
  213. justify-content: center;
  214. align-items: center;
  215. background-color: #fff;
  216. color: $uni-color;
  217. }
  218. </style>