invitePoster.vue 15 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
<template>
  <view class="invitePoster-box">
    <gloable-loading />

    <view class="poster-box flex-items-plus flex-column">
      <image
          class="header-img mar-top-10"
          :src="data.headImage"
      ></image>
      <label class="mar-top-30">发现一件好物,快来和我一起拼</label>
      <image
          class="poster-img mar-top-50"
          :src="data.image"
      ></image>
      <view class="flex-row-plus flex-sp-between mar-top-40">
        <view class="flex-column-plus">
          <label class="title-lab fs28">{{ data.productName }}</label>
          <view class="spellNum flex-items-plus font-color-C5AA7B mar-top-30 fs24">{{ data.person }}人团</view>
          <view class="flex-row-plus mar-top-20 flex-items">
            <label class="font-color-C5AA7B fs24">¥</label>
            <label class="font-color-C5AA7B fs36 mar-left-5">{{ data.price }}</label>
            <label class="font-color-999 fs26 mar-left-20">原价:¥{{ data.originalPrice }}</label>
          </view>
        </view>
        <view class="flex-column-plus flex-items-plus">
          <image
              style="width: 180upx;height: 180upx;"
              :src="qrcode"
          ></image>
          <label class="font-color-666">扫描二维码</label>
        </view>
      </view>
      <!-- #ifndef H5 -->
      <view
          @click="savePoster(1)"
          class="saveposter-but flex-items-plus mar-top-50"
      >保存海报
      </view>
      <!-- #endif -->
      <!-- #ifdef H5 -->
      <view
          @click="savePoster(2)"
          class="saveposter-but flex-items-plus mar-top-50"
      >预览海报
      </view>
      <!-- #endif -->
    </view>
    <view class="bor-reu-8 pad-bot-30 canvas-box">
      <canvas
          style="width: 375px; height: 560px;"
          canvas-id="posterCanvas"
          id="posterCanvas"
      ></canvas>
    </view>
  </view>
</template>

<script>
import GloableLoading from "../../components/diyLoading";
import { base64ToPath } from 'image-tools'

