index.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <!--获取验证码-->
  3. <view>
  4. <u-code
  5. keepRunning
  6. :uniqueKey="uniqueKey"
  7. ref="uCode"
  8. @change="codeChange"
  9. ></u-code>
  10. <c-button size="14" @click="handleClick" :type="btnType">
  11. {{ tips }}
  12. </c-button>
  13. </view>
  14. </template>
  15. <script>
  16. import { getAuthCode } from '@/api/com';
  17. export default {
  18. name: 'cCode',
  19. props: {
  20. uniqueKey: String, // code组件的唯一key,若key相同计时相同
  21. phone: String, // 手机号
  22. codeType: String, // 接口的code
  23. btnType: {
  24. default: 'text',
  25. type: String,
  26. },
  27. },
  28. data() {
  29. return {
  30. tips: '',
  31. };
  32. },
  33. methods: {
  34. handleClick() {
  35. if (this.$listeners.click) {
  36. // 外部自定义触发getCode
  37. this.$emit('click', this.getCode);
  38. return;
  39. }
  40. this.getCode();
  41. },
  42. async getCode() {
  43. //注意:用户可能在倒计时的过程中点击获取验证码的按钮,组件内部提供了通过ref获取的canGetCode变量,在倒计时 过程中,该值为false
  44. if (!this.$refs.uCode.canGetCode) return;
  45. const { phone, codeType } = this;
  46. if (!phone) {
  47. // 手机号为空
  48. this.$c.toast('手机号不能为空');
  49. return;
  50. }
  51. uni.$c.loading('正在获取验证码');
  52. await getAuthCode({
  53. phone,
  54. code: codeType,
  55. });
  56. uni.hideLoading();
  57. uni.$c.toast('验证码已发送');
  58. // 通知验证码组件内部开始倒计时
  59. this.$refs.uCode.start();
  60. },
  61. codeChange(text) {
  62. this.tips = text;
  63. },
  64. },
  65. };
  66. </script>
  67. <style scoped></style>