建材商城系统20241014
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.

265 lines
5.9 KiB

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