worker-time-control/js/main.js

130 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-12-05 14:56:51 +00:00
const renewPeriod = localStorage.getItem('renewPeriod');
let intervalId, isCheckingToken;
2023-11-29 08:26:04 +00:00
2021-10-20 13:13:30 +00:00
function confirmReset() {
$(".confirm").removeClass('confirmKO');
$(".txtConfirm").empty();
}
2021-10-19 08:52:45 +00:00
function printError(msg){
2021-10-20 13:13:30 +00:00
confirmReset();
2021-10-19 08:52:45 +00:00
$(".txtConfirm").append(msg);
$(".confirm").addClass('confirmKO');
2021-10-20 13:13:30 +00:00
$(".confirm").fadeIn(200);
2021-10-19 08:52:45 +00:00
setTimeout(function() {
2021-10-20 13:13:30 +00:00
$(".confirm").fadeOut(200);
setTimeout(confirmReset, 200);
2023-08-23 10:54:58 +00:00
}, 2300);
2021-10-19 08:52:45 +00:00
}
2023-12-05 14:56:51 +00:00
function renewToken() {
2023-11-29 12:38:53 +00:00
$.post({
2023-12-05 14:56:51 +00:00
urlPath: 'vnUsers/renewToken',
2023-11-29 12:38:53 +00:00
processData: false,
success: function (data) {
2023-12-05 14:56:51 +00:00
localStorage.setItem("token", data.id);
2023-11-29 12:38:53 +00:00
localStorage.setItem("ttl", data.ttl);
2023-12-05 14:56:51 +00:00
localStorage.setItem("created", Date.now());
2023-11-29 12:38:53 +00:00
},
})
2023-11-29 08:26:04 +00:00
}
2023-12-05 14:56:51 +00:00
function getTokenConfig() {
const filter = {fields: ['renewInterval', 'renewPeriod']};
$.get({
urlPath: 'AccessTokenConfigs/findOne',
jsonData: filter,
processData: false,
success: function (data) {
if (!data) return;
localStorage.setItem('renewPeriod', data.renewPeriod);
clearInterval(intervalId);
2023-12-07 08:41:04 +00:00
intervalId = setInterval(() => checkValidity(), data.renewInterval * 1000);
2023-12-05 14:56:51 +00:00
},
})
}
function checkValidity() {
2024-02-12 11:56:52 +00:00
const created = +localStorage.getItem('created');
2023-12-05 14:56:51 +00:00
const ttl = localStorage.getItem('ttl');
if (isCheckingToken || !created) return;
isCheckingToken = true;
const renewPeriodInSeconds = Math.min(ttl, renewPeriod) * 1000;
const maxDate = created + renewPeriodInSeconds;
const now = new Date();
if (now.getTime() <= maxDate) return isCheckingToken = false;
renewToken();
isCheckingToken = false;
}
2021-10-19 08:52:45 +00:00
$.ajaxPrefilter(function(xhr) {
2023-11-30 08:25:32 +00:00
const orgErrorHandler = xhr.error;
const token = localStorage.getItem('token')
2022-12-12 09:21:02 +00:00
2021-10-19 08:52:45 +00:00
Object.assign(xhr, {
2024-01-30 13:44:44 +00:00
url: `/api/${xhr.urlPath}`,
2021-10-19 08:52:45 +00:00
headers: {
Authorization : token
2021-10-19 08:52:45 +00:00
},
2024-01-31 08:11:10 +00:00
timeout: 2000,
2021-10-19 08:52:45 +00:00
contentType: 'application/json; charset=utf-8',
dataType: 'json',
processData: false,
data: JSON.stringify(xhr.jsonData),
2021-10-20 13:13:30 +00:00
error: function(xhr, textStatus, err) {
if (orgErrorHandler) {
try {
orgErrorHandler(xhr, textStatus, err);
} catch (e) {
err = e;
}
}
2021-10-19 08:52:45 +00:00
2023-12-07 08:11:36 +00:00
if(xhr?.responseJSON?.error?.code == 'periodNotExceeded') return;
2023-12-05 14:56:51 +00:00
2021-10-19 08:52:45 +00:00
switch (textStatus){
case 'parsererror':
mensaje = 'Requested JSON parse failed';
break;
case 'timeout':
mensaje = 'Time out error';
break;
case 'abort':
mensaje = 'Ajax request aborted';
break;
case 'error':
2024-01-31 08:04:49 +00:00
if (xhr?.responseJSON?.error?.name == 'UserError') {
mensaje = xhr.responseJSON.error.message;
break;
}
2021-10-19 08:52:45 +00:00
switch (xhr.status){
case 0:
mensaje = 'Not connect: Verify Network';
break;
2024-01-31 08:04:49 +00:00
case 504:
mensaje = 'No se ha podido conectar con Salix, consulta con informática';
break;
2021-10-19 08:52:45 +00:00
case 555:
mensaje = JSON.parse(xhr.statusText).Message;
2024-01-31 08:04:49 +00:00
break;
2021-10-19 08:52:45 +00:00
default:
if (xhr.status >= 400 && xhr.status < 500)
mensaje = xhr.statusText;
else
mensaje = 'Ha ocurrido un error, consulta con informática';
}
break;
default:
mensaje = 'Ha ocurrido un error, consulta con informática';
}
printError(mensaje);
2023-12-05 14:56:51 +00:00
2021-10-19 08:52:45 +00:00
}
});
});
2023-11-29 12:38:53 +00:00
2023-12-05 14:56:51 +00:00
if(renewPeriod) getTokenConfig();