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

215 lines
4.9 KiB

3 months ago
  1. <template>
  2. <view class="promotion">
  3. <navbar title="二维码"
  4. bgColor="#E3441A"
  5. color="#fff"
  6. leftClick @leftClick="$utils.navigateBack" />
  7. <view class="promotion-card">
  8. <image :src="userInfo.headImage" mode="aspectFill"
  9. class="headImage"></image>
  10. <image style="width: 100%;" :src="imagePath" mode="widthFix"></image>
  11. <!-- <view class="invitation-code">加油站: {{ title }}</view> -->
  12. <canvas id="myCanvas" type="2d" canvas-id="firstCanvas1"></canvas>
  13. <view class="uni-color-btn"
  14. @click="preservationImg(imagePath)">保存二维码</view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import { mapState } from 'vuex'
  20. export default {
  21. name: 'Promotion',
  22. computed: {
  23. ...mapState(['userInfo']),
  24. },
  25. data() {
  26. return {
  27. url: '',
  28. title: '',
  29. baseUrl: 'https://image.hhlm1688.com/',
  30. canvas: {},
  31. imagePath: '',
  32. }
  33. },
  34. onShow() {
  35. this.getQrCode()
  36. this.$store.commit('getUserInfo')
  37. },
  38. methods: {
  39. getQrCode() {
  40. this.$api('getInviteCode', res => {
  41. if (res.code == 200) {
  42. this.url = res.result.url
  43. this.title = res.result.name
  44. this.draw()
  45. }
  46. })
  47. },
  48. draw() {
  49. uni.showLoading({
  50. title: "拼命绘画中..."
  51. })
  52. wx.createSelectorQuery()
  53. .select('#myCanvas') // 绘制的canvas的id
  54. .fields({
  55. node: true,
  56. size: true
  57. })
  58. .exec((res) => {
  59. const canvas = res[0].node
  60. // 渲染上下文
  61. const ctx = canvas.getContext('2d')
  62. // Canvas 画布的实际绘制宽高
  63. const width = res[0].width
  64. const height = res[0].height
  65. // 初始化画布大小
  66. const dpr = wx.getWindowInfo().pixelRatio
  67. //根据dpr调整
  68. // dpr 2 4
  69. // 3 6
  70. let Ratio = dpr * 2
  71. console.log("bug", dpr)
  72. canvas.width = width * dpr
  73. canvas.height = height * dpr
  74. this.canvas = canvas
  75. ctx.scale(dpr, dpr)
  76. ctx.clearRect(0, 0, width, height)
  77. ctx.fillStyle = '#fff'
  78. ctx.fillRect(0, 0, canvas.width, canvas.height)
  79. //用户图片
  80. // const image = canvas.createImage()
  81. // image.onload = () => {
  82. // ctx.drawImage(image, 30, 18, 40, 40)
  83. // }
  84. // image.src = '/public/img/wechar_1.png'
  85. // image.src = this.userInfo.headImage
  86. ctx.fillStyle = 'black'
  87. ctx.font = '22px PingFangSC-regular';
  88. // 绘制用户姓名
  89. // let nickName = this.userInfo.nickName || '绘制用户姓名'
  90. // ctx.fillText(nickName, canvas.width / Ratio - nickName.length * 11, 40);
  91. // ctx.font = '18px PingFangSC-regular';
  92. // 绘制欢迎语
  93. let s = this.title || ''
  94. ctx.fillText(s, canvas.width / Ratio - s.length * 11, 50);
  95. //二维码图片
  96. const coderImage = canvas.createImage()
  97. coderImage.src = this.baseUrl + this.url
  98. coderImage.onload = () => {
  99. ctx.drawImage(coderImage,
  100. canvas.width / Ratio - 240 / 2, 100, 240, 240)
  101. }
  102. // 绘制完成后存储路径
  103. setTimeout(() => {
  104. wx.canvasToTempFilePath({
  105. x: 0,
  106. y: 0,
  107. width: this.canvas.width,
  108. height: this.canvas.height,
  109. canvas,
  110. success: (res) => {
  111. var tempFilePath = res.tempFilePath;
  112. this.imagePath = tempFilePath
  113. uni.hideLoading()
  114. }
  115. });
  116. }, 600);
  117. })
  118. },
  119. back() {
  120. uni.navigateBack(-1)
  121. },
  122. async preservationImg(img) {
  123. await this.$authorize('scope.writePhotosAlbum')
  124. this.imgApi(img);
  125. },
  126. imgApi(image) {
  127. /* 获取图片的信息 */
  128. uni.getImageInfo({
  129. src: image,
  130. success: function(image) {
  131. /* 保存图片到手机相册 */
  132. uni.saveImageToPhotosAlbum({
  133. filePath: image.path,
  134. success: function() {
  135. uni.showModal({
  136. title: '保存成功',
  137. content: '图片已成功保存到相册',
  138. showCancel: false
  139. });
  140. },
  141. complete(res) {
  142. console.log(res);
  143. }
  144. });
  145. }
  146. });
  147. },
  148. }
  149. }
  150. </script>
  151. <style lang="scss" scoped>
  152. .promotion {
  153. width: 100%;
  154. height: 100vh;
  155. background-color: $uni-color;
  156. .promotion-card {
  157. width: 90%;
  158. margin: 140rpx auto 0rpx auto;
  159. box-shadow: 0rpx 0rpx 15rpx rgba(0, 0, 0, .2);
  160. border-radius: 40rpx;
  161. padding: 40rpx 30rpx;
  162. box-sizing: border-box;
  163. background-color: #fff;
  164. position: relative;
  165. padding-top: 100rpx;
  166. .headImage{
  167. position: absolute;
  168. height: 180rpx;
  169. width: 180rpx;
  170. border-radius: 50%;
  171. top: -80rpx;
  172. left: 50%;
  173. transform: translate(-50%);
  174. border: 10rpx solid #fff;
  175. box-shadow: 0 0 10rpx 10rpx #00000013;
  176. }
  177. }
  178. }
  179. #myCanvas {
  180. position: fixed;
  181. left: 100%;
  182. /* visibility: hidden */
  183. /* visibility: hidden; */
  184. /* margin-top: 100rpx; */
  185. margin: 68rpx auto;
  186. width: 750rpx;
  187. height: 750rpx;
  188. /* line-height: 20px; */
  189. background-color: rgba(255, 255, 255, 1);
  190. text-align: center;
  191. }
  192. </style>