2016-08-17 06:01:20 +08:00
|
|
|
import window from 'window';
|
|
|
|
import _ from '_';
|
|
|
|
import $ from '$';
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { TOKEN_ERROR_LIMIT, AJAX_ERROR_LIMIT, DEFAULT_AJAX_TIMEOUT } from 'Common/Consts';
|
|
|
|
import { StorageResultType, Notification } from 'Common/Enums';
|
2020-07-20 20:33:33 +08:00
|
|
|
import { pInt, pString, isUnd } from 'Common/Utils';
|
2019-07-05 03:19:24 +08:00
|
|
|
import { data as GlobalsData } from 'Common/Globals';
|
|
|
|
import { ajax } from 'Common/Links';
|
|
|
|
import { runHook } from 'Common/Plugins';
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import * as Settings from 'Storage/Settings';
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
class AbstractAjaxRemote {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
|
|
|
this.oRequests = {};
|
|
|
|
}
|
2014-08-20 23:03:12 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
|
|
|
* @param {?Function} fCallback
|
|
|
|
* @param {string} sRequestAction
|
|
|
|
* @param {string} sType
|
|
|
|
* @param {?AjaxJsonDefaultResponse} oData
|
|
|
|
* @param {boolean} bCached
|
|
|
|
* @param {*=} oRequestParameters
|
|
|
|
*/
|
|
|
|
defaultResponse(fCallback, sRequestAction, sType, oData, bCached, oRequestParameters) {
|
2019-07-05 03:19:24 +08:00
|
|
|
const fCall = () => {
|
|
|
|
if (StorageResultType.Success !== sType && GlobalsData.bUnload) {
|
|
|
|
sType = StorageResultType.Unload;
|
|
|
|
}
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (StorageResultType.Success === sType && oData && !oData.Result) {
|
|
|
|
if (
|
2020-07-20 20:33:33 +08:00
|
|
|
oData && [
|
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(oData.ErrorCode)
|
2019-07-05 03:19:24 +08:00
|
|
|
) {
|
|
|
|
GlobalsData.iAjaxErrorCount += 1;
|
|
|
|
}
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (oData && Notification.InvalidToken === oData.ErrorCode) {
|
|
|
|
GlobalsData.iTokenErrorCount += 1;
|
|
|
|
}
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (TOKEN_ERROR_LIMIT < GlobalsData.iTokenErrorCount) {
|
|
|
|
if (GlobalsData.__APP__ && GlobalsData.__APP__.loginAndLogoutReload) {
|
|
|
|
GlobalsData.__APP__.loginAndLogoutReload(false, true);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
}
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (oData.ClearAuth || oData.Logout || AJAX_ERROR_LIMIT < GlobalsData.iAjaxErrorCount) {
|
|
|
|
if (GlobalsData.__APP__ && GlobalsData.__APP__.clearClientSideToken) {
|
|
|
|
GlobalsData.__APP__.clearClientSideToken();
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (!oData.ClearAuth && GlobalsData.__APP__.loginAndLogoutReload) {
|
|
|
|
GlobalsData.__APP__.loginAndLogoutReload(false, true);
|
2014-10-06 02:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} else if (StorageResultType.Success === sType && oData && oData.Result) {
|
|
|
|
GlobalsData.iAjaxErrorCount = 0;
|
|
|
|
GlobalsData.iTokenErrorCount = 0;
|
|
|
|
}
|
2016-07-16 05:29:42 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
runHook('ajax-default-response', [
|
|
|
|
sRequestAction,
|
|
|
|
StorageResultType.Success === sType ? oData : null,
|
|
|
|
sType,
|
|
|
|
bCached,
|
|
|
|
oRequestParameters
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (fCallback) {
|
|
|
|
fCallback(
|
|
|
|
sType,
|
|
|
|
StorageResultType.Success === sType ? oData : null,
|
|
|
|
bCached,
|
|
|
|
sRequestAction,
|
|
|
|
oRequestParameters
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
switch (sType) {
|
2016-08-17 06:01:20 +08:00
|
|
|
case 'success':
|
|
|
|
sType = StorageResultType.Success;
|
|
|
|
break;
|
|
|
|
case 'abort':
|
|
|
|
sType = StorageResultType.Abort;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sType = StorageResultType.Error;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (StorageResultType.Error === sType) {
|
2016-08-17 06:01:20 +08:00
|
|
|
_.delay(fCall, 300);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2016-08-17 06:01:20 +08:00
|
|
|
fCall();
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
|
|
|
* @param {?Function} fResultCallback
|
|
|
|
* @param {Object} oParameters
|
|
|
|
* @param {?number=} iTimeOut = 20000
|
|
|
|
* @param {string=} sGetAdd = ''
|
|
|
|
* @param {Array=} aAbortActions = []
|
|
|
|
* @returns {jQuery.jqXHR}
|
|
|
|
*/
|
|
|
|
ajaxRequest(fResultCallback, params, iTimeOut = 20000, sGetAdd = '', abortActions = []) {
|
2019-07-05 03:19:24 +08:00
|
|
|
const isPost = '' === sGetAdd,
|
2016-08-17 06:01:20 +08:00
|
|
|
headers = {},
|
2019-07-05 03:19:24 +08:00
|
|
|
start = new window.Date().getTime();
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
let action = '';
|
2016-08-17 06:01:20 +08:00
|
|
|
|
|
|
|
params = params || {};
|
|
|
|
action = params.Action || '';
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (action && 0 < abortActions.length) {
|
2016-08-17 06:01:20 +08:00
|
|
|
_.each(abortActions, (actionToAbort) => {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.oRequests[actionToAbort]) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.oRequests[actionToAbort].__aborted = true;
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.oRequests[actionToAbort].abort) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.oRequests[actionToAbort].abort();
|
|
|
|
}
|
|
|
|
this.oRequests[actionToAbort] = null;
|
|
|
|
}
|
|
|
|
});
|
2016-06-30 08:02:45 +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');
|
|
|
|
}
|
|
|
|
|
|
|
|
const oDefAjax = $.ajax({
|
|
|
|
type: isPost ? 'POST' : 'GET',
|
|
|
|
url: ajax(sGetAdd),
|
|
|
|
async: true,
|
|
|
|
dataType: 'json',
|
|
|
|
data: isPost ? params : {},
|
|
|
|
headers: headers,
|
|
|
|
timeout: iTimeOut,
|
|
|
|
global: true
|
|
|
|
});
|
|
|
|
|
|
|
|
oDefAjax.always((oData, sType) => {
|
|
|
|
let cached = false;
|
2019-07-05 03:19:24 +08:00
|
|
|
if (oData && oData.Time) {
|
|
|
|
cached = pInt(oData.Time) > new window.Date().getTime() - start;
|
2014-10-06 02:37:31 +08:00
|
|
|
}
|
|
|
|
|
2020-01-01 01:44:41 +08:00
|
|
|
if (oData && oData.UpdateToken) {
|
|
|
|
if (GlobalsData.__APP__ && GlobalsData.__APP__.setClientSideToken) {
|
|
|
|
GlobalsData.__APP__.setClientSideToken(oData.UpdateToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (action && this.oRequests[action]) {
|
|
|
|
if (this.oRequests[action].__aborted) {
|
2016-08-17 06:01:20 +08:00
|
|
|
sType = 'abort';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.oRequests[action] = null;
|
|
|
|
}
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
this.defaultResponse(fResultCallback, action, sType, oData, cached, params);
|
|
|
|
});
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2020-07-20 20:33:33 +08:00
|
|
|
if (action && 0 < abortActions.length && abortActions.includes(action)) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.oRequests[action]) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.oRequests[action].__aborted = true;
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.oRequests[action].abort) {
|
2016-08-17 06:01:20 +08:00
|
|
|
this.oRequests[action].abort();
|
|
|
|
}
|
|
|
|
this.oRequests[action] = null;
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
|
|
|
|
this.oRequests[action] = oDefAjax;
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2014-08-20 23:03:12 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
// eslint-disable-next-line no-console
|
2019-06-29 08:17:29 +08:00
|
|
|
oDefAjax.catch(console.log);
|
2016-08-17 06:01:20 +08:00
|
|
|
return oDefAjax;
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2014-08-20 23:03:12 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
|
|
|
* @param {?Function} fCallback
|
|
|
|
* @param {string} sAction
|
|
|
|
* @param {Object=} oParameters
|
|
|
|
* @param {?number=} iTimeout
|
|
|
|
* @param {string=} sGetAdd = ''
|
|
|
|
* @param {Array=} aAbortActions = []
|
|
|
|
*/
|
|
|
|
defaultRequest(fCallback, sAction, oParameters, iTimeout, sGetAdd, aAbortActions) {
|
|
|
|
oParameters = oParameters || {};
|
|
|
|
oParameters.Action = sAction;
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
sGetAdd = pString(sGetAdd);
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
runHook('ajax-default-request', [sAction, oParameters, sGetAdd]);
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
return this.ajaxRequest(
|
|
|
|
fCallback,
|
|
|
|
oParameters,
|
|
|
|
isUnd(iTimeout) ? DEFAULT_AJAX_TIMEOUT : pInt(iTimeout),
|
|
|
|
sGetAdd,
|
|
|
|
aAbortActions
|
|
|
|
);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
|
|
|
* @param {?Function} fCallback
|
|
|
|
*/
|
|
|
|
noop(fCallback) {
|
|
|
|
this.defaultRequest(fCallback, 'Noop');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {?Function} fCallback
|
|
|
|
*/
|
|
|
|
getPublicKey(fCallback) {
|
|
|
|
this.defaultRequest(fCallback, 'GetPublicKey');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {?Function} fCallback
|
|
|
|
* @param {string} sVersion
|
|
|
|
*/
|
|
|
|
jsVersion(fCallback, sVersion) {
|
|
|
|
this.defaultRequest(fCallback, 'Version', {
|
|
|
|
'Version': sVersion
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { AbstractAjaxRemote, AbstractAjaxRemote as default };
|