soter-authentication.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const AUTH_MODE = 'fingerPrint'
  2. Page({
  3. onShareAppMessage() {
  4. return {
  5. title: '生物认证',
  6. path: 'page/API/pages/soter-authentication/soter-authentication'
  7. }
  8. },
  9. startAuth() {
  10. const startSoterAuthentication = () => {
  11. wx.startSoterAuthentication({
  12. requestAuthModes: [AUTH_MODE],
  13. challenge: 'test',
  14. authContent: '小程序示例',
  15. success: () => {
  16. wx.showToast({
  17. title: '认证成功'
  18. })
  19. },
  20. fail: (err) => {
  21. console.error(err)
  22. wx.showModal({
  23. title: '失败',
  24. content: '认证失败',
  25. showCancel: false
  26. })
  27. }
  28. })
  29. }
  30. const checkIsEnrolled = () => {
  31. wx.checkIsSoterEnrolledInDevice({
  32. checkAuthMode: AUTH_MODE,
  33. success: (res) => {
  34. console.log(res)
  35. if (parseInt(res.isEnrolled, 10) <= 0) {
  36. wx.showModal({
  37. title: '错误',
  38. content: '您暂未录入指纹信息,请录入后重试',
  39. showCancel: false
  40. })
  41. return
  42. }
  43. startSoterAuthentication()
  44. },
  45. fail: (err) => {
  46. console.error(err)
  47. }
  48. })
  49. }
  50. const notSupported = () => {
  51. wx.showModal({
  52. title: '错误',
  53. content: '您的设备不支持指纹识别',
  54. showCancel: false
  55. })
  56. }
  57. wx.checkIsSupportSoterAuthentication({
  58. success: (res) => {
  59. console.log(res)
  60. if (
  61. !res ||
  62. res.supportMode.length === 0 ||
  63. res.supportMode.indexOf('fingerPrint') < 0
  64. ) {
  65. notSupported()
  66. return
  67. }
  68. checkIsEnrolled()
  69. },
  70. fail: (err) => {
  71. console.error(err)
  72. notSupported()
  73. }
  74. })
  75. }
  76. })