worker-time-control/js/main.js

101 lines
3.1 KiB
JavaScript
Raw Normal View History

2023-11-29 08:26:04 +00:00
var user = "timeControl";
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-11-29 08:26:04 +00:00
function renewToken() {
setInterval(function() {
const ttl = localStorage.getItem('ttl');
const password = localStorage.getItem('password');
if(ttl && password) {
const now = Date.now();
const signInTime = localStorage.getItem('signInTime');
const countdown = now - signInTime;
if(countdown > ttl) {
$.post({
urlPath: 'vnUsers/sign-in',
jsonData: {user, password},
processData: false,
success: function (data) {
localStorage.setItem("token", data.token);
localStorage.setItem("ttl", data.ttl);
localStorage.setItem("signInTime", Date.now());
},
})
}
}
},3600000)// 1 hour
}
2021-10-19 08:52:45 +00:00
$.ajaxPrefilter(function(xhr) {
2021-10-20 13:13:30 +00:00
var orgErrorHandler = xhr.error;
var token = localStorage.getItem('token')
2022-12-12 09:21:02 +00:00
2021-10-19 08:52:45 +00:00
Object.assign(xhr, {
url: `api/${xhr.urlPath}`,
2021-10-19 08:52:45 +00:00
headers: {
Authorization : token
2021-10-19 08:52:45 +00:00
},
timeout: 1000,
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
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':
switch (xhr.status){
case 0:
mensaje = 'Not connect: Verify Network';
break;
case 555:
mensaje = JSON.parse(xhr.statusText).Message;
break;
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);
}
});
});