珠宝小程序前端代码
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.

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