index.js 6 KB
// packageCrowdfunding/components/createImage/index.js
import Draw from '~/utils/Draw'
import {
  IMG_URL
} from '~/utils/constantData';
Component({
  externalClasses: ['custom-class', 'class'],
  /**
   * 组件的属性列表
   */
  properties: {
    mode: {
      type: String,
      value: 'widthFix'
    },
    orderInfo: {
      type: Object,
      value: {},
      observer: function (n) {
        if (n) {
          const fn = wx.getFileSystemManager()
          fn.readdir({
            dirPath: wx.env.USER_DATA_PATH,
            success({
              files
            }) {
              files.length && files.map(e => {
                fn.unlink({
                  filePath: `${wx.env.USER_DATA_PATH}/${e}`
                })
              })
            }
          })
          let _this = this
          _this.drawOrderImg()
        }
      }
    }
  },

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


  /**
   * 组件的方法列表
   */
  methods: {

    // 画图
    async drawOrderImg(callBack) {
      let _this = this
      let {
        plateNo,
        payPrice,
        userName,
        goodsName,
        orderNumber,
        orderStatus,
        goodsNumber,
        createdDate,
        vehicleBrandName,
        vehicleShapeName,
        formContentFirst,
        vehicleSeriesName,
        formDescribeFirst,
        formContentSecond,
        formDescribeSecond,
        vehicleSubBrandName,
      } = this.data.orderInfo
      let valueX = 95,
        labelX = 17,
        fontSize = 14,
        valueEndX = 320 - fontSize / 2,
        textBaseline = 'top',
        canvasWidth = 337,
        canvasHeight = 561,
        carText = '',
        labelList = ['姓名:'],
        spacing = 0,
        textYList = [],
        labelEndX = 80,
        lineNum = 2,
        lineHeight = 24,
        valueList = [userName]

      const draw = new Draw({
        width: canvasWidth,
        height: canvasHeight
      }, _this)
      if (plateNo) {
        labelList.push('车牌号:')
        valueList.push(plateNo)
      }
      if (vehicleBrandName) {
        carText = `${vehicleBrandName || ''} ${vehicleSubBrandName || ''} ${vehicleSeriesName || ''} ${vehicleShapeName || ''}`
        labelList.push('品牌车型:')
        valueList.push(carText)
      }
      console.log(new Date(createdDate).format('yyyy年M月d日 hh:mm:ss'))
      labelList = [...labelList, '商品名称:', '支付时间:']
      // valueList = [...valueList, `${goodsName}${goodsNumber > 1 ? `*${goodsNumber}` : ''}` , new Date(createdDate).format('yyyy年M月d日 hh:mm:ss')]
      valueList = [...valueList, goodsName , new Date(createdDate).format('yyyy年M月d日 hh:mm:ss')]
      if (orderStatus != 10 && orderNumber) {
        labelList.push('订单号:')
        valueList.push(orderNumber)
      }
      if (formContentFirst) {
        labelList.push(formDescribeFirst)
        valueList.push(formContentFirst)
      }
      if (formContentSecond) {
        labelList.push(formDescribeSecond)
        valueList.push(formContentSecond)
      }

      labelList.push('支付定金')
      valueList.push(`${payPrice}元`)
      labelList.map((e, index) => {
        if (e == '商品名称:' && draw.getTextWidth(carText, fontSize) > valueEndX - valueX) spacing = lineHeight
        if ((e == formDescribeSecond && draw.getTextWidth(formContentFirst, fontSize) > valueEndX - valueX) || e === formDescribeSecond && (draw.getTextWidth(formDescribeFirst, fontSize) > labelEndX - labelX)) spacing = lineHeight * 2
        if (index == labelList.length - 1) textYList.push(493)
        else textYList.push(index * 32 + 109 + spacing)
      })
      const imgName = {
        8: '-2',
        10: '-3'
      } [orderStatus]

      let textList = [];
      (labelList || []).map((e, index) => {
        let y = textYList[index] + fontSize / 2
        let label = {
          text: e,
          x: labelX,
          y,
          color: '#999',
          fontSize,
          textBaseline
        }
        let value = {
          text: valueList[index],
          x: valueX,
          y,
          color: '#282829',
          fontSize,
          textBaseline,
          width: valueEndX
        }

        if (e === '商品名称:') {
          value.color = '#FC5B32'
        }

        if ((formDescribeFirst && e === formDescribeFirst) || (formDescribeSecond && e === formDescribeSecond) || (e === '品牌车型:')) {
          value.lineNum = lineNum
          value.lineHeight = lineHeight
          if ((formDescribeFirst && e === formDescribeFirst) || (formDescribeSecond && e === formDescribeSecond)) {
            label.lineNum = lineNum
            label.lineHeight = lineHeight
            label.width = labelEndX
            label.lastFont = ':'
          }
        }
        if (e === '支付定金') {
          label.color = '#282829'
          value.color = orderStatus == 8 ? '#3162E6' : '#F13C3D'
          value.fontWeight = '600'
          value.fontSize = 18
          value.y = textYList[index] + 5
        }

        if (orderStatus == 10) {
          textList.push({
            text: '订单号',
            x: labelX - 1,
            y: 24,
            color: '#282829',
            fontSize,
            textBaseline
          })
          textList.push({
            text: orderNumber || '',
            x: labelX - 1,
            y: 54,
            color: '#282829',
            fontSize,
            textBaseline
          })
        }
        textList.push(label)
        textList.push(value)
      })
      let res = await draw.begin({
        imageList: [{
          url: `${IMG_URL}crowdfunding/order-bg${imgName || ''}.png`,
          x: 0,
          y: 0,
          width: canvasWidth,
          height: canvasHeight,
        }],
        textList: textList,
        lineList: [{
          startX: labelX,
          startY: 478,
          endX: 320,
          endY: 478,
          width: 0.1,
          color: '#F1F1F1'
        }]
      })

      this.setData({
        imagePath: res.path
      })
      console.log(res.path)
    }

  }
})