worker-time-control/js/main.js

100 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-11-30 08:25:32 +00:00
const user = "timeControl";
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-11-29 12:38:53 +00:00
function signIn(user, password) {
$.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("password", password);
localStorage.setItem("signInTime", Date.now());
},
})
2023-11-29 08:26:04 +00:00
}
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, {
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);
}
});
});
2023-11-29 12:38:53 +00:00
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) signIn(user, password)
}
},3600000)// 1 hour