draw.js 4.76 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-05-19 13:46:20
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-07-14 21:38:37
 * @FilePath: \scrm-micro-home\src\mixins\gpt.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import apis from '@/api'
import Drawing from '@/utils/drawing'
const WIDTH = 244
const RATIO = WIDTH / 375
export default {
  data() {
    return {}
  },
  methods: {
    //获取图片宽度高度
    getImgInfo(src) {
      return new Promise(resolve => {
        if (!src) {
          resolve({ dw: 0, dh: 0, src })
        }
        const img = new Image()
        img.src = src
        img.onload = () => {
          resolve({ dw: img.width, dh: img.height, src })
        }
      })
    },
    //主图、内容图、logo合并图片
    async createBig(info, logoInfo, titleStyle, subTitleStyle) {
      console.log(info)
      this.loading = true
      const { backgroundImage, detailImage, merchantLog, marketingTitle, marketingSubtitle } = info
      if (!backgroundImage) return
      try {
        const res = await Promise.all([this.getImgInfo(backgroundImage), this.getImgInfo(detailImage), this.getImgInfo(merchantLog)])
        const h = (res[0].dh / res[0].dw) * WIDTH + (res[1].dh / res[1].dw) * WIDTH
        this.logoInfo = { url: merchantLog, ...logoInfo }
        const drawfun = new Drawing(document.getElementById('canvasId'), {
            width: WIDTH,
            height: h
          }),
          imageList = [
            {
              url: backgroundImage,
              x: 0,
              y: 0,
              width: WIDTH,
              height: (res[0].dh / res[0].dw) * WIDTH
            },
            {
              url: detailImage,
              x: 0,
              y: (res[0].dh / res[0].dw) * WIDTH,
              width: WIDTH,
              height: (res[1].dh / res[1].dw) * WIDTH
            },
            { ...this.logoInfo }
          ].filter(item => item.url)
        const styles = [titleStyle, subTitleStyle]
        for (const style of styles) {
          style.x = parseInt(style.x * RATIO)
          style.y = parseInt(style.y * RATIO)
          style.size = parseInt(style.size * RATIO)
          style.width = parseInt(style.width * RATIO)
        }
        let textList = [],
          title = {
            titleStyle,
            subTitleStyle
          }
        if (title) {
          marketingTitle.split(' ').forEach((item, index) => {
            textList.push({
              x: title.titleStyle.x,
              y: title.titleStyle.y + index * title.titleStyle.fontSize * 1.14,
              fontSize: title.titleStyle.fontSize,
              color: title.titleStyle.color,
              text: item,
              bold: title.titleStyle.bold
            })
          })
          new Array(marketingSubtitle).forEach((item, index) => {
            textList.push({
              x: title.subTitleStyle.x,
              y: title.subTitleStyle.y + title.titleStyle.fontSize * 1.14 * (textList.length - 1),
              fontSize: title.subTitleStyle.fontSize,
              color: title.subTitleStyle.color,
              text: item
            })
          })
        }
        console.log(title, textList)
        const canvasBase64 = await drawfun.begin({
          imageList,
          textList
        })
        return new Promise(async resolve => {
          resolve(await this.uploadImg(canvasBase64))
        })
      } catch (e) {
        console.log(e)
      }
    },
    async createMain(h, url, draggableList) {
      const style1 = draggableList[0]
      const style2 = draggableList[1]
      const drawfun = new Drawing(document.getElementById('canvasId'), {
        width: WIDTH,
        height: h
      })
      const imageList = [
        {
          url,
          x: 0,
          y: 0,
          width: WIDTH,
          height: h
        }
      ]
      const textList = [
        { x: style1.x, y: style1.y, fontSize: style1.size, width: style1.width, text: style1.content, color: style1.color, font: style1.font },
        { x: style2.x, y: style2.y, fontSize: style2.size, width: style2.width, text: style2.content, color: style2.color, font: style2.font }
      ]
      console.log(imageList, textList)
      const canvasBase64 = await drawfun.begin({
        imageList,
        textList
      })
      return new Promise(async resolve => {
        resolve(await this.uploadImg(canvasBase64))
      })
    },
    uploadImg(imgFile) {
      return new Promise((resolve, reject) => {
        const base64 = 'png' + '@' + imgFile.split(',')[1]
        apis.common.pictureUpload({ file: base64, fileType: 'image' }).then(res => {
          resolve(res)
        })
      })
    }
  },
  mounted() {}
}