send-code.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // components/send-code/send-code.js
  2. let setInter = null;
  3. import {
  4. imgServerUrl
  5. } from '../../config/config.js'
  6. Component({
  7. /**
  8. * 组件的属性列表
  9. */
  10. properties: {
  11. // 需要发送验证码的电话号码
  12. phone: {
  13. type: String,
  14. value: ' ',
  15. observer(data) {
  16. if (data) {
  17. const _data = data.toString();
  18. const format_phone = _data.substring(0, 3) + ' ' + _data.substring(3, 7) + ' ' + _data.substring(7, 11);
  19. this.setData({
  20. format_phone
  21. })
  22. }
  23. }
  24. },
  25. // 需要跳转的地址 暂未使用
  26. url: {
  27. type: String,
  28. value: ''
  29. },
  30. // 自动清除已输入文字
  31. reset: {
  32. type: String,
  33. observer() {
  34. this.setData({
  35. code: []
  36. })
  37. }
  38. },
  39. // 自动发送 主要用于绑定手机号码时
  40. auto_send: {
  41. type: Boolean,
  42. value: false,
  43. observer(data) {
  44. if (data === true) {
  45. this.send()
  46. }
  47. }
  48. }
  49. },
  50. /**
  51. * 组件的初始数据
  52. */
  53. data: {
  54. img: imgServerUrl + '/images/happyjob/login_xiaobai.png',
  55. hidden: true, //键盘是否抬起 默认是
  56. step: 1000, // 倒计时的步长
  57. countdown: '点击发送验证码', //点击的按钮文字
  58. code: [], // 验证码
  59. numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0], // 键盘原始数组
  60. keybord: [], //键盘实际渲染数组
  61. time: 60, // 倒计时时长
  62. format_phone: '' //整理格式后的电话号码
  63. },
  64. ready() {
  65. // 初始化界面
  66. this.init();
  67. },
  68. detached() {
  69. clearInterval(setInter)
  70. },
  71. /**
  72. * 组件的方法列表
  73. */
  74. methods: {
  75. // 初始化方法 生成键盘布局数组
  76. init(status) {
  77. // 原始数组
  78. const arr = this.data.numbers;
  79. // 实际渲染数组
  80. let keybord = [];
  81. // 如果status = true 则打乱排序生成新的原始数组
  82. if (status) {
  83. for (let i = 0, len = arr.length * 2; i < len; i++) {
  84. var rdm = Math.floor(Math.random() * arr.length)
  85. arr.push(arr[rdm])
  86. arr.splice(rdm, 1)
  87. }
  88. }
  89. // 将原始数组切割成四个小数组 方便渲染
  90. for (let i = 0; i < 4; i++) {
  91. keybord.push(arr.slice(i * 3, (i + 1) * 3))
  92. }
  93. this.setData({
  94. keybord,
  95. hidden: false
  96. })
  97. },
  98. // 键盘事件 点击之后
  99. keyboard(e) {
  100. const {
  101. value
  102. } = e.currentTarget.dataset;
  103. const code = this.data.code;
  104. // push 进入验证码数组
  105. if (code.length < 6) {
  106. code.push(value);
  107. this.setData({
  108. code
  109. })
  110. // 当数组长度达到的时候 触发完成方法
  111. if (code.length === 6) {
  112. this.triggerEvent('done', {
  113. code: this.data.code.join('')
  114. })
  115. }
  116. }
  117. },
  118. // 删除验证码
  119. del() {
  120. const code = this.data.code;
  121. code.pop();
  122. this.setData({
  123. code
  124. })
  125. },
  126. // 发送验证码
  127. send() {
  128. const time = this.data.time;
  129. if (time === 60) {
  130. this.countdown();
  131. this.setData({
  132. hidden: false
  133. })
  134. this.triggerEvent('send')
  135. }
  136. },
  137. // 取消按键 点击键盘收起
  138. cancel() {
  139. this.setData({
  140. hidden: true
  141. })
  142. },
  143. // 键盘唤醒时 添加标签闪烁效果
  144. showFlash(e) {
  145. this.setData({
  146. hidden: false
  147. })
  148. },
  149. // 倒计时
  150. countdown() {
  151. let _time = this.data.time;
  152. _time--;
  153. this.setData({
  154. time: _time,
  155. countdown: `${_time}s后重新发送`
  156. })
  157. setInter = setInterval(() => {
  158. let _time = this.data.time;
  159. if (_time > 1) {
  160. _time--;
  161. this.setData({
  162. time: _time,
  163. countdown: `${_time}s后重新发送`
  164. })
  165. } else {
  166. clearInterval(setInter);
  167. this.setData({
  168. time: 60,
  169. countdown: `重新发送验证码`
  170. }, _ => {
  171. setInter = null;
  172. })
  173. }
  174. }, this.data.step)
  175. }
  176. }
  177. })