CrowdfundPoster.js 5.1 KB
// packageOutput/pages/summary/components/CrowdfundPoster/CrowdfundPoster.js
import Draw from '~/utils/Draw'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
      observer(n) {
        this.check(n).then(res => {
          if (res) this.setData({
            poster: res,
            show: true
          })
          else this.begin()
        }).catch(() => this.begin())
      }
    },
    findKey: {
      type: String,
      value: 'activityId'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {},

  /**
   * 组件的方法列表
   */
  methods: {
    begin() {
      let {
        item
      } = this.data
      item.posterUrl && this.getImageInfo(item)
    },
    check(n) {
      const fileList = wx.$storage.get('fileList') || [],
        fn = wx.getFileSystemManager(),
        {
          findKey
        } = this.data
      return new Promise((resolve, reject) => {
        if (fileList.length) {
          fn.readdir({
            dirPath: wx.env.USER_DATA_PATH,
            success({
              files
            }) {
              console.log(files, '文件列表')
              if (files.length < 21) {
                let item = fileList.find(e => e[findKey] === n[findKey])
                if (item) {
                  const poster = files.find(s => s.includes(item.fileId))
                  wx.hideLoading()
                  if (poster) resolve(`${wx.env.USER_DATA_PATH}/${poster}`)
                  else reject()
                } else reject()
              } else {
                wx.$storage.rm('fileList')
                files.map(e => {
                  fn.unlink({
                    filePath: `${wx.env.USER_DATA_PATH}/${e}`
                  })
                })
                reject()
              }
            }
          })
        } else reject()
      })
    },
    getImageInfo(n, width, height) {
      wx.$api.showLoading('处理中')
      let that = this
      wx.getImageInfo({
        src: `${n.posterUrl}?v=${Date.now()}`,
        success: function (res) {
          console.log(res)
          // w = res.width
          // h = res.height
          that.init(n, res.width, res.height)
        },
        fail: function (res) {
          console.log(res)
          wx.$api.showToast('生成失败')
        }
      })
    },
    async init(n, ImgWidth, ImgHeight) {
      let proportion = ImgWidth / 247
      let actualHeight = ImgHeight / proportion
      console.log(actualHeight)
      n.activityName = n.activityName.length > 12 ? `${n.activityName.substring(0, 12)}...` : n.activityName
      const drawfun = new Draw({
            width: 279,
            // height: 554
            height: 82 + actualHeight
          },
          this
        ),
        {
          userInfo
        } = wx.$storage.get(),
        merchantName = userInfo.merchantName.length > 12 ? `${userInfo.merchantName.substring(0, 12)}...` : userInfo.merchantName,
        {
          findKey
        } = this.data,
        res = await drawfun.begin({
          imageList: [{
              x: 0,
              y: 0,
              width: 279,
              height: 66,
              url: 'https://img02.glsx.com.cn/weapp/resource/saas/business-assistant/output/poster-bg.png'
            },
            {
              x: 16,
              y: 70,
              width: 247,
              height: actualHeight,
              url: n.posterUrl
            }
          ],
          textList: [{
              x: 12,
              y: 14,
              fontSize: 14,
              color: '#2D2D33',
              text: merchantName,
              textBaseline: 'top'
            },
            {
              x: 12,
              y: 36,
              fontSize: 14,
              color: '#1B233D',
              text: n.activityName,
              textBaseline: 'top',
              fontWeight: 500
            },
            {
              x: 14 + drawfun.getTextWidth(n.activityName, 14) + 9,
              y: 37,
              fontSize: 10,
              color: '#4466F0',
              text: '众筹',
              textBaseline: 'top',
              border: {
                width: 1,
                color: '#4466F0',
                radius: 2,
                padding: [2, 5],
                opacity: 0.5
              }
            }
          ]
        })
      console.log(res)
      wx.hideLoading()
      this.setData({
        poster: res.path,
        show: true
      })
      let fileList = wx.$storage.get('fileList') || []
      fileList.push({
        fileId: res.fileId,
        path: res.path,
        [findKey]: n[findKey]
      })
      wx.$storage.set('fileList', fileList)
    },
    download() {
      const filePath = this.data.poster,
        _this = this
      wx.saveImageToPhotosAlbum({
        filePath,
        success() {
          wx.$api.showToast('保存成功')
        },
        fail(err) {
          const auth = _this.selectComponent('#auth')
          auth.check()
        }
      })
    },
    hide() {
      this.setData({
        show: false,
        poster: ''
      })
    },
    noScroll() {},
    authFail(e) {
      this.setData({
        showAuth: true
      })
    }
  }
})