电速宝
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. Component({
  2. properties: {
  3. // 是否显示弹窗
  4. isShow: {
  5. type: Boolean,
  6. value: false
  7. },
  8. // 手机号(用于显示)
  9. phoneNumber: {
  10. type: String,
  11. value: ''
  12. },
  13. // 倒计时秒数
  14. countdownSeconds: {
  15. type: Number,
  16. value: 60
  17. }
  18. },
  19. data: {
  20. // 验证码数组(6位)
  21. codes: ['', '', '', '', '', ''],
  22. // 实际输入的验证码值
  23. codeValue: '',
  24. // 当前激活的输入框索引
  25. currentIndex: 0,
  26. // 是否聚焦
  27. isFocus: false,
  28. // 倒计时
  29. countdown: 60,
  30. // 是否可以重新发送
  31. canResend: false,
  32. // 验证码是否输入完成
  33. isComplete: false,
  34. // 倒计时计时器
  35. timer: null
  36. },
  37. observers: {
  38. // 监听isShow变化,控制弹窗显示隐藏
  39. isShow: function (isShow) {
  40. if (isShow) {
  41. this.startCountdown();
  42. this.focusInput();
  43. } else {
  44. this.clearCountdown();
  45. this.resetCodeInput();
  46. }
  47. },
  48. // 监听countdownSeconds变化
  49. countdownSeconds: function (seconds) {
  50. this.setData({
  51. countdown: seconds
  52. });
  53. },
  54. // 监听验证码变化,更新输入状态
  55. codeValue: function (value) {
  56. const codes = [];
  57. for (let i = 0; i < 6; i++) {
  58. codes[i] = value[i] || '';
  59. }
  60. // 更新当前激活的输入框索引
  61. let currentIndex = value.length < 6 ? value.length : 5;
  62. // 判断是否输入完成
  63. const isComplete = value.length === 6;
  64. this.setData({
  65. codes,
  66. currentIndex,
  67. isComplete
  68. });
  69. // 如果输入完成,自动触发确认事件
  70. if (isComplete) {
  71. this.triggerEvent('complete', { code: value });
  72. }
  73. }
  74. },
  75. methods: {
  76. // 聚焦输入框
  77. focusInput() {
  78. this.setData({
  79. isFocus: true
  80. });
  81. },
  82. // 输入框失焦
  83. onInputBlur() {
  84. this.setData({
  85. isFocus: false
  86. });
  87. },
  88. // 验证码输入
  89. onCodeInput(e) {
  90. const value = e.detail.value.replace(/\D/g, ''); // 只保留数字
  91. this.setData({
  92. codeValue: value
  93. });
  94. },
  95. // 关闭弹窗
  96. onClose() {
  97. this.triggerEvent('close');
  98. },
  99. // 确认
  100. onConfirm() {
  101. if (this.data.isComplete) {
  102. this.triggerEvent('confirm', { code: this.data.codeValue });
  103. }
  104. },
  105. // 重新发送验证码
  106. onResend() {
  107. this.triggerEvent('resend');
  108. this.startCountdown();
  109. },
  110. // 开始倒计时
  111. startCountdown() {
  112. // 清除之前的计时器
  113. this.clearCountdown();
  114. const countdownSeconds = this.data.countdownSeconds;
  115. this.setData({
  116. countdown: countdownSeconds,
  117. canResend: false
  118. });
  119. // 设置新的计时器
  120. const timer = setInterval(() => {
  121. let countdown = this.data.countdown - 1;
  122. if (countdown <= 0) {
  123. this.setData({
  124. countdown: 0,
  125. canResend: true
  126. });
  127. this.clearCountdown();
  128. } else {
  129. this.setData({
  130. countdown
  131. });
  132. }
  133. }, 1000);
  134. this.setData({ timer });
  135. },
  136. // 清除倒计时
  137. clearCountdown() {
  138. if (this.data.timer) {
  139. clearInterval(this.data.timer);
  140. this.setData({ timer: null });
  141. }
  142. },
  143. // 重置验证码输入
  144. resetCodeInput() {
  145. this.setData({
  146. codes: ['', '', '', '', '', ''],
  147. codeValue: '',
  148. currentIndex: 0,
  149. isComplete: false
  150. });
  151. }
  152. },
  153. // 组件销毁时清除计时器
  154. detached() {
  155. this.clearCountdown();
  156. }
  157. });