/**
 * 2022年7月19日
 * 钟是志
 * 一站式接口为了应付北电科的鉴权 按欢哥搞的 把接口地址进行一次转换
 * 禅道 28321 一站式接口越权处理
 * */

import { getToken } from '@/webPublic/one_stop_public/utils/getToken';
import { isJSON } from '@/webPublic/one_stop_public/2022beidianke/isJSON';
import { strToBinary } from '@/webPublic/one_stop_public/Base16/strToBinary';
import { getHeaders } from '@/webPublic/zyd_public/utils/getHeaders';

function getApi(apiUrl) {
  const data = {
    url: apiUrl,
    token: getToken(),
  };
  console.log(JSON.stringify(data));
  const datasCode = strToBinary(JSON.stringify(data));
  console.log(datasCode);
  let file = new File([datasCode], 'fileParams.jpeg', {
    type: 'image/jpeg',
  });

  let datasTrue = {
    fileParams: file,
    bin: true,
    rbin: true,
    token: getToken(),
  };
  const formData = new FormData();
  for (let key in datasTrue) {
    formData.append(key, datasTrue[key]);
  }
  console.log(datasTrue);

  fetch(window.CONFIG.API_ACTION_PATH + '/safe/pub/getApis', {
    headers: {
      Accept: 'application/json',
    },
    method: 'POST',
    credentials: 'omit',
    mode: 'cors',
    body: formData,
  })
    .then((res) => {
      return res.json();
    })
    .then((res) => {
      console.log(res);
    });
}


export function getTransformApi(apiUrl = '/DataObjApi/getFormDataPage') { // api 表示一站式接口后缀.
  const t = getToken();
  const openIsBinary = window.CONFIG?.IS_BINARY;
  if (!t || t.length < 11 || !openIsBinary) { // 没有token 或者没有开启二进制加密 直接返回 不需要转换
    return new Promise((resolve, reject) => {
      resolve(apiUrl);
    });
  } else {
    const storage = localStorage.getItem('oneStopTransformApi');
    if (storage && isJSON(storage)) {
      let d = JSON.parse(storage);
      if (d && d[api]) {
        return new Promise((resolve, reject) => {
          resolve(d[apiUrl]);
        });
      } else {
        return getApi(apiUrl);
      }
    } else {
      return getApi(apiUrl);
    }
  }
}

export function setTransformApi(api = '', transformApi = '') {
  const t = getToken();
}