const NET = require('../../utils/request')
const API = require('../../config/api')
export default {
  components: {GloableLoading},
  data() {
    return {
      data: {},
      canvasWidth: 0,
      codeImg: '',
      codeImg2: '',
      headImage: '',
      image: '',
      qrcode: '',
      qrcodeimg: '',
      loadingQrcode: true
    };
  },
  onLoad(options) {
    this.data = JSON.parse(options.data)
    this.getShare()
  },
  methods: {
    savePoster(type) {
      if (this.loadingQrcode) {
        uni.showToast({
          icon: "none",
          title: "请稍等,正在生成二维码..."
        })
        return
      }
      this.$showLoading()
      if (type == 1) {
        // uni.showLoading({
        //   mask: true,
        // 	title:"生成图片中..."
        // })
        let that = this
        setTimeout(() => {
          // #ifdef H5 || MP-WEIXIN || APP-PLUS
          uni.canvasToTempFilePath({ //把画布转化成临时文件进行保存
            fileType: 'png', // 保存成的文件类型
            quality: 0, // 图片质量
            canvasId: 'posterCanvas', // 画布ID
            success: (res) => {
              this.$hideLoading()
              console.log(res.tempFilePath, 'test0000')
              that.saveDownload(res.tempFilePath)
            },
            fail: () => {
              uni.showToast({
                title: '保存失败,稍后再试',
                duration: 2000,
                icon: 'none'
              })
              this.$hideLoading()
              // uni.hideLoading();
            }
          })
          // #endif
          // #ifdef MP-ALIPAY
          const CanvasContext = my.createCanvasContext('posterCanvas');
          console.log('kahu', CanvasContext)
          CanvasContext.toTempFilePath({
            success: (res) => {
              console.log(2222)
              console.log(res.apFilePath)
              my.saveImage({
                url: res.apFilePath,
                success: res => {
                  console.log('saveImage', res)
                  // uni.hideLoading();
                  this.$hideLoading()
                  uni.showToast({
                    title: '图片保存成功~',
                    duration: 2000
                  })
                },
                fail: err => {
                  this.$hideLoading()
                  console.error('saveImage err', err)
                },
              })
            },
            fail: () => {
              this.$hideLoading()
              uni.showToast({
                title: '保存失败,稍后再试',
                duration: 2000,
                icon: 'none'
              })
              // uni.hideLoading();
            }
          })
          // #endif
        }, 5000)
      } else if (type == 2) {
        // uni.showLoading({
        //   mask: true,
        // 	title:"图片生成中..."
        // })
        let that = this
        setTimeout(() => {
          uni.canvasToTempFilePath({ //把画布转化成临时文件进行保存
            fileType: 'png', // 保存成的文件类型
            quality: 0, // 图片质量
            canvasId: 'posterCanvas', // 画布ID
            success: (res) => {
              uni.downloadFile({
                url: res.tempFilePath,//网络路径,下载下来
                success: (res1) => {
                  this.$hideLoading()
                  console.log(res1)
                  if (res1.statusCode === 200) {
                    // uni.hideLoading();
                    uni.showModal({
                      title: '提示',
                      content: '长按即可保存图片',
                      confirmText: "确定",
                      cancelText: '取消',
                      success: (res) => {
                        if (res.confirm) {
                          uni.previewImage({
                            current: res1.tempFilePath, // 当前显示图片的http链接
                            urls: [res1.tempFilePath] // 需要预览的图片http链接列表
                          })
                        }
                      }
                    })
                  }
                }
              })
            },
            fail: (err) => {
              this.$hideLoading()
              uni.showToast({
                title: '保存失败,稍后再试',
                duration: 2000,
                icon: 'none'
              })
            }
          })
        }, 5000)
      }
    },
    saveDownload(file) {
      const that = this
      // uni.showLoading({
      // mask: true,
      // 			title:"图片保存中..."
      // 		})
      this.$showLoading()
      console.log(file, 'test1')
      uni.getImageInfo({
        src: file,
        success: (res1) => {
          console.log(res1, 'test2')
          // 2-保存图片至相册
          console.log(2222)
          uni.saveImageToPhotosAlbum({ // 存成图片至手机
            filePath: res1.path, //画布保存的图片临时文件
            success: (res2) => {
              console.log(3333)
              this.$hideLoading()
              // uni.hideLoading();
              uni.showToast({
                title: '图片保存成功~',
                duration: 2000
              })
            },
            fail: (res3) => {
              this.$hideLoading()
              if (res3.errMsg === 'saveImageToPhotosAlbum:fail auth deny') {
                // that.$store.dispatch('SetPhoneShow', 1)
                uni.showToast({
                  title: '保存失败,请检查是否授权小程序保存图片!',
                  duration: 3000,
                  icon: 'none'
                })
              } else {
                uni.showToast({
                  title: '保存失败,稍后再试',
                  duration: 2000,
                  icon: 'none'
                })
              }
              // uni.hideLoading();
            }
          })
        }
      })
    },
    getShare() {
      NET.request(API.getShare,
          {
            collageId: this.data.collageId,
            orderId: this.data.orderId,
            productId: this.data.productId,
            skuId: this.data.skuId,
            type: 0
          },
          "GET").then(res => {
        // #ifndef MP-WEIXIN
        this.qrcode = res.data.qrcode
        // #endif
        // #ifdef MP-WEIXIN
        this.qrcode = res.data.xcxQrcode
        // #endif
        console.log(this.qrcode, 'this.qrcode')
        this.getCanvas()
        this.loadingQrcode = false
      }).catch(res => {
      })
    },
    getCanvas() {
      let that = this
      // #ifndef MP-WEIXIN
      uni.getImageInfo({
        src: that.data.headImage,
        success: function (image) {
          that.headImage = image.path
          uni.getImageInfo({
            src: that.data.image,
            success: function (image2) {
              that.image = image2.path
              uni.getImageInfo({
                src: that.qrcode,
                success: function (image3) {
                  that.qrcodeimg = image3.path
                  setTimeout(() => {
                    var ctx = uni.createCanvasContext('posterCanvas')
                    ctx.setFillStyle('#FFFFFF')
                    ctx.fillRect(0, 0, 375, 560)
                    // ctx.drawImage(that.data.headImage, 0, 0, 150, 100)
                    that.drawRound(ctx, 25, 160, 28, that.headImage)
                    ctx.setFontSize(14)
                    ctx.setFillStyle('#333333')
                    ctx.fillText('发现一件好物,快来和我一起拼', 89, 120)
                    ctx.drawImage(that.image, 25, 150, 310, 200)
                    ctx.setFontSize(14)
                    ctx.setFillStyle('#333333')
                    ctx.fillText(that.data.productName, 40, 390)
                    ctx.setFontSize(14)
                    ctx.setFillStyle('#C5AA7B')
                    ctx.fillText(that.data.person + '人团', 40, 430)
                    ctx.setFontSize(24)
                    ctx.setFillStyle('#C5AA7B')
                    ctx.fillText('¥' + that.data.price, 40, 470)
                    ctx.setFontSize(14)
                    ctx.setFillStyle('#999999')
                    ctx.fillText('原价:¥' + that.data.originalPrice, 120, 468)
                    ctx.drawImage(that.qrcodeimg, 245, 370, 80, 80)
                    ctx.setFontSize(14)
                    ctx.setStrokeStyle('#666666')
                    ctx.fillText('扫描二维码', 250, 468)
                    ctx.draw()
                  }, 1500)
                }
              });
            }
          });
        }
      });
      // #endif
      // #ifdef MP-WEIXIN

      uni.getImageInfo({
        src: that.data.image,
        success: function (image2) {
          that.image = image2.path
          uni.getImageInfo({
            src: that.qrcode,
            success: function (image3) {
              that.qrcodeimg = image3.path
              setTimeout(() => {
                var ctx = uni.createCanvasContext('posterCanvas')
                ctx.setFillStyle('#FFFFFF')
                ctx.fillRect(0, 0, 375, 560)
                // ctx.drawImage(that.data.headImage, 0, 0, 150, 100)
                // that.drawRound(ctx, 25, 160, 28, that.headImage)
                ctx.setFontSize(14)
                ctx.setFillStyle('#333333')
                ctx.fillText('发现一件好物,快来和我一起拼', 89, 120)
                ctx.drawImage(that.image, 25, 150, 310, 200)
                ctx.setFontSize(14)
                ctx.setFillStyle('#333333')
                let newStr = '',startPartIndex = 0
                for (let i = 0; i < that.data.productName.length; i++) {
                  if(i%14=== 0){
                    newStr += (that.data.productName.slice(startPartIndex,i) + '\n')
                    startPartIndex = i
                  }
                  if(i === that.data.productName.length-1) newStr += that.data.productName.slice(startPartIndex)
                }
                const productNameArr = newStr.split('\n').filter(item=>item);
                productNameArr.forEach((productNameItem,productNameIndex)=>{
                  let y = 390 + (productNameIndex * 22 )
                  ctx.fillText(productNameItem, 40, y)
                })
                ctx.setFontSize(14)
                ctx.setFillStyle('#C5AA7B')
                ctx.fillText(that.data.person + '人团', 40, 430 + ((productNameArr.length - 1 )* 22))
                ctx.setFontSize(24)
                ctx.setFillStyle('#C5AA7B')
                ctx.fillText('¥' + that.data.price, 40, 470 + ((productNameArr.length-1) * 22))
                ctx.setFontSize(14)
                ctx.setFillStyle('#999999')
                ctx.fillText('原价:¥' + that.data.originalPrice, 120, 468+((productNameArr.length-1) * 22))
                ctx.drawImage(that.qrcodeimg, 245, 370+ (productNameArr.length-1) * 22, 80, 80)
                ctx.setFontSize(14)
                ctx.setStrokeStyle('#666666')
                ctx.fillText('扫描二维码', 250, 468 + (productNameArr.length-1) * 22)
                ctx.draw()
              }, 1500)
            }
          });
        }
      });
      // #endif

    },
    exportImg() {
      uni.canvasToTempFilePath({
        fileType: "jpg",
        canvasId: 'couponQrcode',
        success: function (res) {
          let that = this
          // 在H5平台下,tempFilePath 为 base64
          // #ifndef H5
          that.codeImg = res.tempFilePath
          // #endif
          // #ifdef H5
          base64ToPath(res.tempFilePath).then(path => {
            that.codeImg = path
          }).catch(error => {
            console.error(error)
          });
          // #endif
        }
      })
    },
    drawRound(ctx, r, x, y, img) {
      ctx.save() // 保存之前的
      var r = r // 半径*屏幕分辨率比例
      var d = 2 * r // 直径
      var cx = x + r // 圆弧坐标x
      var cy = y + r // 圆弧坐标 y
      ctx.arc(cx, cy, r, 0, 2 * Math.PI)
      ctx.clip() // 裁剪
      ctx.drawImage(img, x, y, d, d) // 画头像
      ctx.restore() // 返回上一状态
    },
  }
}
</script>

<style lang="scss">
.qrcode {
  padding: 0upx 0 20upx 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.title {
  text-align: center;
}

.canvas-box {
  position: absolute;
  top: -88888rpx;
}

page {
  background-color: #F7F7F7;
}

.invitePoster-box {
  .bor-reu-8 {
    border-radius: 8rpx;
  }

  .poster-box {
    background-color: #FFFFFF;
    margin: 30rpx 30rpx;
    width: 690rpx;
    padding: 40rpx 34rpx;
    border-radius: 8rpx;

    .header-img {
      width: 100rpx;
      height: 100rpx;
      border-radius: 50%;
    }

    .poster-img {
      width: 100%;
      height: 414rpx;
    }

    .title-lab {
      width: 400rpx;
    }

    .spellNum {
      background-color: #FFEDDF;
      border-radius: 22rpx;
      width: 144rpx;
      height: 44rpx;
    }

    .code-img {
      width: 142rpx;
      height: 142rpx;
    }

    .saveposter-but {
      border-radius: 5rpx;
      background-color: #C5AA7B;
      color: #FFFFFF;
      width: 420rpx;
      height: 66rpx;
    }
  }
}
</style>