import { Injectable } from '@angular/core'; import { Platform, ActionSheetController } from 'ionic-angular' import { TouchID } from '@ionic-native/touch-id'; import { Modal } from '../modal' import { FingerOption } from './finger.type' import { BaseLog as Log } from 'base-log' const log = new Log('Finger'); @Injectable() export class Finger { constructor( public modal:Modal, public platform: Platform, public actionSheetCtrl:ActionSheetController, public touchID:TouchID ) { } defaultOption:FingerOption = { title:'通过Home键验证已有手机指纹', enterText:'验证登录密码', } isAvailable = () => { return new Promise((resolve, reject)=>{ //resolve(true); // ios if(!this.touchID || !this.touchID.isAvailable){ resolve(false); }else { return this.touchID.isAvailable().then((type)=>{ log.info('touchID type',type,2); resolve(type == 'touch' ? true : false); },(error)=>{ log.info('error',error,2); resolve(false); }); } }) } // 验证指纹 check = (option?:FingerOption, enterHander?) => { option = Object.assign({},this.defaultOption, option); return this.touchID.isAvailable().then(()=>{ return this.touchID.verifyFingerprintWithCustomPasswordFallbackAndEnterPasswordLabel(option.title,option.enterText); },(error)=>{ log.info('finger error',JSON.stringify(error),true); // 锁住状态则直接弹passcode if(error.code == -8) { return this.touchID.verifyFingerprint(option.title).then(() => { return this.touchID.verifyFingerprintWithCustomPasswordFallbackAndEnterPasswordLabel(option.title,option.enterText); }); }else if(error.code == '-5' || error.code == '-6' || error.code == '-7'){ // 指纹是否可用 this.modal.tip('您尚未开启指纹功能,请在 设置 -> Touch ID与密码 中开启!'); }else{ this.modal.tip('请确保您的指纹功能已开启,请在 设置 -> Touch ID与密码 中查看!'); } return Promise.reject(error); }).then(()=>{ // 成功后回调 return Promise.resolve(true); },(err)=>{ log.info('finger err',JSON.stringify(err)); // -1 错误3次,需提示并重新让用户点击 // -2 点击取消按钮 // -3 点击自定义按钮 // -8 错误太多锁住,需调用默认方法 if(err.code == -3){ if(enterHander) enterHander(); }else if(err.code == -1){ this.modal.tip('指纹不匹配!'); return Promise.reject(err); }else{ return Promise.reject(err); } }); } }