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

233 lines
4.9 KiB

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