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

194 lines
4.1 KiB

5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
5 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 months ago
4 months ago
5 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>
  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. coderImage.onload = () => {
  90. ctx.drawImage(coderImage,
  91. canvas.width / 2 - 175, canvas.height / 2 - 100, 350, 350)
  92. this.canvasToTempFilePath()
  93. }
  94. }
  95. })
  96. },
  97. canvasToTempFilePath(){
  98. // 绘制完成后存储路径
  99. setTimeout(() => {
  100. wx.canvasToTempFilePath({
  101. x: 0,
  102. y: 0,
  103. width: this.canvas.width,
  104. height: this.canvas.height,
  105. canvas : this.canvas,
  106. success: (res) => {
  107. var tempFilePath = res.tempFilePath;
  108. this.imagePath = tempFilePath
  109. this.$store.commit('setPromotionUrl', res.tempFilePath)
  110. uni.hideLoading()
  111. this.load = false
  112. }
  113. });
  114. }, 200);
  115. },
  116. back() {
  117. uni.navigateBack(-1)
  118. },
  119. async preservationImg(img) {
  120. await this.$authorize('scope.writePhotosAlbum')
  121. this.imgApi(img);
  122. },
  123. imgApi(image) {
  124. /* 获取图片的信息 */
  125. uni.getImageInfo({
  126. src: image,
  127. success: function(image) {
  128. /* 保存图片到手机相册 */
  129. uni.saveImageToPhotosAlbum({
  130. filePath: image.path,
  131. success: function() {
  132. uni.showModal({
  133. title: '保存成功',
  134. content: '图片已成功保存到相册',
  135. showCancel: false
  136. });
  137. },
  138. complete(res) {
  139. console.log(res);
  140. }
  141. });
  142. }
  143. });
  144. },
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. .promotion {
  150. width: 100%;
  151. height: 100vh;
  152. background-color: $uni-color;
  153. }
  154. .image{
  155. width: 100%;
  156. height: calc(100vh - 200rpx);
  157. }
  158. #myCanvas {
  159. position: fixed;
  160. left: 100%;
  161. /* visibility: hidden */
  162. /* visibility: hidden; */
  163. /* margin-top: 100rpx; */
  164. width: 750rpx;
  165. height: calc(100vh - 120rpx);
  166. /* line-height: 20px; */
  167. background-color: rgba(255, 255, 255, 1);
  168. text-align: center;
  169. }
  170. </style>