敢为人鲜小程序前端代码仓库
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.

196 lines
4.2 KiB

1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year 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>
  12. </template>
  13. <script>
  14. import { mapState } from 'vuex'
  15. export default {
  16. name: 'Promotion',
  17. computed: {
  18. ...mapState(['userInfo', 'promotionUrl']),
  19. },
  20. data() {
  21. return {
  22. url: '',
  23. title: '',
  24. baseUrl: 'https://image.hhlm1688.com/',
  25. canvas: {},
  26. imagePath: '',
  27. }
  28. },
  29. onShow() {
  30. if(this.promotionUrl){
  31. this.imagePath = this.promotionUrl
  32. }else{
  33. this.getQrCode()
  34. }
  35. this.$store.commit('getUserInfo')
  36. },
  37. methods: {
  38. getQrCode() {
  39. this.$api('getInviteCode', res => {
  40. if (res.code == 200) {
  41. this.url = res.result.url
  42. this.title = res.result.name
  43. this.draw()
  44. }
  45. })
  46. },
  47. draw() {
  48. uni.showLoading({
  49. title: "拼命绘画中..."
  50. })
  51. wx.createSelectorQuery()
  52. .select('#myCanvas') // 绘制的canvas的id
  53. .fields({
  54. node: true,
  55. size: true
  56. })
  57. .exec((res) => {
  58. const canvas = res[0].node
  59. // 渲染上下文
  60. const ctx = canvas.getContext('2d')
  61. // Canvas 画布的实际绘制宽高
  62. const width = res[0].width
  63. const height = res[0].height
  64. console.log(width, height);
  65. // 初始化画布大小
  66. const dpr = wx.getWindowInfo().pixelRatio
  67. //根据dpr调整
  68. // dpr 2 4
  69. // 3 6
  70. let Ratio = dpr * 2
  71. canvas.width = width * dpr
  72. canvas.height = height * dpr
  73. this.canvas = canvas
  74. ctx.scale(dpr, dpr)
  75. ctx.clearRect(0, 0, width, height)
  76. // ctx.fillStyle = '#fff'
  77. // ctx.fillRect(0, 0, canvas.width, canvas.height)
  78. //图片
  79. const bgImage = canvas.createImage()
  80. bgImage.src = this.configList.qr_code
  81. //二维码图片
  82. const coderImage = canvas.createImage()
  83. coderImage.src = this.baseUrl + this.url
  84. bgImage.onload = () => {
  85. canvas.width = bgImage.width
  86. canvas.height = bgImage.height
  87. ctx.drawImage(bgImage,
  88. 0, 0, bgImage.width, bgImage.height)
  89. let size = Math.floor(canvas.width / 3)
  90. coderImage.onload = () => {
  91. ctx.drawImage(coderImage,
  92. canvas.width / 2 - (size / 2), canvas.height * 0.6, size, size)
  93. this.canvasToTempFilePath()
  94. }
  95. }
  96. })
  97. },
  98. canvasToTempFilePath(){
  99. // 绘制完成后存储路径
  100. setTimeout(() => {
  101. wx.canvasToTempFilePath({
  102. x: 0,
  103. y: 0,
  104. width: this.canvas.width,
  105. height: this.canvas.height,
  106. canvas : this.canvas,
  107. success: (res) => {
  108. var tempFilePath = res.tempFilePath;
  109. this.imagePath = tempFilePath
  110. this.$store.commit('setPromotionUrl', res.tempFilePath)
  111. uni.hideLoading()
  112. this.load = false
  113. }
  114. });
  115. }, 200);
  116. },
  117. back() {
  118. uni.navigateBack(-1)
  119. },
  120. async preservationImg(img) {
  121. await this.$authorize('scope.writePhotosAlbum')
  122. this.imgApi(img);
  123. },
  124. imgApi(image) {
  125. /* 获取图片的信息 */
  126. uni.getImageInfo({
  127. src: image,
  128. success: function(image) {
  129. /* 保存图片到手机相册 */
  130. uni.saveImageToPhotosAlbum({
  131. filePath: image.path,
  132. success: function() {
  133. uni.showModal({
  134. title: '保存成功',
  135. content: '图片已成功保存到相册',
  136. showCancel: false
  137. });
  138. },
  139. complete(res) {
  140. console.log(res);
  141. }
  142. });
  143. }
  144. });
  145. },
  146. }
  147. }
  148. </script>
  149. <style lang="scss" scoped>
  150. .promotion {
  151. width: 100%;
  152. height: 100vh;
  153. background-color: $uni-color;
  154. }
  155. .image{
  156. width: 100%;
  157. height: calc(100vh - 200rpx);
  158. }
  159. #myCanvas {
  160. position: fixed;
  161. left: 100%;
  162. /* visibility: hidden */
  163. /* visibility: hidden; */
  164. /* margin-top: 100rpx; */
  165. width: 750rpx;
  166. height: calc(100vh - 120rpx);
  167. /* line-height: 20px; */
  168. background-color: rgba(255, 255, 255, 1);
  169. text-align: center;
  170. }
  171. </style>