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

240 lines
5.1 KiB

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