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

优化代码

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