2017-06-25 03:26:27 +08:00
|
|
|
import window from 'window';
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { ajax } from 'Common/Links';
|
2020-07-30 03:49:41 +08:00
|
|
|
import { isNormal, pString } from 'Common/Utils';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { DEFAULT_AJAX_TIMEOUT, TOKEN_ERROR_LIMIT, AJAX_ERROR_LIMIT } from 'Common/Consts';
|
2020-07-22 04:22:58 +08:00
|
|
|
import { Notification } from 'Common/Enums';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { data as GlobalsData } from 'Common/Globals';
|
2020-07-22 04:22:58 +08:00
|
|
|
import { runHook } from 'Common/Plugins';
|
2016-08-17 06:01:20 +08:00
|
|
|
import * as Settings from 'Storage/Settings';
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { AbstractBasicPromises } from 'Promises/AbstractBasic';
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
class AbstractAjaxPromises extends AbstractBasicPromises {
|
2016-09-10 06:38:16 +08:00
|
|
|
oRequests = {};
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
|
|
|
super();
|
2016-09-10 06:38:16 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.clear();
|
|
|
|
}
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
clear() {
|
|
|
|
this.oRequests = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(sAction, bClearOnly) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.oRequests[sAction]) {
|
|
|
|
if (!bClearOnly && this.oRequests[sAction].abort) {
|
2020-07-22 04:22:58 +08:00
|
|
|
// this.oRequests[sAction].__aborted__ = true;
|
2016-08-17 06:01:20 +08:00
|
|
|
this.oRequests[sAction].abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.oRequests[sAction] = null;
|
|
|
|
delete this.oRequests[sAction];
|
2015-03-14 07:10:00 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return this;
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
ajaxRequest(action, isPost, timeOut, params, additionalGetString, fTrigger) {
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2020-07-30 03:49:41 +08:00
|
|
|
additionalGetString = undefined === additionalGetString ? '' : pString(additionalGetString);
|
2020-07-22 04:22:58 +08:00
|
|
|
|
|
|
|
let init = {
|
|
|
|
mode: 'same-origin',
|
|
|
|
cache: 'no-cache',
|
|
|
|
redirect: 'error',
|
|
|
|
referrerPolicy: 'no-referrer',
|
|
|
|
credentials: 'same-origin'
|
|
|
|
};
|
|
|
|
if (isPost) {
|
|
|
|
init.method = 'POST';
|
|
|
|
init.headers = {
|
|
|
|
// 'Content-Type': 'application/json'
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
|
|
|
};
|
|
|
|
params.XToken = Settings.appSettingsGet('token');
|
|
|
|
// init.body = JSON.stringify(params);
|
|
|
|
const formData = new window.FormData();
|
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
formData.append(key, params[key])
|
|
|
|
});
|
|
|
|
init.body = (new URLSearchParams(formData)).toString();
|
|
|
|
}
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
runHook('ajax-default-request', [action, params, additionalGetString]);
|
2015-04-29 04:51:20 +08:00
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
this.setTrigger(fTrigger, true);
|
2015-04-29 04:51:20 +08:00
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
if (window.AbortController) {
|
|
|
|
this.abort(action);
|
|
|
|
const controller = new window.AbortController();
|
|
|
|
setTimeout(() => controller.abort(), isNormal(timeOut) ? timeOut : DEFAULT_AJAX_TIMEOUT);
|
|
|
|
init.signal = controller.signal;
|
|
|
|
this.oRequests[action] = controller;
|
|
|
|
}
|
2016-05-24 01:33:01 +08:00
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
return window.fetch(ajax(additionalGetString), init)
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => {
|
|
|
|
this.abort(action, true);
|
2016-05-24 01:33:01 +08:00
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
if (!data) {
|
|
|
|
return Promise.reject(Notification.AjaxParse);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-05-24 01:33:01 +08:00
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
if (data.UpdateToken && GlobalsData.__APP__ && GlobalsData.__APP__.setClientSideToken) {
|
|
|
|
GlobalsData.__APP__.setClientSideToken(data.UpdateToken);
|
2020-01-01 01:44:41 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
/*
|
|
|
|
let isCached = false, type = '';
|
|
|
|
if (data && data.Time) {
|
|
|
|
isCached = pInt(data.Time) > microtime() - start;
|
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
// backward capability
|
2019-07-05 03:19:24 +08:00
|
|
|
switch (true) {
|
2016-08-17 06:01:20 +08:00
|
|
|
case 'success' === textStatus && data && data.Result && action === data.Action:
|
|
|
|
type = StorageResultType.Success;
|
|
|
|
break;
|
|
|
|
case 'abort' === textStatus && (!data || !data.__aborted__):
|
|
|
|
type = StorageResultType.Abort;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
type = StorageResultType.Error;
|
|
|
|
break;
|
|
|
|
}
|
2020-07-22 04:22:58 +08:00
|
|
|
runHook('ajax-default-response', [
|
2019-07-05 03:19:24 +08:00
|
|
|
action,
|
|
|
|
StorageResultType.Success === type ? data : null,
|
|
|
|
type,
|
|
|
|
isCached,
|
|
|
|
params
|
2016-08-17 06:01:20 +08:00
|
|
|
]);
|
2020-07-22 04:22:58 +08:00
|
|
|
*/
|
2016-08-17 06:01:20 +08:00
|
|
|
this.setTrigger(fTrigger, false);
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
if (!data.Result || action !== data.Action) {
|
2020-07-20 20:33:33 +08:00
|
|
|
if ([
|
2019-07-05 03:19:24 +08:00
|
|
|
Notification.AuthError,
|
|
|
|
Notification.AccessError,
|
|
|
|
Notification.ConnectionError,
|
|
|
|
Notification.DomainNotAllowed,
|
|
|
|
Notification.AccountNotAllowed,
|
|
|
|
Notification.MailServerError,
|
|
|
|
Notification.UnknownNotification,
|
|
|
|
Notification.UnknownError
|
2020-07-22 04:22:58 +08:00
|
|
|
].includes(data.ErrorCode)
|
2019-07-05 03:19:24 +08:00
|
|
|
) {
|
2020-07-22 04:22:58 +08:00
|
|
|
++GlobalsData.iAjaxErrorCount;
|
2015-05-06 00:41:15 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
if (Notification.InvalidToken === data.ErrorCode) {
|
|
|
|
++GlobalsData.iTokenErrorCount;
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (TOKEN_ERROR_LIMIT < GlobalsData.iTokenErrorCount) {
|
|
|
|
if (GlobalsData.__APP__ && GlobalsData.__APP__.loginAndLogoutReload) {
|
2016-08-17 06:01:20 +08:00
|
|
|
GlobalsData.__APP__.loginAndLogoutReload(false, true);
|
|
|
|
}
|
2016-05-24 01:33:01 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
if (data.ClearAuth || data.Logout || AJAX_ERROR_LIMIT < GlobalsData.iAjaxErrorCount) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (GlobalsData.__APP__ && GlobalsData.__APP__.clearClientSideToken) {
|
2016-08-17 06:01:20 +08:00
|
|
|
GlobalsData.__APP__.clearClientSideToken();
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
if (GlobalsData.__APP__ && !data.ClearAuth && GlobalsData.__APP__.loginAndLogoutReload) {
|
2016-08-17 06:01:20 +08:00
|
|
|
GlobalsData.__APP__.loginAndLogoutReload(false, true);
|
|
|
|
}
|
2015-05-06 00:41:15 +08:00
|
|
|
}
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
return Promise.reject(data.ErrorCode ? data.ErrorCode : Notification.AjaxFalse);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 04:22:58 +08:00
|
|
|
return data;
|
|
|
|
}).catch(err => {
|
|
|
|
if (err.name == 'AbortError') { // handle abort()
|
|
|
|
return Promise.reject(Notification.AjaxAbort);
|
|
|
|
}
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
getRequest(sAction, fTrigger, sAdditionalGetString, iTimeOut) {
|
2020-07-30 03:49:41 +08:00
|
|
|
sAdditionalGetString = undefined === sAdditionalGetString ? '' : pString(sAdditionalGetString);
|
2016-08-17 06:01:20 +08:00
|
|
|
sAdditionalGetString = sAction + '/' + sAdditionalGetString;
|
|
|
|
|
|
|
|
return this.ajaxRequest(sAction, false, iTimeOut, null, sAdditionalGetString, fTrigger);
|
|
|
|
}
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
postRequest(action, fTrigger, params, timeOut) {
|
|
|
|
params = params || {};
|
|
|
|
params.Action = action;
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return this.ajaxRequest(action, true, timeOut, params, '', fTrigger);
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { AbstractAjaxPromises, AbstractAjaxPromises as default };
|