//var url = "http://172.16.198.221:3000/api/"; var url = "https://salix.verdnatura.es"; //var urlweb = "https://verdnatura.es/"; var LOGIN = "/api/Accounts/login"; var CHECKUUID = "Devices/checkUuid"; $(document).ready(function () { $(".loading").show(); FastClick.attach(document.body); $(".loginbtn").on("click", login_new); $("#txtuser").val(localStorage.getItem("user")); $("#txtpassword").val(localStorage.getItem("password")); //$("#txtpassword").on("click", login_new); checkLogin_new(); }); function checkLogin() { if (localStorage.getItem("user") == null) { $(".loading").hide(); } else { $("#txtuser").val(localStorage.getItem("user")); $("#txtpassword").val(localStorage.getItem("password")); login(); } } function checkLogin_new() { if (localStorage.getItem("user") == null) { $(".loading").hide(); } else { $("#txtuser").val(localStorage.getItem("user")); $("#txtpassword").val(localStorage.getItem("password")); login_new(); } } function login_new() { //navigator.notification.alert("User or password fail.", null, "Verdnatura says:", "¡ok!"); $(".loading").fadeIn(500); // var boundary = generateUUID(); const xhr = new XMLHttpRequest(); xhr.open("POST", url + LOGIN, true); xhr.setRequestHeader("version", "1"); xhr.setRequestHeader("application", "json"); xhr.setRequestHeader("Content-Type", "application/json"); let json = { user: $("#txtuser").val(), password: $("#txtpassword").val() } xhr.send(JSON.stringify(json)); xhr.onload = function () { var response = JSON.parse(xhr.response); if (response["token"]) { localStorage.setItem("user", $("#txtuser").val()); localStorage.setItem("password", $("#txtpassword").val()); localStorage.setItem("token", response["token"]); window.location = "main.html"; } else { navigator.notification.alert("User or password fail.", null, "Verdnatura says:", "¡ok!"); removeItems(); $(".loading").fadeOut(500); } }; xhr.onerror = function (e) { console.log(e) navigator.notification.alert("We could not authenticate you contact with Verdnatura spain", null, "Verdnatura says:", "¡ups!"); removeItems(); $(".loading").fadeOut(500); }; } function cancelLoading() { navigator.notification.alert("Esto te va un poco lento, ¿no?, ¿Has mirado tu conexión?", null, "Verdnatura dice:", "jopee, qué pesados.."); $(".loading").fadeOut(500); } function removeItems() { localStorage.removeItem("user"); localStorage.removeItem("password"); } function generateUUID() { // Public Domain/MIT var d = new Date().getTime(); if (typeof performance !== 'undefined' && typeof performance.now === 'function') { d += performance.now(); //use high-precision timer if available } return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); }