import {Inject, Injectable, OnInit} from '@angular/core'; import {M2_DEVICE_INFO_PROVIDER} from '../../core/tokens/m2.tokens'; import {DeviceInfoInterface} from '../../core/interfaces/device-info.interface'; import {StorageService} from '../../common/tools/storage.service'; import {M2Toast} from '../../layout/setting/m2.toast'; import {FingerprintAIO} from '@ionic-native/fingerprint-aio/ngx'; import {UserProvider} from '../../core/services/user/user'; import {Router} from '@angular/router'; import {AuthService} from '../../core/services/auth/auth-service'; import {CryptoJsHelp} from '../../common/tools/cryptoJs-help'; import {FingerPrintInterface} from '../../core/interfaces/finger-print.interface'; @Injectable() export class FingerPrintService implements FingerPrintInterface { fingerPrintOld: boolean; fingerPrint: boolean; allowFingerPrint: boolean; account: any; constructor(@Inject(M2_DEVICE_INFO_PROVIDER) private deviceInfoInterface: DeviceInfoInterface, private userProvider: UserProvider, private m2Toast: M2Toast, private router: Router, private storageService: StorageService, private fingerprintAIO: FingerprintAIO, private authService: AuthService) { } getAllowStatus() { this.fingerPrintOld = this.storageService.read('_DF_allowFingerPrint'); this.fingerPrint = this.fingerPrintOld; return this.fingerPrint; } async settingFingerPrint(onOff: boolean) { this.fingerPrint = onOff; if (this.fingerPrintOld === this.fingerPrint) { return; } this.fingerPrintOld = this.fingerPrint; if (this.fingerPrint) { this.deviceInfoInterface.ready().then(item => { if (item.isCordova) { this.fingerprintAIO.isAvailable().then(() => { const options = { clientId: 'df2app', // Android: Used for encryption. iOS: used for dialogue if no `localizedReason` is given. clientSecret: 'm234adUbn45*Hfd34', // Necessary for Android encrpytion of keys. Use random secret key. disableBackup: true, // Only for Android(optional) localizedFallbackTitle: 'Use Pin', // Only for iOS localizedReason: 'Please authenticate' // Only for iOS }; this.fingerprintAIO.show(options) .then((result: any) => { console.log(result); this.storageService.write('_DF_allowFingerPrint', this.fingerPrint); }) .catch((error: any) => console.log(error)); }); } else { this.m2Toast.presentInfoToast('运行在浏览器环境,开启个指纹干什么?手指头戳哪里?你戳一个我看看!').then(); } }); } else { this.storageService.write('_DF_allowFingerPrint', this.fingerPrint); this.m2Toast.presentInfoToast('指纹登录已关闭!').then(); } } async showFingerPrint() { try { this.allowFingerPrint = this.storageService.read('_DF_allowFingerPrint'); const item = await this.deviceInfoInterface.ready(); if (item.isCordova && this.allowFingerPrint) { this.fingerprintAIO.isAvailable().then(() => { const options = { clientId: 'df2app', // Android: Used for encryption. iOS: used for dialogue if no `localizedReason` is given. clientSecret: 'm234adUbn45*Hfd34', // Necessary for Android encrpytion of keys. Use random secret key. disableBackup: true, // Only for Android(optional) localizedFallbackTitle: 'Use Pin', // Only for iOS localizedReason: 'Please authenticate' // Only for iOS }; this.fingerprintAIO.show(options) .then((result: any) => { this.account = CryptoJsHelp.decrypt(this.storageService.readString('_DF_userPin')); let redirectUrl = this.authService.redirectUrl; if (!redirectUrl) { redirectUrl = ''; } this.userProvider.login(this.account).then((rt: boolean) => { if (rt) { this.storageService.write('_DF_userPin', CryptoJsHelp.encrypt(this.account)); this.router.navigateByUrl(redirectUrl).then(); } else { this.m2Toast.presentDangerToast('登录失败!'); } }).catch((err) => { this.m2Toast.presentDangerToast(err.error.message); }); }) .catch((error: any) => console.log(error)); }); } } catch (e) { console.log(e); } } }