import { isAppEnv, signAuth } from '../Bridge'
import { global } from '@/const/Global'
import { transferFile } from '../TransferFile'
import axios from 'axios'

export async function getImgUrl(imgData: string, entId: string) {
  let token = ''
  if (process.env.NODE_ENV === 'development') {
    token = 'MOCK'
  } else {
    if (!isAppEnv()) {
      token = global.sig
    } else {
      token = signAuth()
    }
  }
  const file = transferFile(imgData, 'text')
  const formData = new FormData()
  formData.append('appId', 'dtalk')
  formData.append('key', `oa/${entId + new Date().getTime()}.png`)
  formData.append('file', file)
  return axios({
    method: 'post',
    url: `/oss/upload`,
    data: formData,
    headers: {
      'content-type': 'multipart/form-data',
      'FZM-SIGNATURE': token
    }
  }).then((res: any) => {
    return res.data.data
  })
}
