in console-ui/src/globalLib.js [492:567]
async function Request(...allArgs) {
// 除了config外的传参
let [config, ...args] = allArgs;
// 处理前置中间件
config = handleMiddleWare.apply(this, [config, ...args, middlewareList]);
// 处理自定义url
config = handleCustomService.apply(this, [config, ...args]);
if (!config) return;
// xsrf
if (
config.type &&
config.type.toLowerCase() === 'post' &&
config.data &&
Object.prototype.toString.call(config.data) === '[object Object]' &&
!config.data.sec_token
) {
const sec_token = aliwareGetCookieByKeyName('XSRF-TOKEN');
sec_token && (config.data.sec_token = sec_token);
}
// 处理后置中间件
config = handleMiddleWare.apply(this, [config, ...args, middlewareBackList]);
const [url, paramsStr] = config.url.split('?');
const params = paramsStr ? paramsStr.split('&') : [];
const _LOGINPAGE_ENABLED = localStorage.getItem(LOGINPAGE_ENABLED);
let accessTokenInHeader = '';
if (_LOGINPAGE_ENABLED !== 'false') {
let token = {};
try {
token = JSON.parse(localStorage.token);
} catch (e) {
console.log('Token Error', localStorage.token, e);
goLogin();
}
const { accessToken = '' } = token;
accessTokenInHeader = accessToken;
}
return $.ajax(
Object.assign({}, config, {
type: config.type,
url: [url, params.join('&')].join('?'),
data: config.data || '',
dataType: config.dataType || 'json',
beforeSend(xhr) {
config.beforeSend && config.beforeSend(xhr);
},
headers: {
Authorization: localStorage.getItem('token') || undefined,
AccessToken: accessTokenInHeader,
},
})
).then(
success => {
return success;
},
error => {
// 处理403 forbidden
const { status, responseJSON = {} } = error || {};
if (responseJSON.message) {
const _errorcontent = responseJSON?.data ? ` : ${responseJSON.data}` : '';
Message.error(responseJSON.message + _errorcontent);
}
if (
[401, 403].includes(status) &&
['unknown user!', 'token invalid!', 'token expired!'].includes(responseJSON.message)
) {
goLogin();
}
return error;
}
);
}