提交 6c5757c4 authored 作者: 钟是志's avatar 钟是志

优化代码

上级 05a47c94
......@@ -8,16 +8,21 @@
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';
import { binaryToStr } from '@/webPublic/one_stop_public/Base16/binaryToStr';
import { getUrlInfo } from '@/webPublic/one_stop_public/DetailForAudit/utils';
const isTest = getUrlInfo().test || true;
function getApi(apiUrl) {
const data = {
url: apiUrl,
token: getToken(),
};
console.log(JSON.stringify(data));
if(isTest){
console.log('转换接口的参数为', data);
}
const datasCode = strToBinary(JSON.stringify(data));
console.log(datasCode);
let file = new File([datasCode], 'fileParams.jpeg', {
type: 'image/jpeg',
});
......@@ -26,15 +31,13 @@ function getApi(apiUrl) {
fileParams: file,
bin: true,
rbin: true,
token: getToken(),
// 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', {
return fetch(window.CONFIG.API_ACTION_PATH + '/safe/pub/getApis', {
headers: {
Accept: 'application/json',
},
......@@ -45,37 +48,79 @@ function getApi(apiUrl) {
})
.then((res) => {
return res.json();
})
.then((res) => {
console.log(res);
}).then((res) => {
if (res.rbin && isJSON(binaryToStr(res.rbin))) {
const u = JSON.parse(binaryToStr(res.rbin)).url;
const g = encodeURIComponent(u);
if(isTest){
console.log('转换接口返回的结果为: ', g);
}
const newUrl = window.CONFIG.API_ACTION_PATH + '/apis/' + g;
if(isTest){
console.log('转换后的接口地址为: ', newUrl);
}
// setLocalStorageTransformApi(apiUrl, g); // TODO 暂时不存入缓存
return newUrl;
} else {
return false;
}
});
}
export function getTransformApi(apiUrl = '/DataObjApi/getFormDataPage') { // api 表示一站式接口后缀.
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 = '') {
const t = getToken();
let old = getLocalStorageTransformApi();
if (old[t]) {
old[t][api] = transformApi;
} else {
old = {};
old[t] = {};
old[t][api] = transformApi;
}
localStorage.setItem('oneStopTransformApi', JSON.stringify(old));
}
export function getTransformApi(url) { // api 表示一站式接口后缀.
let apiUrl = '';
if (url && typeof url === 'string' && url.indexOf('/onestop') > -1 && url.includes(window.CONFIG.API_ACTION_PATH)) {
apiUrl = url.replace(window.CONFIG.API_ACTION_PATH, '');
} else {
return new Promise((resolve, reject) => {
resolve(url);
});
}
const t = getToken();
const openIsBinary = window.CONFIG?.IS_BINARY;
if (!t || t.length < 11 || !openIsBinary) { // 没有token 或者没有开启二进制加密 直接返回 不需要转换
return new Promise((resolve, reject) => {
resolve(apiUrl);
resolve(url);
});
} 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]);
const storage = getLocalStorageTransformApi();
if(storage && storage[t] && storage[t][apiUrl]){
return new Promise((resolve) => {
resolve(window.CONFIG.API_ACTION_PATH + '/apis/' + storage[t][apiUrl]);
});
} else {
return getApi(apiUrl);
}
return new Promise((resolve) => {
if (storage[url]) {
resolve(storage[url]);
} else {
return getApi(apiUrl);
return getApi(apiUrl).then((g) => {
resolve(g);
});
}
});
}
}
export function setTransformApi(api = '', transformApi = '') {
const t = getToken();
}
......@@ -103,9 +103,9 @@ export async function requestFileInfo(url, datas) {
return false;
}
const trueUrl = url && url.indexOf('http') > -1 ? url : queryApiActionPath() + url;
getTransformApi();
return fetch(trueUrl, {
let trueUrl = url && url.indexOf('http') > -1 ? url : queryApiActionPath() + url;
const transformApi = await getTransformApi(trueUrl);
return fetch(transformApi, {
headers: {
Accept: 'application/json',
...getHeaders(url).headers,
......
......@@ -136,7 +136,6 @@ export const request = (url, data, options = {}) => {
return giveBase16EnCode(data, url)
.then((newData) => {
if (typeof newData === 'object' && newData.url && newData.datas) {
console.log(newData);
return requestFileInfo(newData.url, newData.datas);
}
return requestOrigin(url, newData, options);
......
......@@ -28,15 +28,15 @@ export function getHeaders(url = '') {
res.headers.auth = gg5xxxxx(secretString);
res.headers.timestamp = timestamp;
if (isTest) {
console.log(
{
url: url,
secretString,
md5S: gg5xxxxx(secretString),
timestamp,
userId: userId,
},
);
// console.log(
// {
// url: url,
// secretString,
// md5S: gg5xxxxx(secretString),
// timestamp,
// userId: userId,
// },
// );
}
}
return res;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论