soter-authentication.js 2.1 KB

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