send-code.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. captcha
  65. },
  66. pageLifetimes: {
  67. show() {
  68. // const captchaResult = getApp().captchaResult;
  69. // getApp().captchaResult = null; // 验证码的票据为一次性票据,取完需要置空
  70. // if (captchaResult && captchaResult.ret === 0) {
  71. // // 将验证码的结果返回至服务端校验
  72. // const ticket = captchaResult.ticket;
  73. // const randstr = captchaResult.randstr;
  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. this.selectComponent('#captcha').show()
  97. },
  98. // 初始化方法 生成键盘布局数组
  99. init(status) {
  100. // 原始数组
  101. const arr = this.data.numbers;
  102. // 实际渲染数组
  103. let keybord = [];
  104. // 如果status = true 则打乱排序生成新的原始数组
  105. if (status) {
  106. for (let i = 0, len = arr.length * 2; i < len; i++) {
  107. var rdm = Math.floor(Math.random() * arr.length)
  108. arr.push(arr[rdm])
  109. arr.splice(rdm, 1)
  110. }
  111. }
  112. // 将原始数组切割成四个小数组 方便渲染
  113. for (let i = 0; i < 4; i++) {
  114. keybord.push(arr.slice(i * 3, (i + 1) * 3))
  115. }
  116. this.setData({
  117. keybord,
  118. hidden: false
  119. })
  120. },
  121. // 键盘事件 点击之后
  122. keyboard(e) {
  123. const {
  124. value
  125. } = e.currentTarget.dataset;
  126. const code = this.data.code;
  127. // push 进入验证码数组
  128. if (code.length < 6) {
  129. code.push(value);
  130. this.setData({
  131. code
  132. })
  133. // 当数组长度达到的时候 触发完成方法
  134. if (code.length === 6) {
  135. this.triggerEvent('done', {
  136. code: this.data.code.join('')
  137. })
  138. }
  139. }
  140. },
  141. // 删除验证码
  142. del() {
  143. const code = this.data.code;
  144. code.pop();
  145. this.setData({
  146. code
  147. })
  148. },
  149. // 发送验证码
  150. send(data) {
  151. const time = this.data.time;
  152. if (time === 60) {
  153. this.countdown();
  154. this.setData({
  155. hidden: false
  156. })
  157. this.triggerEvent('send', data)
  158. }
  159. },
  160. // 取消按键 点击键盘收起
  161. cancel() {
  162. this.setData({
  163. hidden: true
  164. })
  165. },
  166. // 键盘唤醒时 添加标签闪烁效果
  167. showFlash(e) {
  168. this.setData({
  169. hidden: false
  170. })
  171. },
  172. // 倒计时
  173. countdown() {
  174. let _time = this.data.time;
  175. _time--;
  176. this.setData({
  177. time: _time,
  178. countdown: `${_time}s后重新发送`
  179. })
  180. setInter = setInterval(() => {
  181. let _time = this.data.time;
  182. if (_time > 1) {
  183. _time--;
  184. this.setData({
  185. time: _time,
  186. countdown: `${_time}s后重新发送`
  187. })
  188. } else {
  189. clearInterval(setInter);
  190. this.setData({
  191. time: 60,
  192. countdown: `重新发送验证码`
  193. }, _ => {
  194. setInter = null;
  195. })
  196. }
  197. }, this.data.step)
  198. },
  199. // 验证码验证结果回调
  200. handlerVerify: function (ev) {
  201. // 如果使用了 mpvue,ev.detail 需要换成 ev.mp.detail
  202. if (ev.detail.ret === 0) {
  203. // 验证成功
  204. console.log('randstr:', ev.detail.randstr)
  205. console.log('ticket:', ev.detail.ticket)
  206. this.send({
  207. ticket: ev.detail.ticket,
  208. randstr: ev.detail.randstr
  209. })
  210. } else {
  211. // 验证失败
  212. // 请不要在验证失败中调用refresh,验证码内部会进行相应处理
  213. }
  214. },
  215. // 验证码准备就绪
  216. handlerReady: function () {
  217. console.log('验证码准备就绪')
  218. },
  219. // 验证码弹框准备关闭
  220. handlerClose: function (ev) {
  221. // 如果使用了 mpvue,ev.detail 需要换成 ev.mp.detail,ret为0是验证完成后自动关闭验证码弹窗,ret为2是用户主动点击了关闭按钮关闭验证码弹窗
  222. if (ev && ev.detail.ret && ev.detail.ret === 2) {
  223. console.log('点击了关闭按钮,验证码弹框准备关闭');
  224. } else {
  225. console.log('验证完成,验证码弹框准备关闭');
  226. }
  227. },
  228. // 验证码出错
  229. handlerError: function (ev) {
  230. console.log(ev.detail.errMsg)
  231. wx.showModal({
  232. title: '提示',
  233. content: ev.detail.errMsg,
  234. showCancel: false
  235. })
  236. },
  237. }
  238. })