import {reduxTools} from '@yoronsoft/js-utils'; import TouchID from "react-native-touch-id"; import language from "../language"; import theme, {ThemeCss} from "../theme"; import {getConfigName, loggerConfig} from "../config"; import {ITouchID} from "./touchID"; /** * touchID 是否开起 */ const appTouchIDHasUsable = `${getConfigName}_touchIDHasUsable`; const logger = loggerConfig('touchID') const touchID: ITouchID = { hasUsable(): boolean { return reduxTools.get(appTouchIDHasUsable); }, init(): void { const params = { unifiedErrors: true,// use unified error messages (default false) passcodeFallback: true, // if true is passed, itwill allow isSupported to return an error if the device is not enrolled in touch id/face id etc. Otherwise, it will just tell you what method is supported, even if the user is not enrolled. (default false) }; const hasRedux = reduxTools.get(appTouchIDHasUsable); TouchID.isSupported(params).then(res => { logger.log('has()', res); const value = !!res if (typeof hasRedux === 'boolean') reduxTools.update(appTouchIDHasUsable, value) else reduxTools.create(appTouchIDHasUsable, value); }).catch(err => { logger.err('has() err', err.toString()) if (typeof hasRedux === 'boolean') reduxTools.update(appTouchIDHasUsable, false) else reduxTools.create(appTouchIDHasUsable, false); }) }, reacquire(): Promise { return new Promise(resolve => { const params = { unifiedErrors: true,// use unified error messages (default false) passcodeFallback: true, // if true is passed, itwill allow isSupported to return an error if the device is not enrolled in touch id/face id etc. Otherwise, it will just tell you what method is supported, even if the user is not enrolled. (default false) }; TouchID.isSupported(params).then(res => { logger.log('reacquire()', res); reduxTools.update(appTouchIDHasUsable, !!res); return resolve(!!res) }).catch(err => { logger.err('reacquire() err', err.toString()) reduxTools.update(appTouchIDHasUsable, false); return resolve(false) }) }) }, verify: function (): Promise { const css = theme.get(); const langTitle = language.getTouchID('title'); const langSensorDescription = language.getTouchID('sensorDescription'); const langSensorErrorDescription = language.getTouchID('sensorErrorDescription'); const langCancel = language.getTouchID('cancel'); const langDesc = language.getTouchID('desc'); const params = { title: langTitle.length ? langTitle : '身份验证', // Android imageColor: css.main.toString(), // Android imageErrorColor: "#ff0000", // Android sensorDescription: langSensorDescription.length ? langSensorDescription : '触摸传感器', // Android sensorErrorDescription: langSensorErrorDescription.length ? langSensorErrorDescription : '验证失败', // Android cancelText: langCancel.length ? langCancel : '取消', // Android // iOS (if empty, then label is hidden) fallbackLabel: '', // 使用统一的错误信息(默认 false) unifiedErrors: true, // iOS - 如果 faceId/touchId 不可用,允许设备回退到使用密码。 这并不意味着如果 touchId/faceId 前几次失败,它将恢复为密码,而是如果前者未注册,那么它将使用密码。 passcodeFallback: true, } return new Promise(resolve => { TouchID.authenticate(langDesc.length ? langDesc : '验证指纹', params) .then((res: boolean | PromiseLike) => { logger.log('verify()', res) return resolve(res); }) .catch((err: any) => { logger.err('verify() err', err.toString()) return resolve(false); }) }) } } export default touchID