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

269 lines
6.0 KiB

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