share.js 7.04 KB
// components/share/share.js
import util from '../../utils/util.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false,
      observer: function (newVal, oldVal) {
        this.check(newVal)
      }
    },
    data: {
      type: Object,
      value: {},
      observer: function (newVal, oldVal) {

      }
    },
    hideWx: Boolean,
    hideCancel: Boolean,
    goods: Boolean
  },
  ready() {

  },
  /**
   * 组件的初始数据
   */
  data: {
    imgWidth: 250,
    imgHeight: 420,
    tempFilePath: '',
    bgImg: './images/bg.png'
  },

  /** 
   * 组件的方法列表
   */
  methods: {
    /**
     * 检查
     */
    check(bool) {
      let data = this.properties.data
      if (bool) {
        this.getImgData(res => {
          if (res.qr) this.draw(res)
        })
      }
    },
    /**
     * 禁止滑动
     */
    noScroll() {},
    /**
     * 取消
     */
    cancel() {
      this.triggerEvent('hide', false)
    },
    saveImg() {
      wx.saveImageToPhotosAlbum({
        filePath: this.data.tempFilePath,
        success(e) {
          console.log(e)
          wx.showToast({
            title: '保存成功'
          })
        },
        fail(err) {
          console.log(err)
          wx.showToast({
            title: err
          })
        }
      })
    },
    /**
     * 保存
     */
    save() {
      let that = this
      wx.getSetting({
        success(res) {
          if (!res.authSetting['scope.writePhotosAlbum']) {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success() {
                that.saveImg()
              },
              fail(err) {
                console.log(err)
                wx.showToast({
                  title: '请在小程序设置允许保存图片',
                  icon: 'none',
                  mask: true,
                  success() {}
                })
              }
            })
          } else {
            that.saveImg()
          }
        }
      })
      wx.nextTick(() => this.cancel())
    },
    /**
     * 画!
     */
    draw(images) {
      const data = this.properties.data
      const ctx = wx.createCanvasContext(`img`, this)
      let isGoods = this.properties.goods
      const imgWidth = isGoods ? this.data.imgWidth : 315
      const imgHeight = isGoods ? this.data.imgHeight : 456
      util.getViewInfo('.choose-list', res => {
        util.getViewInfo('.bg', phoneInfo => {
          util.getViewInfo('.info', info => {
            let viewTop = (phoneInfo.height - res.height - info.height) / 2
            this.setData({
              viewTop: viewTop,
            })
          },this)
        }, this)
      }, this)
      let target = !isGoods ? 'draw_store_card' : 'draw_goods'
      ctx.clearRect(0, 0, imgWidth, imgHeight)
      this[target](images, data, ctx, imgWidth, imgHeight, res => {
        ctx.draw(true, setTimeout(() => this.getCanvasImg(), 500))
      })
    },
    /**
     * 生成门店
     */
    draw_store_card(images, data, ctx, imgWidth, imgHeight, callBack) {
      console.log(images)
      // 背景
      ctx.setTextAlign('left')
      ctx.setTextBaseline('top')
      ctx.setFillStyle('#fff')
      ctx.drawImage(this.data.bgImg, 0, 0, imgWidth, imgHeight)
      ctx.save()
      //头像
      ctx.beginPath()
      ctx.arc(69, 71, 35, 0, 2 * Math.PI)
      ctx.clip()
      ctx.drawImage(images.photo || '/images/default.png', 34, 36, 70, 70)
      ctx.restore()
      ctx.save()
      //名称
      ctx.setFillStyle('#333')
      let haveName = data.name
      if (haveName) {
        ctx.setFontSize(20)
        ctx.setStrokeStyle('#333')
        ctx.strokeText(haveName, 120, 37)
      }
      ctx.setFontSize(14)
      if (data.phone) ctx.fillText(`联系电话:${data.phone}`, 120, haveName ? 65 : 46)
      ctx.fillText(data.merchantName, 120, haveName ? 85 : 66, 161)
      //提示
      ctx.setTextAlign('center')
      ctx.fillText(data.tips || '我已上传好产品和视频,期待您的关注~', imgWidth / 2, 142, 245)
      //二维码
      ctx.drawImage(images.qr, imgWidth / 2 - 90, 205, 180, 180)
      return callBack()
    },
    /**生成商品 */
    draw_goods(images, data, ctx, imgWidth, imgHeight, callBack) {
      // 背景
      ctx.setTextAlign('left')
      ctx.setTextBaseline('top')
      ctx.setFillStyle('#fff')
      ctx.fillRect(0, 0, imgWidth, imgHeight)
      ctx.save()
      //商品图
      ctx.drawImage(images.photo, 0, 0, imgWidth, 311)
      ctx.setFontSize(14)
      ctx.setStrokeStyle('#333')
      ctx.strokeText(data.name, 14, 321, 222)
      ctx.setFontSize(12)
      ctx.setFillStyle('#666')
      ctx.fillText(`商户名称:${data.user.merchantName}`, 14, 353, 157)
      if (data.user.name || data.user.contactName) ctx.fillText(`联系人:${data.user.name || data.user.contactName}`, 14, 369, 157)
      if (data.user.phone) ctx.fillText(`联系电话:${data.user.phone}`, 14, 385, 157)
      //二维码
      ctx.drawImage(images.qr, 171, 347, 63, 62)
      return callBack()
    },
    /**
     * 画布生成图片
     */
    getCanvasImg() {
      let that = this
      wx.canvasToTempFilePath({
        x: 0,
        y: 0,
        width: that.data.imgWidth * 2,
        height: that.data.imgHeight * 2,
        canvasId: 'img',
        fileType: 'jpg',
        quality: 1,
        success(res) {
          wx.hideLoading()
          that.triggerEvent('init', res.tempFilePath)
          wx.nextTick(() => {
            that.setData({
              tempFilePath: res.tempFilePath,
              flag: true
            })
          })

        },
      }, this)
    },
    /**
     *  图片转成本地
     */
    getImgData(callBack) {
      let images = {}
      let goods = this.properties.goods
      let data = this.properties.data
      let photo = !goods ? data.photo || data.shopPhoto : data.productCoverUrl
      console.log(goods, data, photo)
      let that = this
      wx.showLoading({
        title: '加载中',
        mask: true,
        success(res) {
          // 获取二维码
          wx.getImageInfo({
            src: data.qr,
            success(res) {
              images.qr = res.path
              if (photo) {
                // 背景图
                wx.getImageInfo({
                  src: photo,
                  success(res) {
                    images.photo = res.path
                    return callBack(images)
                  },
                  fail(err) {
                    console.log('photo', err)
                    wx.hideLoading()
                    wx.showToast({
                      icon: 'none',
                      title: '图片生成失败,请稍后再试'
                    })
                  }
                })
              } else {
                return callBack(images)
              }
            },
            fail(err) {
              console.log('qr', err)
              wx.hideLoading()
              wx.showToast({
                icon: 'none',
                title: '图片生成失败,请稍后再试'
              })
            }
          })
        }
      })
    }
  }
})