index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // components/password/index.js
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. show: {
  8. type: Boolean,
  9. value: false,
  10. },
  11. length: {
  12. type: Number,
  13. value: 6,
  14. },
  15. title: {
  16. type: String,
  17. value: '请输入密码',
  18. },
  19. showBtn: {
  20. type: Boolean,
  21. value: false
  22. },
  23. btnTitle: {
  24. type: String,
  25. value: '确 认',
  26. },
  27. },
  28. /**
  29. * 组件的初始数据
  30. */
  31. data: {
  32. password: '',
  33. showKeyFocus:true
  34. },
  35. /**
  36. * 组件的方法列表
  37. */
  38. methods: {
  39. onClose() {
  40. this.setData({
  41. show: false,
  42. password: ''
  43. })
  44. },
  45. //失去输入密码焦点
  46. onblur() {
  47. console.log('失去焦点');
  48. this.setData({
  49. showKeyFocus: false
  50. })
  51. },
  52. //模拟点击唤起键盘
  53. onShowBlur() {
  54. console.log('唤起键盘焦点');
  55. this.setData({
  56. showKeyFocus: true
  57. })
  58. },
  59. //输入完成自动失去焦点
  60. onInput(e) {
  61. let {
  62. length
  63. } = this.data
  64. let {
  65. cursor
  66. } = e.detail
  67. if (cursor == length) {
  68. console.log('输入完成');
  69. wx.hideKeyboard()
  70. this.okResult()
  71. }
  72. },
  73. /* 确认密码返回事件给父组件 */
  74. okResult() {
  75. let {
  76. password
  77. } = this.data
  78. this.triggerEvent("onResult", {
  79. password: password
  80. });
  81. },
  82. onClickOk(){
  83. let {
  84. password,
  85. length
  86. } = this.data
  87. if(password && String(password).length == length){
  88. this.triggerEvent("onClickBack", {
  89. password: password
  90. });
  91. return
  92. }
  93. }
  94. },
  95. lifetimes:{
  96. detached: function() {
  97. // 在组件实例被从页面节点树移除时执行
  98. console.log('子组件————————detached')
  99. this.setData({
  100. password:''
  101. })
  102. },
  103. },
  104. })