send-code.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // components/send-code/send-code.js
  2. let setInter = null;
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. phone: {
  9. type: String,
  10. value: ' '
  11. },
  12. url: {
  13. type: String,
  14. value: ''
  15. },
  16. reset: {
  17. type: String,
  18. }
  19. },
  20. observers: {
  21. 'reset': function() {
  22. this.setData({
  23. code: []
  24. })
  25. }
  26. },
  27. /**
  28. * 组件的初始数据
  29. */
  30. data: {
  31. up: true,
  32. speed: 1000,
  33. countdown: '点击发送验证码',
  34. code: [],
  35. numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
  36. keybord: [],
  37. time: 60
  38. },
  39. ready() {
  40. // this.countdown();
  41. const arr = this.data.numbers;
  42. const nums = [];
  43. for (let i = 0; i < 4; i++) {
  44. nums.push(arr.slice(i * 3, (i + 1) * 3))
  45. }
  46. this.setData({
  47. keybord: nums,
  48. up: false
  49. })
  50. },
  51. detached() {
  52. clearInterval(setInter)
  53. },
  54. /**
  55. * 组件的方法列表
  56. */
  57. methods: {
  58. keyboard(e) {
  59. const {
  60. value
  61. } = e.currentTarget.dataset;
  62. const code = this.data.code;
  63. if (code.length < 6) {
  64. code.push(value);
  65. this.setData({
  66. code
  67. })
  68. if (code.length === 6) {
  69. this.triggerEvent('done', {
  70. code: this.data.code.join('')
  71. })
  72. // wx.navigateTo({
  73. // url: this.data.url,
  74. // success: () => {
  75. // this.setData({
  76. // code: []
  77. // })
  78. // }
  79. // })
  80. }
  81. }
  82. },
  83. del() {
  84. const code = this.data.code;
  85. code.pop();
  86. this.setData({
  87. code
  88. })
  89. },
  90. send() {
  91. const time = this.data.time;
  92. if (time === 60) {
  93. this.countdown();
  94. this.setData({
  95. up: false
  96. })
  97. this.triggerEvent('send')
  98. }
  99. },
  100. cancel() {
  101. this.setData({
  102. up: true
  103. })
  104. },
  105. showFlash(e) {
  106. this.setData({
  107. up: false
  108. })
  109. },
  110. countdown() {
  111. let _time = this.data.time;
  112. _time--;
  113. this.setData({
  114. time: _time,
  115. countdown: `${_time}s后重新发送`
  116. })
  117. setInter = setInterval(() => {
  118. let _time = this.data.time;
  119. if (_time > 1) {
  120. _time--;
  121. this.setData({
  122. time: _time,
  123. countdown: `${_time}s后重新发送`
  124. })
  125. } else {
  126. clearInterval(setInter);
  127. this.setData({
  128. time: 60,
  129. countdown: `重新发送`
  130. }, _ => {
  131. setInter = null;
  132. })
  133. }
  134. }, this.data.speed)
  135. }
  136. }
  137. })