sms-btn.uvue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <slot :disabled="isDisabled" :countdown="countdown" :btnText="btnText">
  3. <cl-button text :disabled="isDisabled" @tap="open">
  4. {{ btnText }}
  5. </cl-button>
  6. </slot>
  7. </template>
  8. <script lang="ts" setup>
  9. import { computed, reactive, ref } from "vue";
  10. import { useUi } from "@/uni_modules/cool-ui";
  11. import { request, type Response, isDark, t, $t, parse } from "@/.cool";
  12. import { sendSmsCode } from "@/api/user";
  13. const props = defineProps({
  14. phone: String
  15. });
  16. const emit = defineEmits(["success"]);
  17. const ui = useUi();
  18. type Captcha = {
  19. visible: boolean;
  20. loading: boolean;
  21. sending: boolean;
  22. img: string;
  23. };
  24. // 验证码
  25. const captcha = reactive<Captcha>({
  26. visible: false,
  27. loading: false,
  28. sending: false,
  29. img: ""
  30. });
  31. // 倒计时
  32. const countdown = ref(0);
  33. // 是否禁用
  34. const isDisabled = computed(() => countdown.value > 0 || props.phone == "");
  35. // 按钮文案
  36. const btnText = computed(() =>
  37. countdown.value > 0 ? $t("{n}s后重新获取", { n: countdown.value }) : t("获取验证码")
  38. );
  39. const code = ref("");
  40. const captchaId = ref("");
  41. // 清空
  42. function clear() {
  43. code.value = "";
  44. captchaId.value = "";
  45. }
  46. // 开始倒计时
  47. function startCountdown() {
  48. countdown.value = 60;
  49. let timer: number = 0;
  50. function fn() {
  51. countdown.value--;
  52. if (countdown.value < 1) {
  53. clearInterval(timer);
  54. }
  55. }
  56. // @ts-ignore
  57. timer = setInterval(() => {
  58. fn();
  59. }, 1000);
  60. fn();
  61. }
  62. // 发送短信
  63. async function send() {
  64. captcha.sending = true;
  65. await request({
  66. url: "/verify/sms/code/send",
  67. method: "POST",
  68. data: {
  69. mobile: props.phone,
  70. randomStr: 12,
  71. messageType: 1
  72. }
  73. })
  74. ui.showToast({
  75. message: t("短信已发送,请查收")
  76. });
  77. startCountdown();
  78. captcha.sending = false;
  79. }
  80. // 打开
  81. function open() {
  82. if (props.phone != "") {
  83. if (/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(props.phone!)) {
  84. captcha.visible = true;
  85. send();
  86. } else {
  87. ui.showToast({
  88. message: t("请填写正确的手机号格式")
  89. });
  90. }
  91. }
  92. }
  93. defineExpose({
  94. open,
  95. send,
  96. startCountdown
  97. });
  98. </script>