Verify.ets 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import TitleBar from '../view/TitleBar'
  2. import { promptAction, router } from '@kit.ArkUI'
  3. import { CommonConstants } from '../common/constants/CommonConstants'
  4. import { AppUtil, PreferencesUtil, ToastUtil } from '@pura/harmony-utils'
  5. import { Utility } from '../common/util/Utility'
  6. import UserAuthModel from '../viewmodel/UserAuthModel'
  7. import { common, Want } from '@kit.AbilityKit'
  8. @Preview
  9. @Component
  10. export struct Verify{
  11. @State verType:number = 0;// 0:密码 1:指纹 2:人脸
  12. @State isMima:boolean = true
  13. @State isFingerprint:boolean = false
  14. @State isFace:boolean = false
  15. public onVerifyCompleted?: (isSuccess:boolean) => void//验证完成的结果。通知是否成功
  16. @State titleBarModel: TitleBar.Model = new TitleBar.Model()
  17. .setTitleTextStyle(FontStyle.Normal)
  18. .setTitleBarStyle(TitleBar.BarStyle.TRANSPARENT)
  19. .setLeftIcon($r('app.media.left_back_white'))
  20. .setTitleName('验证密码')
  21. .setTitleFontColor(Color.White)
  22. .setTitleBarBackground($r('app.color.title_bar_bg'))
  23. .setLeftTitleBackground($r('app.color.title_bar_bg'))
  24. .setOnLeftClickListener(() => {
  25. router.back()
  26. })
  27. @State verName:string = ''
  28. // 组件生命周期
  29. aboutToAppear() {
  30. this.verName = AppUtil.getVersionName()
  31. this.verType = PreferencesUtil.getNumberSync('verType')
  32. this.doStartVerify()
  33. console.info('LifeCycleComponent aboutToAppear');
  34. }
  35. doStartVerify(){
  36. if(this.verType === 1){
  37. if(UserAuthModel.isFingerprintAvailable()){
  38. this.isMima = false
  39. this.isFingerprint = true
  40. this.isFace = false
  41. UserAuthModel.getFingerprintAuth(this.userAuthResult);
  42. UserAuthModel.start()
  43. }else{
  44. this.gotoAuthSetting()
  45. }
  46. }else if(this.verType === 2){//如果是人脸,自动开启识别
  47. if(UserAuthModel.isFaceAvailable()){
  48. this.isMima = false
  49. this.isFingerprint = false
  50. this.isFace = true
  51. UserAuthModel.getFaceAuth(this.userAuthResult);
  52. UserAuthModel.start()
  53. }else{
  54. this.gotoAuthSetting()
  55. }
  56. }else{
  57. if(UserAuthModel.isPinAvailable()){
  58. this.isMima = true
  59. this.isFingerprint = false
  60. this.isFace = false
  61. UserAuthModel.getPINAuth(this.userAuthResult);
  62. UserAuthModel.start()
  63. }else{
  64. this.gotoAuthSetting()
  65. }
  66. }
  67. }
  68. //跳转到生物识别系统设置页面
  69. gotoAuthSetting(){
  70. // ToastUtil.showToast('第一次请开启密码、指纹或人脸其中的一种安全验证方式。')
  71. let context = getContext(this) as common.UIAbilityContext;
  72. let want: Want = {
  73. bundleName: 'com.huawei.hmos.settings',
  74. abilityName: 'com.huawei.hmos.settings.MainAbility',
  75. uri: 'biometrics_and_password_settings',
  76. parameters: {
  77. pushParams: 'com.example.natificationdemo'
  78. }
  79. };
  80. context.startAbility(want)
  81. }
  82. /**
  83. * Operations After a User Is Successfully Authenticated.
  84. *
  85. * @param Whether the user authentication is successful.
  86. */
  87. userAuthResult: (isSuccess: boolean) => void = (isSuccess: boolean) => {
  88. this.onVerifyCompleted?.(isSuccess)//播放完成的接口回调
  89. }
  90. // 组件生命周期
  91. aboutToDisappear() {
  92. console.info('LifeCycleComponent aboutToDisappear');
  93. }
  94. build() {
  95. Column(){
  96. // TitleBar({model:$titleBarModel})
  97. Column() {
  98. Column(){
  99. //锁屏密码
  100. Row() {
  101. Image($r('app.media.mima'))
  102. .height(30)
  103. .alignSelf(ItemAlign.Center)
  104. .margin({ left: 28 })
  105. Text('密码')
  106. .margin({ left: 10, right: 20 })
  107. .fontSize(18)
  108. .fontColor(Color.Gray)
  109. .fontWeight(480)
  110. .id('text_update')
  111. Blank()
  112. Radio({ value: 'Radio1', group: 'radioGroup' })
  113. .width(22)
  114. .height(22)
  115. .margin({ left: 20, right: 20 })
  116. .align(Alignment.Center)
  117. .checked(this.isMima)
  118. .onChange((isChecked: boolean) => {
  119. if (isChecked) {
  120. this.verType = 0
  121. }
  122. })
  123. }
  124. .width('100%')
  125. .height(66)
  126. .onClick(() => {
  127. this.isMima = true
  128. this.isFingerprint = false
  129. this.isFace = false
  130. this.verType = 0
  131. })
  132. Row() {
  133. Image($r('app.media.touchid'))
  134. .height(30)
  135. .alignSelf(ItemAlign.Center)
  136. .margin({ left: 28 })
  137. Text('指纹')
  138. .margin({ left: 10, right: 20 })
  139. .fontSize(18)
  140. .fontColor(Color.Gray)
  141. .fontWeight(480)
  142. .id('text_update')
  143. Blank()
  144. Radio({ value: 'Radio2', group: 'radioGroup' })
  145. .width(22)
  146. .height(22)
  147. .margin({ left: 20, right: 20 })
  148. .align(Alignment.Center)
  149. .checked(this.isFingerprint)
  150. .onChange((isChecked: boolean) => {
  151. if (isChecked) {
  152. this.verType = 1
  153. }
  154. })
  155. }
  156. .width('100%')
  157. .height(66)
  158. .onClick(() => {
  159. this.isMima = false
  160. this.isFingerprint = true
  161. this.isFace = false
  162. this.verType = 1
  163. })
  164. Row() {
  165. Image($r('app.media.face'))
  166. .height(30)
  167. .alignSelf(ItemAlign.Center)
  168. .margin({ left: 28 })
  169. Text('人脸')
  170. .margin({ left: 10, right: 20 })
  171. .fontSize(18)
  172. .fontColor(Color.Gray)
  173. .fontWeight(480)
  174. .id('text_update')
  175. Blank()
  176. Radio({ value: 'Radio3', group: 'radioGroup' })
  177. .width(22)
  178. .height(22)
  179. .margin({ left: 20, right: 20 })
  180. .align(Alignment.Center)
  181. .checked(this.isFace)
  182. .onChange((isChecked: boolean) => {
  183. if (isChecked) {
  184. this.verType = 2
  185. }
  186. })
  187. }
  188. .width('100%')
  189. .height(66)
  190. .onClick(() => {
  191. this.isMima = false
  192. this.isFingerprint = false
  193. this.isFace = true
  194. this.verType = 2
  195. })
  196. }
  197. .backgroundColor($r('app.color.bottom_control_background'))
  198. .margin({top:30,left:30,right:30})
  199. .borderRadius(20)
  200. Button('确定', { type: ButtonType.Capsule, stateEffect: false })
  201. .width('70%')
  202. .height(55)
  203. .margin({ top: 25, bottom: 10 })
  204. .backgroundColor($r('app.color.title_bar_bg'))
  205. // .linearGradient({
  206. // direction: GradientDirection.Right,
  207. // colors: [['#ff37a0fc', 0.0], ['#f5856e', 0.5], ['#67e667', 1.0]]
  208. // })
  209. .onClick(() => {
  210. this.doStartVerify()
  211. PreferencesUtil.putSync('verType',this.verType)
  212. })
  213. }
  214. // .linearGradient({colors:[['#ff37a0fc', 0.0], ['#67e667', 0.5]]})
  215. .height('100%')
  216. .width('100%')
  217. .backgroundColor($r('app.color.index_background'))
  218. }
  219. }
  220. }