Created
August 11, 2016 13:34
-
-
Save almsx/72fe01d8f44660687038e101df9d6d3e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var actInd = Ti.UI.createActivityIndicator({ | |
width : Ti.UI.SIZE, | |
height : Ti.UI.SIZE, | |
color : '#FFF', | |
message : 'Cargando, espere por favor', | |
font : Alloy.Globals.tipografia18Bold | |
}); | |
$.transparenteCargando.add(actInd); | |
actInd.show(); | |
$.txtCajaEmail.addEventListener('return', function(e) { | |
$.txtCajaPassword.focus(); | |
}); | |
$.txtCajaEmail.addEventListener('change', function(e) { | |
var minusculas = $.txtCajaEmail.value; | |
var asignaMinus = minusculas.toLowerCase(); | |
$.txtCajaEmail.value = asignaMinus; | |
}); | |
$.txtCajaPassword.addEventListener('return', function(e) { | |
$.txtCajaPassword.blur(); | |
}); | |
$.loginApp.addEventListener('singletap', function(e) { | |
var fuente = e.source.id; | |
var userApp = $.txtCajaEmail.value; | |
var passApp = $.txtCajaPassword.value; | |
Ti.API.info('selecciono fuente ' + fuente); | |
if (fuente == 'registroApparel' || fuente == 'txtRegistrarme') { | |
var registroLogin = Alloy.createController('registroApp').getView(); | |
registroLogin.open({ | |
modal : true | |
}); | |
}; | |
if (fuente == 'olvidePassword' || fuente == 'txtPasswordForget') { | |
var olvide = Alloy.createController('olvidePassword').getView(); | |
olvide.open({ | |
modal : true | |
}); | |
}; | |
if (fuente == 'cajaLogin' || fuente == 'loginTXT') { | |
$.txtCajaEmail.blur(); | |
$.txtCajaPassword.blur(); | |
login(userApp, passApp); | |
} | |
if (fuente == 'buttonMenuBack' || fuente == 'imageButtonBack') { | |
$.loginApp.close(); | |
}; | |
}); | |
function login(usuario, password) { | |
if (usuario.length != 0) { | |
var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; | |
if (filter.test(usuario)) { | |
if (password.length != 0) { | |
if (password.length >= 3) { | |
usuariosLogueo(usuario, password); | |
} else { | |
var a = Titanium.UI.createAlertDialog({ | |
title : 'Apparel', | |
message : 'Tu Contraseña debe tener 6 caracteres.', | |
buttonNames : ['OK'] | |
}); | |
a.show(); | |
a.addEventListener('click', function(e) { | |
if (e.index == 0) { | |
$.txtCajaPassword.focus(); | |
} | |
}); | |
} | |
} | |
} else { | |
var a = Titanium.UI.createAlertDialog({ | |
title : 'Apparel', | |
message : 'Formato de Correo electrónico incorrecto.', | |
buttonNames : ['OK'] | |
}); | |
a.show(); | |
a.addEventListener('click', function(e) { | |
if (e.index == 0) { | |
$.txtCajaEmail.focus(); | |
} | |
}); | |
} | |
} else { | |
var a = Titanium.UI.createAlertDialog({ | |
title : 'Apparel', | |
message : 'Debe ingresar su correo electrónico de manera correcta.', | |
buttonNames : ['OK'] | |
}); | |
a.show(); | |
a.addEventListener('click', function(e) { | |
if (e.index == 0) { | |
$.txtCajaEmail.focus(); | |
} | |
}); | |
} | |
}; | |
function usuariosLogueo(user, pass) { | |
$.transparenteCargando.visible = true; | |
var url = Alloy.Globals.servidorWebService + '/sessions'; | |
var xhr = Titanium.Network.createHTTPClient({ | |
onload : function(e) { | |
obj = this.responseText; | |
var nuevojson = JSON.parse(obj); | |
$.transparenteCargando.visible = false; | |
Ti.App.Properties.setBool('login', true); | |
Ti.App.Properties.setString('nombreU', nuevojson.user.name); | |
Ti.App.Properties.setString('idU', nuevojson.user.id); | |
Ti.App.Properties.setString('nombreU', nuevojson.user.name); | |
Ti.App.Properties.setString('promocionU', nuevojson.user.promotion_code); | |
Ti.App.Properties.setString('tokenU', nuevojson.user.token); | |
//var servicios = Alloy.createController('servicios').getView(); | |
var servicios = Alloy.createController('menuApp').getView(); | |
servicios.open(); | |
$.loginApp.close(); | |
//Ti.App.fireEvent('lanzadorApp'); | |
}, | |
onerror : function(e) { | |
obj = this.responseText; | |
var nuevojson = JSON.parse(obj); | |
$.transparenteCargando.visible = false; | |
var a = Titanium.UI.createAlertDialog({ | |
title : 'Apparel', | |
message : nuevojson.message, | |
buttonNames : ['OK'] | |
}); | |
a.show(); | |
}, | |
timeout : 10000000 | |
}); | |
var params = { | |
"email" : user, | |
"password" : pass | |
}; | |
Ti.API.info('estoy enviando para hacer login: \n' + JSON.stringify(params)); | |
xhr.open("POST", url); | |
xhr.send(params); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment