2014-08-20 23:03:12 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
import window from 'window';
|
|
|
|
import _ from '_';
|
|
|
|
import $ from '$';
|
|
|
|
|
|
|
|
import {TOKEN_ERROR_LIMIT, AJAX_ERROR_LIMIT, DEFAULT_AJAX_TIMEOUT} from 'Common/Consts';
|
|
|
|
import {StorageResultType, Notification} from 'Common/Enums';
|
|
|
|
import {inArray, pInt, pString, isUnd} from 'Common/Utils';
|
|
|
|
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';
|
|
|
|
|
|
|
|
class AbstractAjaxRemote
|
2016-06-30 08:02:45 +08:00
|
|
|
{
|
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) {
|
|
|
|
const
|
|
|
|
fCall = () => {
|
|
|
|
if (StorageResultType.Success !== sType && GlobalsData.bUnload)
|
2014-10-06 02:37:31 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
sType = StorageResultType.Unload;
|
2014-10-06 02:37:31 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (StorageResultType.Success === sType && oData && !oData.Result)
|
2014-10-06 02:37:31 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
if (oData && -1 < inArray(oData.ErrorCode, [
|
|
|
|
Notification.AuthError, Notification.AccessError,
|
|
|
|
Notification.ConnectionError, Notification.DomainNotAllowed, Notification.AccountNotAllowed,
|
|
|
|
Notification.MailServerError, Notification.UnknownNotification, Notification.UnknownError
|
|
|
|
]))
|
|
|
|
{
|
|
|
|
GlobalsData.iAjaxErrorCount += 1;
|
|
|
|
}
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (oData && Notification.InvalidToken === oData.ErrorCode)
|
2014-10-06 02:37:31 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
GlobalsData.iTokenErrorCount += 1;
|
2014-10-06 02:37:31 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (TOKEN_ERROR_LIMIT < GlobalsData.iTokenErrorCount)
|
2014-10-06 02:37:31 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
if (GlobalsData.__APP__ && GlobalsData.__APP__.loginAndLogoutReload)
|
|
|
|
{
|
|
|
|
GlobalsData.__APP__.loginAndLogoutReload(false, true);
|
|
|
|
}
|
|
|
|
}
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (oData.ClearAuth || oData.Logout || AJAX_ERROR_LIMIT < GlobalsData.iAjaxErrorCount)
|
|
|
|
{
|
|
|
|
if (GlobalsData.__APP__ && GlobalsData.__APP__.clearClientSideToken)
|
2014-10-06 02:37:31 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
GlobalsData.__APP__.clearClientSideToken();
|
|
|
|
|
|
|
|
if (!oData.ClearAuth && GlobalsData.__APP__.loginAndLogoutReload)
|
|
|
|
{
|
|
|
|
GlobalsData.__APP__.loginAndLogoutReload(false, true);
|
|
|
|
}
|
2014-10-06 02:37:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
else if (StorageResultType.Success === sType && oData && oData.Result)
|
|
|
|
{
|
|
|
|
GlobalsData.iAjaxErrorCount = 0;
|
|
|
|
GlobalsData.iTokenErrorCount = 0;
|
|
|
|
}
|
2016-07-16 05:29:42 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
runHook('ajax-default-response', [sRequestAction, StorageResultType.Success === sType ? oData : null, sType, bCached, oRequestParameters]);
|
2014-10-06 02:37:31 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (fCallback)
|
2014-10-06 02:37:31 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
fCallback(
|
|
|
|
sType,
|
|
|
|
StorageResultType.Success === sType ? oData : null,
|
|
|
|
bCached,
|
|
|
|
sRequestAction,
|
|
|
|
oRequestParameters
|
|
|
|
);
|
2014-10-06 02:37:31 +08:00
|
|
|
}
|
2016-08-17 06:01:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
switch (sType)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (StorageResultType.Error === sType)
|
|
|
|
{
|
|
|
|
_.delay(fCall, 300);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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 = []) {
|
|
|
|
const
|
|
|
|
isPost = '' === sGetAdd,
|
|
|
|
headers = {},
|
|
|
|
start = (new window.Date()).getTime();
|
|
|
|
|
|
|
|
let
|
|
|
|
action = '';
|
|
|
|
|
|
|
|
params = params || {};
|
|
|
|
action = params.Action || '';
|
|
|
|
|
|
|
|
if (action && 0 < abortActions.length)
|
2014-10-06 02:37:31 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
_.each(abortActions, (actionToAbort) => {
|
|
|
|
if (this.oRequests[actionToAbort])
|
|
|
|
{
|
|
|
|
this.oRequests[actionToAbort].__aborted = true;
|
|
|
|
if (this.oRequests[actionToAbort].abort)
|
|
|
|
{
|
|
|
|
this.oRequests[actionToAbort].abort();
|
|
|
|
}
|
|
|
|
this.oRequests[actionToAbort] = null;
|
|
|
|
}
|
|
|
|
});
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (isPost)
|
2016-06-30 08:02:45 +08:00
|
|
|
{
|
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;
|
|
|
|
if (oData && oData.Time)
|
2014-10-06 02:37:31 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
cached = pInt(oData.Time) > (new window.Date()).getTime() - start;
|
2014-10-06 02:37:31 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (action && this.oRequests[action])
|
|
|
|
{
|
|
|
|
if (this.oRequests[action].__aborted)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
if (action && 0 < abortActions.length && -1 < inArray(action, abortActions))
|
2016-06-30 08:02:45 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
if (this.oRequests[action])
|
2016-06-30 08:02:45 +08:00
|
|
|
{
|
2016-08-17 06:01:20 +08:00
|
|
|
this.oRequests[action].__aborted = true;
|
|
|
|
if (this.oRequests[action].abort)
|
|
|
|
{
|
|
|
|
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-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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return this.ajaxRequest(fCallback, oParameters,
|
|
|
|
isUnd(iTimeout) ? DEFAULT_AJAX_TIMEOUT : pInt(iTimeout), sGetAdd, aAbortActions);
|
|
|
|
}
|
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
|
|
|
|
* @param {string} sMessage
|
|
|
|
* @param {string} sFileName
|
|
|
|
* @param {number} iLineNo
|
|
|
|
* @param {string} sLocation
|
|
|
|
* @param {string} sHtmlCapa
|
|
|
|
* @param {number} iTime
|
|
|
|
*/
|
|
|
|
jsError(fCallback, sMessage, sFileName, iLineNo, sLocation, sHtmlCapa, iTime) {
|
|
|
|
this.defaultRequest(fCallback, 'JsError', {
|
|
|
|
'Message': sMessage,
|
|
|
|
'FileName': sFileName,
|
|
|
|
'LineNo': iLineNo,
|
|
|
|
'Location': sLocation,
|
|
|
|
'HtmlCapa': sHtmlCapa,
|
|
|
|
'TimeOnPage': iTime
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {?Function} fCallback
|
|
|
|
* @param {string} sType
|
|
|
|
* @param {Array=} mData = null
|
|
|
|
* @param {boolean=} bIsError = false
|
|
|
|
*/
|
|
|
|
jsInfo(fCallback, sType, mData, bIsError = false) {
|
|
|
|
this.defaultRequest(fCallback, 'JsInfo', {
|
|
|
|
'Type': sType,
|
|
|
|
'Data': mData,
|
|
|
|
'IsError': bIsError ? '1' : '0'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
export {AbstractAjaxRemote, AbstractAjaxRemote as default};
|