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

247 lines
5.5 KiB

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