推拿小程序前端代码仓库
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.

261 lines
5.9 KiB

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