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

增加访客模式下的一站式接口

上级 59f3ba83
...@@ -56,10 +56,12 @@ function getApi(apiUrl) { ...@@ -56,10 +56,12 @@ function getApi(apiUrl) {
}).then((res) => { }).then((res) => {
if (res.rbin && isJSON(binaryToStr(res.rbin))) { if (res.rbin && isJSON(binaryToStr(res.rbin))) {
const u = JSON.parse(binaryToStr(res.rbin)).url; const u = JSON.parse(binaryToStr(res.rbin)).url;
const g = encodeURIComponent(u); const newUrl = window.CONFIG.API_ACTION_PATH + '/apis/' + md5(u);
const newUrl = window.CONFIG.API_ACTION_PATH + '/apis/' + g; setLocalStorageTransformApi(apiUrl, u); // 存入缓存
setLocalStorageTransformApi(apiUrl, g); // 存入缓存 return {
return newUrl; transformApi: newUrl,
headersApi: u,
};
} else { } else {
return false; return false;
} }
...@@ -106,28 +108,37 @@ export async function getTransformApi(url) { // ...@@ -106,28 +108,37 @@ export async function getTransformApi(url) { //
apiUrl = url.replace(window.CONFIG.API_ACTION_PATH, ''); apiUrl = url.replace(window.CONFIG.API_ACTION_PATH, '');
} else { } else {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve(url); resolve({
transformApi: url,
headersApi: undefined,
});
}); });
} }
if (!visitor_client && (!t || t.length < 11 || !needTransform)) { // 没有token 或者没有开启二进制加密 且不是访客模式 直接返回 if (!visitor_client && (!t || t.length < 11 || !needTransform)) { // 没有token 或者没有开启二进制加密 且不是访客模式 直接返回
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
resolve(url); resolve({
transformApi: url,
headersApi: undefined,
});
}); });
} else { } else {
const storage = getLocalStorageTransformApi(); const storage = getLocalStorageTransformApi();
if(storage && storage[t] && storage[t][apiUrl]){ if(storage && storage[t] && storage[t][apiUrl]){
return new Promise((resolve) => { return new Promise((resolve) => {
resolve(window.CONFIG.API_ACTION_PATH + '/apis/' + storage[t][apiUrl]); const apis = storage[t][apiUrl];
resolve({
transformApi: window.CONFIG.API_ACTION_PATH + '/apis/' + md5(apis),
headersApi: apis,
})
}); });
} }
return new Promise((resolve) => { return new Promise((resolve) => {
if (storage[url]) {
resolve(storage[url]);
} else {
return getApi(apiUrl).then((g) => { return getApi(apiUrl).then((g) => {
resolve(g); resolve({
transformApi: g.transformApi,
headersApi: g.headersApi,
});
}); });
}
}); });
} }
} }
...@@ -107,14 +107,15 @@ export async function requestFileInfo(url, datas) { ...@@ -107,14 +107,15 @@ export async function requestFileInfo(url, datas) {
if (test) { if (test) {
console.log('转换前', trueUrl); console.log('转换前', trueUrl);
} }
const transformApi = await getTransformApi(trueUrl); const {transformApi, headersApi } = await getTransformApi(trueUrl);
if (test) { if (test) {
console.log('转换后' + transformApi); console.log('转换后' + transformApi, headersApi);
} }
return fetch(transformApi, { return fetch(transformApi, {
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
...getHeaders(url).headers, ...getHeaders(transformApi).headers,
apis: headersApi,
}, },
method: 'POST', method: 'POST',
credentials: 'omit', credentials: 'omit',
......
...@@ -269,6 +269,16 @@ export async function requestOrigin(url, data, options = {}){ ...@@ -269,6 +269,16 @@ export async function requestOrigin(url, data, options = {}){
} }
let trueUrl = (options.prefix || '') + url; let trueUrl = (options.prefix || '') + url;
trueUrl = await getTransformApi(trueUrl); trueUrl = await getTransformApi(trueUrl);
if(options.headers){
options.headers = {
...options.headers,
apis: trueUrl && trueUrl.indexOf('/safe/pub/getApis') > -1 ? window.onestopTransformApis : undefined,
}
}else{
options.headers = {
apis: trueUrl && trueUrl.indexOf('/safe/pub/getApis') > -1 ? window.onestopTransformApis : undefined,
}
}
delete options.prefix; delete options.prefix;
// POST请求 // POST请求
return fetchRequest(trueUrl, { return fetchRequest(trueUrl, {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论