2017-06-25 03:26:27 +08:00
|
|
|
import window from 'window';
|
2016-08-17 06:01:20 +08:00
|
|
|
import $ from '$';
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { ajax } from 'Common/Links';
|
2020-07-20 20:33:33 +08:00
|
|
|
import { microtime, isUnd, isNormal, pString, pInt } from 'Common/Utils';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { DEFAULT_AJAX_TIMEOUT, TOKEN_ERROR_LIMIT, AJAX_ERROR_LIMIT } from 'Common/Consts';
|
|
|
|
import { StorageResultType, Notification } from 'Common/Enums';
|
|
|
|
import { data as GlobalsData } from 'Common/Globals';
|
2016-08-17 06:01:20 +08:00
|
|
|
import * as Plugins from 'Common/Plugins';
|
|
|
|
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) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.oRequests[sAction].__aborted__ = true;
|
|
|
|
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) {
|
2017-06-25 03:26:27 +08:00
|
|
|
return new window.Promise((resolve, reject) => {
|
2016-08-17 06:01:20 +08:00
|
|
|
const start = microtime();
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
timeOut = isNormal(timeOut) ? timeOut : DEFAULT_AJAX_TIMEOUT;
|
|
|
|
additionalGetString = isUnd(additionalGetString) ? '' : pString(additionalGetString);
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (isPost) {
|
2016-08-17 06:01:20 +08:00
|
|
|
params.XToken = Settings.appSettingsGet('token');
|
|
|
|
}
|
2015-04-29 04:51:20 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
Plugins.runHook('ajax-default-request', [action, params, additionalGetString]);
|
2015-04-29 04:51:20 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.setTrigger(fTrigger, true);
|
2016-05-24 01:33:01 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
const oH = $.ajax({
|
|
|
|
type: isPost ? 'POST' : 'GET',
|
|
|
|
url: ajax(additionalGetString),
|
|
|
|
async: true,
|
|
|
|
dataType: 'json',
|
2019-07-05 03:19:24 +08:00
|
|
|
data: isPost ? params || {} : {},
|
2016-08-17 06:01:20 +08:00
|
|
|
timeout: timeOut,
|
|
|
|
global: true
|
|
|
|
}).always((data, textStatus) => {
|
2019-07-05 03:19:24 +08:00
|
|
|
let isCached = false,
|
2016-08-17 06:01:20 +08:00
|
|
|
errorData = null;
|
2016-05-24 01:33:01 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (data && data.Time) {
|
2016-08-17 06:01:20 +08:00
|
|
|
isCached = pInt(data.Time) > microtime() - start;
|
|
|
|
}
|
2016-05-24 01:33:01 +08:00
|
|
|
|
2020-01-01 01:44:41 +08:00
|
|
|
if (data && data.UpdateToken) {
|
|
|
|
if (GlobalsData.__APP__ && GlobalsData.__APP__.setClientSideToken) {
|
|
|
|
GlobalsData.__APP__.setClientSideToken(data.UpdateToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
// backward capability
|
|
|
|
let type = '';
|
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;
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
Plugins.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
|
|
|
]);
|
2016-05-24 01:33:01 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if ('success' === textStatus) {
|
|
|
|
if (data && data.Result && action === data.Action) {
|
2016-08-17 06:01:20 +08:00
|
|
|
data.__cached__ = isCached;
|
|
|
|
resolve(data);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else if (data && data.Action) {
|
2016-08-17 06:01:20 +08:00
|
|
|
errorData = data;
|
|
|
|
reject(data.ErrorCode ? data.ErrorCode : Notification.AjaxFalse);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-08-17 06:01:20 +08:00
|
|
|
errorData = data;
|
|
|
|
reject(Notification.AjaxParse);
|
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} else if ('timeout' === textStatus) {
|
2016-08-17 06:01:20 +08:00
|
|
|
errorData = data;
|
|
|
|
reject(Notification.AjaxTimeout);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else if ('abort' === textStatus) {
|
|
|
|
if (!data || !data.__aborted__) {
|
2016-08-17 06:01:20 +08:00
|
|
|
reject(Notification.AjaxAbort);
|
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-08-17 06:01:20 +08:00
|
|
|
errorData = data;
|
|
|
|
reject(Notification.AjaxParse);
|
2015-05-06 00:41:15 +08:00
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.oRequests[action]) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.oRequests[action] = null;
|
|
|
|
delete this.oRequests[action];
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2015-05-06 00:41:15 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.setTrigger(fTrigger, false);
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (errorData) {
|
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-20 20:33:33 +08:00
|
|
|
].includes(errorData.ErrorCode)
|
2019-07-05 03:19:24 +08:00
|
|
|
) {
|
2016-08-17 06:01:20 +08:00
|
|
|
GlobalsData.iAjaxErrorCount += 1;
|
2015-05-06 00:41:15 +08:00
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (Notification.InvalidToken === errorData.ErrorCode) {
|
2016-08-17 06:01:20 +08:00
|
|
|
GlobalsData.iTokenErrorCount += 1;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (errorData.ClearAuth || errorData.Logout || AJAX_ERROR_LIMIT < GlobalsData.iAjaxErrorCount) {
|
|
|
|
if (GlobalsData.__APP__ && GlobalsData.__APP__.clearClientSideToken) {
|
2016-08-17 06:01:20 +08:00
|
|
|
GlobalsData.__APP__.clearClientSideToken();
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (GlobalsData.__APP__ && !errorData.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
|
|
|
}
|
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
});
|
2015-03-14 07:10:00 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (oH) {
|
|
|
|
if (this.oRequests[action]) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.oRequests[action] = null;
|
|
|
|
delete this.oRequests[action];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.oRequests[action] = oH;
|
2016-05-24 01:33:01 +08:00
|
|
|
}
|
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) {
|
|
|
|
sAdditionalGetString = isUnd(sAdditionalGetString) ? '' : pString(sAdditionalGetString);
|
|
|
|
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 };
|