captcha.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // components/captcha/captcha.js
  2. import { getDB, getModels, getCommand, getTempFileURLs, getClient } from '../../utils/cloudbase.js'
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. phone: {
  9. type: String,
  10. required: true
  11. },
  12. },
  13. /**
  14. * 组件的初始数据
  15. */
  16. data: {
  17. generateCode: '-999999', // 点击发送验证码时生成的验证码
  18. userCode: '', // 用户输入的验证码
  19. defaultCode: '989898', // 测试时使用
  20. },
  21. /**
  22. * 组件的方法列表
  23. */
  24. methods: {
  25. /**
  26. * 监听验证码输入
  27. * @param {*} e
  28. */
  29. onInputCode(e) {
  30. this.setData({
  31. userCode: e.detail.value
  32. })
  33. },
  34. // 获取验证码
  35. async getCode() {
  36. const { phone } = this.data;
  37. if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
  38. wx.showToast({
  39. title: '请输入有效的手机号码',
  40. icon: 'none'
  41. });
  42. return;
  43. }
  44. const generateCode = this.generateCode(); // 本地生成验证码(用于发送)
  45. this.setData({ generateCode, isSending: true });
  46. wx.showLoading({ title: '获取中...', mask: true });
  47. // 保存验证码到 data
  48. const client = await getClient();
  49. this.setData({ generateCode });
  50. client.callFunction({
  51. // 云函数名称
  52. name: 'lxp',
  53. // 传给云函数的参数
  54. data: {
  55. mobile: this.data.phone,
  56. code: generateCode,
  57. },
  58. }).then(res => {
  59. if (res.result.response.code === 200) {
  60. console.log('数据获取成功 =>', res.result.response);
  61. wx.showToast({
  62. title: '验证码发送成功',
  63. icon: 'none'
  64. });
  65. // 启动倒计时
  66. this.startCountdown(60);
  67. } else {
  68. console.error('数据获取失败 =>', res.result.response);
  69. wx.showToast({
  70. title: '验证码发送失败',
  71. icon: 'none'
  72. });
  73. }
  74. }).catch(error => {
  75. console.error(error)
  76. wx.showToast({
  77. title: '验证码发送失败',
  78. icon: 'none'
  79. });
  80. })
  81. },
  82. /**
  83. * 生成验证码
  84. */
  85. generateCode() {
  86. return Math.floor(100000 + Math.random() * 900000).toString();
  87. },
  88. /**
  89. * 校验验证码
  90. */
  91. validCaptcha() {
  92. const { generateCode, userCode, defaultCode } = this.data
  93. const flag = generateCode === userCode || defaultCode === userCode
  94. const result = {
  95. flag,
  96. msg: '',
  97. }
  98. if (!flag) {
  99. result.msg = '验证码输入错误'
  100. }
  101. if (!userCode) {
  102. result.msg = '请输入验证码'
  103. }
  104. return result
  105. },
  106. startCountdown(seconds) {
  107. this.setData({ countdown: seconds });
  108. const timer = setInterval(() => {
  109. if (this.data.countdown <= 1) {
  110. clearInterval(timer);
  111. this.setData({ countdown: 0, isSending: false }); // 倒计时结束后重置 isSending
  112. } else {
  113. this.setData({ countdown: this.data.countdown - 1 });
  114. }
  115. }, 1000);
  116. },
  117. }
  118. })