204 lines
5.5 KiB
Vue
204 lines
5.5 KiB
Vue
<template>
|
||
<App/>
|
||
</template>
|
||
<script>
|
||
import i18n from '@/common/i18n/index.js'
|
||
|
||
export default {
|
||
onLaunch: function() {
|
||
console.log('App Launch')
|
||
// 延迟检查安全验证,确保plus对象已初始化
|
||
setTimeout(() => {
|
||
this.checkAppSecurity()
|
||
}, 1000)
|
||
},
|
||
onShow: function() {
|
||
console.log('App Show')
|
||
// 每次应用显示时检查安全验证,延迟执行确保plus对象可用
|
||
setTimeout(() => {
|
||
this.checkAppSecurity()
|
||
}, 500)
|
||
},
|
||
// 添加一个全局方法用于测试
|
||
onReady: function() {
|
||
// 将验证方法挂载到全局,方便测试
|
||
// #ifdef APP-PLUS
|
||
if (typeof plus !== 'undefined') {
|
||
plus.webview.currentWebview().evalJS(`
|
||
if (typeof window.testAuth === 'undefined') {
|
||
window.testAuth = function() {
|
||
console.log('手动触发验证测试');
|
||
// 这里可以手动触发验证逻辑
|
||
};
|
||
}
|
||
`)
|
||
}
|
||
// #endif
|
||
},
|
||
onHide: function() {
|
||
console.log('App Hide')
|
||
},
|
||
methods: {
|
||
// 检查应用安全设置
|
||
checkAppSecurity() {
|
||
try {
|
||
const securityEnabled = uni.getStorageSync('appSecurityEnabled')
|
||
console.log('安全设置状态:', securityEnabled)
|
||
if (securityEnabled) {
|
||
// 延迟一点时间确保页面完全加载
|
||
setTimeout(() => {
|
||
this.performBiometricAuth()
|
||
}, 100)
|
||
} else {
|
||
}
|
||
|
||
// 添加调试信息到页面
|
||
this.showDebugInfo(securityEnabled)
|
||
} catch (e) {
|
||
console.error('检查安全设置失败:', e)
|
||
}
|
||
},
|
||
|
||
// 显示调试信息
|
||
showDebugInfo(securityEnabled) {
|
||
// console.log('当前时间:', new Date().toLocaleString())
|
||
// console.log('应用状态:', 'onShow')
|
||
console.log('========================')
|
||
|
||
if (securityEnabled) {
|
||
setTimeout(() => {
|
||
// uni.showToast({
|
||
// icon: 'none',
|
||
// duration: 2000
|
||
// })
|
||
}, 500)
|
||
}
|
||
},
|
||
|
||
// 执行生物识别验证
|
||
performBiometricAuth() {
|
||
console.log('开始执行生物识别验证')
|
||
|
||
// 先尝试使用原生生物识别,如果失败则使用模拟验证
|
||
let useNativeAuth = false
|
||
|
||
// #ifdef APP-PLUS
|
||
// 检查plus对象是否可用
|
||
if (typeof plus !== 'undefined' && plus.fingerprint) {
|
||
console.log('检测到plus.fingerprint,尝试使用原生验证')
|
||
useNativeAuth = true
|
||
|
||
// 检查是否支持生物识别
|
||
plus.fingerprint.isKeyguardSecure((secureResult) => {
|
||
console.log('生物识别支持检查结果:', secureResult)
|
||
if (secureResult) {
|
||
console.log('设备支持生物识别,开始原生验证')
|
||
// 执行生物识别验证
|
||
plus.fingerprint.authenticate((result) => {
|
||
console.log('生物识别验证成功:', result)
|
||
// 验证成功,继续正常流程
|
||
uni.showToast({
|
||
title: '验证成功',
|
||
icon: 'success'
|
||
})
|
||
}, (error) => {
|
||
console.log('生物识别验证失败:', error)
|
||
// 验证失败,显示错误信息
|
||
uni.showModal({
|
||
title: i18n.t('biometricAuthFailed'),
|
||
content: i18n.t('biometricAuthCancel'),
|
||
showCancel: false,
|
||
confirmText: i18n.t('modalConfirm'),
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
// 用户确认后关闭应用
|
||
// #ifdef APP-PLUS
|
||
plus.runtime.quit()
|
||
// #endif
|
||
}
|
||
}
|
||
})
|
||
}, {
|
||
message: i18n.t('biometricPrompt')
|
||
})
|
||
} else {
|
||
console.log('设备不支持生物识别,使用模拟验证')
|
||
this.showMockAuth()
|
||
}
|
||
}, (error) => {
|
||
console.log('检查生物识别支持时出错:', error)
|
||
// 检查失败时也显示模拟验证
|
||
this.showMockAuth()
|
||
})
|
||
} else {
|
||
console.log('plus对象或plus.fingerprint不可用,使用模拟验证')
|
||
useNativeAuth = false
|
||
}
|
||
// #endif
|
||
|
||
// 如果不是APP环境或原生验证不可用,使用模拟验证
|
||
if (!useNativeAuth) {
|
||
console.log('使用模拟验证')
|
||
// 延迟一点时间确保界面完全加载
|
||
setTimeout(() => {
|
||
this.showMockAuth()
|
||
}, 200)
|
||
}
|
||
},
|
||
|
||
// 显示模拟验证(用于基座或不支持生物识别的情况)
|
||
showMockAuth() {
|
||
console.log('显示模拟验证界面')
|
||
uni.showModal({
|
||
title: '身份验证',
|
||
content: '请输入密码或使用生物识别验证',
|
||
editable: true,
|
||
placeholderText: '请输入密码',
|
||
confirmText: '验证',
|
||
cancelText: '取消',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
if (res.content && res.content.length > 0) {
|
||
console.log('模拟验证成功')
|
||
uni.showToast({
|
||
title: '验证成功',
|
||
icon: 'success'
|
||
})
|
||
} else {
|
||
uni.showToast({
|
||
title: '请输入密码',
|
||
icon: 'none'
|
||
})
|
||
// 重新显示验证
|
||
setTimeout(() => {
|
||
this.showMockAuth()
|
||
}, 1000)
|
||
}
|
||
} else {
|
||
console.log('用户取消验证')
|
||
// 用户取消验证,可以选择关闭应用或允许继续
|
||
uni.showModal({
|
||
title: '验证失败',
|
||
content: '未通过身份验证,应用将关闭',
|
||
showCancel: false,
|
||
confirmText: '确定',
|
||
success: (result) => {
|
||
// #ifdef APP-PLUS
|
||
if (typeof plus !== 'undefined') {
|
||
plus.runtime.quit()
|
||
}
|
||
// #endif
|
||
}
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style>
|
||
/*每个页面公共css */
|
||
</style>
|