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 === 'production') {
    if (!isAppEnv()) {
      token = global.sig
    } else {
      token = signAuth()
    }
  } else {
    token = 'MOCK'
  }
  const file = transferFile(imgData, 'text')
  const formData = new FormData()
  formData.append('appId', 'dtalk')
  formData.append('key', `oa/${entId + new Date().getTime()}`)
  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
  })
}
