send-code.js 4.8 KB

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