今天在APP中集成了指纹与手势登陆功能,本文章分两部分进行记录。一是手势功能的逻辑。二是代码实现。该APP是采用APPCAN开发,直接用其已写好的插件,调用至关接口就要能够了。html
一、在APP的我的中心加入手势设置的方法,用户只要登陆后进入我的中心就能够设置手势登陆功能。ajax
页面代码以下:json
<a href="#" class="my_info" onclick="createGesture()">
<div class="fl tuoch_setting">手势设置(仅本机有效)</div>
<div class="fr fa fa-angle-right ulev2 sc-text"></div>
</a>
onclick方法以下,单击后会打开手势设置界面。微信
isNeedVerifyBeforeCreate参数是在你重设手势时会不会较验以前的手势。我这选的是不校验。只要你登陆了,你的手势就能够从新设置。不论你以前用的什么手势。
function createGesture(){ var data={ isNeedVerifyBeforeCreate:false } uexGestureUnlock.create(JSON.stringify(data)); }
二、手势设置成功后咱们若是退出,在登陆界面会多一个手势登陆功能app
点击后会对以前设置好了的手势进行验证,页面代码以下:ide
<div id='gestureunlockdiv' class="resxinlang ub-img umhw2" onclick="uexGestureUnlockverify();">手势登陆</div>
onclick方法也是很是简单,只要调用插件uexGestureUnlock的接口就ok。lua
function uexGestureUnlockverify()
{
uexGestureUnlock.verify();
}
三、上面插件的方法在执行行咱们都要对插件uexGestureUnlock进行初始化,包括手势验证的回调方法cbVerify。还有设置手势和验证手势时的监听方法onEventOccururl
function initGestureUnlock() { uexGestureUnlock.onEventOccur=function(info){ if(JSON.parse(info).eventCode==11){ appcan.window.openToast("手势密码设置完成!",2000,5,0); } if(JSON.parse(info).eventCode==5){ appcan.window.openToast("手势密码验证成功!",2000,5,0); //登陆操做 //GestureLogin();
} if(JSON.parse(info).eventCode==4 || JSON.parse(info).eventCode==10) { uexGestureUnlock.cancel(); } //1 插件初始化 //2 开始手势密码验证 //3 手势密码验证失败 //4 验证过程被用户取消 //5 手势密码验证成功 //6 开始手势密码设置 //7 用户输入的密码不符合长度要求 //8 开始第二次输入手势密码 //9 两次输入的密码不一致 //10 手势密码设置被用户取消 //11 手势密码设置完成
} uexGestureUnlock.cbIsGestureCodeSet=function(info){ if(JSON.parse(info).result){ } } uexGestureUnlock.cbVerify=function(info){ if(JSON.parse(info).errorString=="在未设置密码的状况下进行验证密码操做"){ appcan.alert("请先进行手势设置!"); return false; } if(JSON.parse(info).isFinished=="false") { appcan.alert("手势密码登录失败!请使用帐号密码从新登陆!"); $("#gestureunlockdiv").hide(); } if(JSON.parse(info).isFinished) { GestureLogin(); } } uexGestureUnlock.cbCreate=function(info){ } var data={ backgroundImage:"../images/uexGestureUnlockbg.jpg", iconImage:"../images/uexGestureUnlockTouxiang.png", normalThemeColor:"#008cff", //普通主题色
selectedThemeColor:"#124dc3",//选中主题色
errorThemeColor:"#ff0000", //错误主题色
cancelVerificationButtonTitle:"取消", minimumCodeLength:5, verificationErrorPrompt:"验证错误!您还能够尝试%d次", verificationBeginPrompt:GestureLoginUserName } uexGestureUnlock.config(JSON.stringify(data)); uexGestureUnlock.isGestureCodeSet(); }
四、在验证手势成功后会调用自定义的方法 GestureLogin(),该方法中有一个变量GestureLoginUserName,就是登陆的用户名,这个用户名来自于咱们登陆成功时写入文件的用户名。spa
function GestureLogin(){ if(GestureLoginUserName=='') { appcan.window.openToast("请先使用用户名和密码登陆!",2000,5,0); return; } showMsg(); appcan.request.ajax({ url:GetServiceHostIp()+"LoginByUserId?userName="+escape(GestureLoginUserName), type:'get', dataType:'json', success:function(data, status, requestCode, response, xhr) { hidenMsg(); if(data.Status=="1") { appcan.window.publish('login',data.Data); appcan.locStorage.setVal('islog','1'); appcan.locStorage.setVal('userId',data.Data.userId); appcan.locStorage.setVal('userName',data.Data.userName); appcan.locStorage.setVal('IdCard',data.Data.idCard); appcan.locStorage.setVal('xmbm',data.Data.xmbm); appcan.window.publish('close_login','0'); uexWindow.evaluateScript("desktop", "0", "location.reload('../approval/desktop.html')"); uexWindow.open("desktop", 0, "../approval/desktop.html", 1, 0, 0, 0); uexWindow.close(); } else if(data.Status=="2"){ appcan.window.openToast(data.Message,2000,5,0); } else{ appcan.window.openToast("用户名或密码错误,错误代码:"+data.Status,2000,5,0); } } , error:function(xhr,status,errMessage){ hidenMsg(); appcan.window.openToast('登录失败,缘由:errMessage:'+errMessage+',xhr:'+xhr+',status:'+status,2000,5,0); } }); }
五、接下来的问题就来了,咱们如何将信息写入到文件中及读取呢,请看我下面提供的一组有用的插件接口插件
//建立文件
var file = uexFileMgr.create({ path:"wgt://data/1.txt" }); if(!file){ alert("建立失败!"); } //打开文件
var file = uexFileMgr.open({ path: "wgt://data/1.txt", mode: 3 }); if(!file){ alert("打开失败!"); } //判断文件是否存在
var ret = uexFileMgr.isFileExistByPath("wgt://data/test.txt"); alert(ret); //写文件
var file = uexFileMgr.open({ path: "wgt://data/1.txt", mode: 3 }); uexFileMgr.writeFile(file, 0, "test",function(err){ alert(err); }); //读文件
var file = uexFileMgr.open({ path: "wgt://data/1.txt", mode: 3 }); uexFileMgr.readFile(file, -1,0,function(error,data){ if(!error){ alert(data); }else{ alert("读取失败!"); } }); //关闭文件
var file = uexFileMgr.open({ path: "wgt://data/1.txt", mode: 3 }); var ret = uexFileMgr.closeFile(file); alert(ret);
舒适提示:如须要微信投票、点赞、注册的朋友能够联系我,百万水军为您服务