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