localstorageTransform.js 4.3 KB
Newer Older
钟是志's avatar
钟是志 committed
1 2 3 4 5 6 7 8 9
/**
 * 2022年7月19日
 * 钟是志
 * 一站式接口为了应付北电科的鉴权 按欢哥搞的 把接口地址进行一次转换
 * 禅道 28321 一站式接口越权处理
 * */

import { getToken } from '@/webPublic/one_stop_public/utils/getToken';
import { isJSON } from '@/webPublic/one_stop_public/2022beidianke/isJSON';
钟是志's avatar
钟是志 committed
10
import { strToBinary } from '@/webPublic/one_stop_public/Base16/strToBinary';
钟是志's avatar
钟是志 committed
11 12
import { binaryToStr } from '@/webPublic/one_stop_public/Base16/binaryToStr';
import { getUrlInfo } from '@/webPublic/one_stop_public/DetailForAudit/utils';
13
import md5 from 'js-md5';
钟是志's avatar
钟是志 committed
14

钟是志's avatar
钟是志 committed
15 16 17 18 19
const urlInfo = getUrlInfo();
if(!!urlInfo.visitor){
  sessionStorage.setItem('visitor_client', JSON.stringify(true));
}
const isTest = urlInfo.test;
20
const visitor_client = sessionStorage.getItem('visitor_client') === 'true';
钟是志's avatar
钟是志 committed
21

钟是志's avatar
钟是志 committed
22

钟是志's avatar
钟是志 committed
23
function getApi(apiUrl) {
24
  const token = getToken();
钟是志's avatar
钟是志 committed
25 26 27
  const data = {
    url: apiUrl,
  };
28 29 30 31 32 33
  if((!token || token.length < 11) && visitor_client){
    data.client = 'client_' + md5(navigator.appVersion); // 访客模式
  }else{
    data.token = token;
  }

钟是志's avatar
钟是志 committed
34
  const datasCode = strToBinary(JSON.stringify(data));
钟是志's avatar
钟是志 committed
35

钟是志's avatar
钟是志 committed
36 37 38 39
  let file = new File([datasCode], 'fileParams.jpeg', {
    type: 'image/jpeg',
  });
  let datasTrue = {
钟是志's avatar
钟是志 committed
40
    fileParams: window.CONFIG.SAFE_TYPE === 2 ? datasCode : file,
钟是志's avatar
钟是志 committed
41
    bin: true,
钟是志's avatar
钟是志 committed
42
    rbin: !!window.CONFIG.rbin,
钟是志's avatar
钟是志 committed
43
    // token: getToken(),
钟是志's avatar
钟是志 committed
44 45
  };
  const formData = new FormData();
钟是志's avatar
钟是志 committed
46 47 48 49 50 51 52


  let u = window.CONFIG.API_ACTION_PATH + '/safe/pub/getApis';
  if(window.CONFIG.SAFE_TYPE === 2){
    u = window.CONFIG.API_ACTION_PATH + '/safe/pub';
    data.path = '/getApis';
  }
钟是志's avatar
钟是志 committed
53 54 55
  for (let key in datasTrue) {
    formData.append(key, datasTrue[key]);
  }
钟是志's avatar
钟是志 committed
56 57

  return fetch(u, {
钟是志's avatar
钟是志 committed
58 59 60 61
    headers: {
      Accept: 'application/json',
    },
    method: 'POST',
62
    credentials: 'include',
钟是志's avatar
钟是志 committed
63 64 65 66 67
    mode: 'cors',
    body: formData,
  })
    .then((res) => {
      return res.json();
钟是志's avatar
钟是志 committed
68 69 70
    }).then((res) => {
      if (res.rbin && isJSON(binaryToStr(res.rbin))) {
        const u = JSON.parse(binaryToStr(res.rbin)).url;
71 72 73 74 75 76
        const newUrl = window.CONFIG.API_ACTION_PATH + '/apis/' + md5(u);
        setLocalStorageTransformApi(apiUrl, u); // 存入缓存
        return {
          transformApi: newUrl,
          headersApi: u,
        };
钟是志's avatar
钟是志 committed
77 78 79
      } else {
        return false;
      }
钟是志's avatar
钟是志 committed
80 81 82 83
    });
}


钟是志's avatar
钟是志 committed
84 85 86 87 88 89 90 91 92 93 94
function getLocalStorageTransformApi(api = '') {
  const storage = localStorage.getItem('oneStopTransformApi');
  if (storage && isJSON(storage)) {
    let d = JSON.parse(storage);
    return d;
  } else {
    return {};
  }
}

function setLocalStorageTransformApi(api = '', transformApi = '') {
95 96 97 98
  let t = getToken();
  // if((!t || t.length < 11) &&  visitor_client){
  //   t = md5(navigator.appVersion);
  // }
钟是志's avatar
钟是志 committed
99 100 101 102 103 104 105 106 107 108 109
  let old = getLocalStorageTransformApi();
  if (old[t]) {
    old[t][api] = transformApi;
  } else {
    old = {};
    old[t] = {};
    old[t][api] = transformApi;
  }
  localStorage.setItem('oneStopTransformApi', JSON.stringify(old));
}

110 111

export async function getTransformApi(url) { //
钟是志's avatar
钟是志 committed
112
  let apiUrl = '';
113 114
  let needTransform = true;
  const t = getToken();
钟是志's avatar
钟是志 committed
115
  const TRANSFORM_BIN = window.CONFIG?.TRANSFORM_BIN;
116 117 118
  if(!TRANSFORM_BIN){
    needTransform = false;
  }
119
  if (needTransform && url && typeof url === 'string' && url.indexOf('/onestop') > -1 && url.includes(window.CONFIG.API_ACTION_PATH)) {
钟是志's avatar
钟是志 committed
120 121 122
    apiUrl = url.replace(window.CONFIG.API_ACTION_PATH, '');
  } else {
    return new Promise((resolve, reject) => {
123 124 125 126
      resolve({
        transformApi: url,
        headersApi: undefined,
      });
钟是志's avatar
钟是志 committed
127 128
    });
  }
129
  if (!visitor_client && (!t || t.length < 11 || !needTransform)) { // 没有token 或者没有开启二进制加密 且不是访客模式 直接返回
钟是志's avatar
钟是志 committed
130
    return new Promise((resolve, reject) => {
131 132 133 134
      resolve({
        transformApi: url,
        headersApi: undefined,
      });
钟是志's avatar
钟是志 committed
135
    });
钟是志's avatar
钟是志 committed
136
  } else {
钟是志's avatar
钟是志 committed
137 138 139
    const storage = getLocalStorageTransformApi();
    if(storage && storage[t] && storage[t][apiUrl]){
      return new Promise((resolve) => {
140 141 142 143 144
        const apis = storage[t][apiUrl];
        resolve({
          transformApi: window.CONFIG.API_ACTION_PATH + '/apis/' + md5(apis),
          headersApi: apis,
        })
钟是志's avatar
钟是志 committed
145 146 147 148
      });
    }
    return new Promise((resolve) => {
        return getApi(apiUrl).then((g) => {
149 150 151 152
          resolve({
            transformApi: g.transformApi,
            headersApi: g.headersApi,
          });
钟是志's avatar
钟是志 committed
153 154
        });
    });
钟是志's avatar
钟是志 committed
155 156
  }
}