bio-auth.uvue 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <view class="uni-container">
  3. <page-head :title="title"></page-head>
  4. <view class="uni-common-mt">
  5. <text class="uni-title-text">设备支持情况:</text>
  6. <text class="uni-subtitle-text">{{ supportStatus }}</text>
  7. </view>
  8. <view class="uni-common-mt">
  9. <text class="uni-title-text">认证结果:</text>
  10. <text class="uni-subtitle-text">{{ authResult }}</text>
  11. </view>
  12. <button @click="checkSupport" class="uni-common-mt">检查支持的认证方式</button>
  13. <button @click="checkAuth('fingerPrint')" class="uni-common-mt">检查指纹</button>
  14. <button @click="checkAuth('facial')" class="uni-common-mt">检查FaceID</button>
  15. <button @click="startAuth('fingerPrint')" type="primary" class="uni-common-mt">指纹认证</button>
  16. <button @click="startAuth('facial')" type="primary" class="uni-common-mt">FaceID认证</button>
  17. </view>
  18. </template>
  19. <script setup lang="uts">
  20. const title = '生物认证'
  21. const supportStatus = ref('未检查')
  22. const authResult = ref('等待认证')
  23. const checkSupport = () => {
  24. uni.checkIsSupportSoterAuthentication({
  25. success: (res) => {
  26. supportStatus.value = res.supportMode.length > 0 ?
  27. `支持: ${res.supportMode.join(', ')}` :
  28. '不支持任何生物认证'
  29. },
  30. fail: () => {
  31. supportStatus.value = '检查失败'
  32. }
  33. })
  34. }
  35. const checkAuth = (mode: string) => {
  36. uni.checkIsSoterEnrolledInDevice({
  37. checkAuthMode: mode,
  38. success: (res) => {
  39. console.log('res: ',res);
  40. authResult.value = `${mode === 'fingerPrint' ? '指纹' : 'FaceID'}${res.isEnrolled ? '已录入' : '未录入'}`
  41. },
  42. fail: (err) => {
  43. console.log('err: ',err);
  44. authResult.value = `${mode === 'fingerPrint' ? '指纹' : 'FaceID'}检查失败,${err}`
  45. }
  46. })
  47. }
  48. const startAuth = (mode: string) => {
  49. uni.startSoterAuthentication({
  50. requestAuthModes: [mode],
  51. challenge: '123456',
  52. authContent: `请用${mode === 'fingerPrint' ? '指纹' : 'FaceID'}解锁`,
  53. success: (res) => {
  54. console.log('res: ',res);
  55. authResult.value = `${res.authMode === 'fingerPrint' ? '指纹' : 'FaceID'}认证成功`
  56. },
  57. fail: (err) => {
  58. console.log('err: ',err);
  59. authResult.value = `${mode === 'fingerPrint' ? '指纹' : 'FaceID'}认证失败,${err}`
  60. }
  61. })
  62. }
  63. </script>