draw.js 4.71 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-05-19 13:46:20
 * @LastEditors: yangming yangming@didihu.com.cn
 * @LastEditTime: 2023-08-30 18:11:11
 * @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, merchantLogo, marketingTitle, marketingSubtitle } = info
      if (!backgroundImage) return
      try {
        const res = await Promise.all([this.getImgInfo(backgroundImage), this.getImgInfo(merchantLogo)])
        const h = (res[0].dh / res[0].dw) * WIDTH
        this.logoInfo = { url: merchantLogo, ...logoInfo }
        const drawfun = new Drawing(document.getElementById('canvasId'), {
            width: WIDTH,
            height: h
          }),
          imageList = [
            {
              url: backgroundImage,
              x: 0,
              y: 0,
              width: WIDTH,
              height: h
            }
          ]
        if (info.merchantLogo) {
          imageList.push({ ...logoInfo, url: info.merchantLogo })
        }
        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(imageList, 'imageList')
        const canvasBase64 = await drawfun.begin({
          imageList,
          textList
        })
        return new Promise(async resolve => {
          resolve(await this.uploadImg(canvasBase64))
        })
      } catch (e) {
        console.log(e)
      }
    },
    async createMain(info, draggableList) {
      const style1 = draggableList[0]
      const style2 = draggableList[1]
      const drawfun = new Drawing(document.getElementById('canvasId'), {
        width: WIDTH,
        height: info.backgroundHeight
      })
      const imageList = [
        {
          url: info.backgroundImage,
          x: 0,
          y: 0,
          width: WIDTH,
          height: info.backgroundHeight
        }
      ]
      if (info.merchantLogo) {
        imageList.push({ ...info.logoInfo, url: info.merchantLogo })
      }
      const textList = [
        { x: style1.x, y: style1.y, fontSize: style1.size, width: style1.width, text: style1.content, color: style1.color, font: style1.font, shadow: style1.shadow },
        { x: style2.x, y: style2.y, fontSize: style2.size, width: style2.width, text: style2.content, color: style2.color, font: style2.font, shadow: style2.shadow }
      ]
      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() {}
}