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

246 lines
5.4 KiB

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