import request from '@/utils/request'
import {getToken} from "@/utils/auth";

const apiUrl = `/knowledge/api`

// 图片流地址
export const imgUrl = apiUrl + '/image/'
export const uploadFileUrl = apiUrl + '/uploadFile'
// 文件下载地址，后接文件编码
export const downloadFileUrl = apiUrl + '/downloadFile'
export const qrCodeFileUrl = apiUrl + '/qrcodeFile'
export const getImage = (code) => {
  return request({
    url: imgUrl + '/' + code,
    method: 'get',
    responseType: 'blob',
  })
}

export const deleteImage = key => {
  return request({
    url: `${apiUrl}/deleteImage`,
    method: 'post',
    data: { "key": key }
  })
}

export const deleteDoc = key => {
  return request({
    url: `${apiUrl}/deleteDoc`,
    method: 'post',
    data: { "key": key }
  })
}

// 删除文件
export const deleteFile = fileCode => {
  return request({
    url: `${apiUrl}/deleteFile`,
    method: 'post',
    data: {
      "fileCode": fileCode
    }
  })
}

// 文件下载
export const downloadFile = (url,fileName) => {
  request({
    url: url,
    method: 'get',
    responseType: 'blob'
  }).then(res => {
    let a = document.createElement('a')
    a.style.display = 'none'
    a.setAttribute('download', fileName)
    a.href = URL.createObjectURL(new Blob([res.data]))
    document.body.appendChild(a)
    a.click()
    URL.revokeObjectURL(a.href)
    document.body.removeChild(a)
  })
}

export function getDownUrl(url) {
  let preViewUrl = downloadFileUrl +"?fileName="+ url   + '&Authorization=Bearer ' + getToken()
  return preViewUrl
}
