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

266 lines
6.0 KiB

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