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

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

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