ie-sms.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <view :class="['ie-sms text-primary text-30', customClass]" @click="handleSend">{{ smsTip }}</view>
  3. <ie-captcha ref="captchaRef" v-model:code="code" v-model:uuid="uuid" @valid="handleValid" />
  4. </template>
  5. <script lang="ts" setup>
  6. import { useAppConfig } from '@/hooks/useAppConfig';
  7. import { useSms } from '@/hooks/useSms';
  8. import IeCaptcha from './ie-captcha.vue';
  9. import { EnumSmsApiType, EnumSmsType } from '@/common/enum';
  10. import { SmsRequestDTO } from '@/types/user';
  11. import { validatePhone } from '@/hooks/useValidation';
  12. const sms = useSms();
  13. const { smsTip, isCountingDown, smsIsSending } = sms;
  14. const appConfig = useAppConfig();
  15. defineOptions({
  16. virtualHost: true,
  17. })
  18. const props = defineProps({
  19. phone: {
  20. type: String,
  21. default: '',
  22. required: true
  23. },
  24. smsApiType: {
  25. type: String as PropType<EnumSmsApiType>,
  26. default: EnumSmsApiType.NO_VALIDATION_NO_TOKEN
  27. },
  28. smsType: {
  29. type: String as PropType<EnumSmsType>,
  30. default: EnumSmsType.CODE
  31. },
  32. customClass: {
  33. type: String,
  34. default: ''
  35. },
  36. beforeSend: {
  37. type: Function,
  38. default: () => true
  39. }
  40. });
  41. const captchaRef = ref();
  42. const uuid = ref('');
  43. const code = ref<string>('');
  44. const handleValid = (data: { code: string; uuid: string }) => {
  45. handleSubmit();
  46. }
  47. const handleSubmit = () => {
  48. if (appConfig.isSmsCaptchaEnable.value && !code.value) {
  49. uni.$ie.showToast('请输入验证码');
  50. return;
  51. }
  52. uni.$ie.showLoading();
  53. const params: SmsRequestDTO = {
  54. mobile: props.phone,
  55. smsType: props.smsType,
  56. uuid: uuid.value,
  57. code: code.value
  58. };
  59. sms.sendSms(props.smsApiType, params).then(res => {
  60. uni.$ie.hideLoading();
  61. if (res.success) {
  62. captchaRef.value.close();
  63. uni.$ie.showToast('短信发送成功');
  64. emit('send', props.phone, code.value, uuid.value);
  65. } else if (res.error.msg === '手机号已存在') {
  66. captchaRef.value.close();
  67. } else {
  68. captchaRef.value.changeImage();
  69. }
  70. }).catch((err) => {
  71. uni.$ie.hideLoading();
  72. });
  73. }
  74. const emit = defineEmits<{
  75. send: [phone: string, code: string, uuid: string]
  76. }>()
  77. const handleSend = async () => {
  78. if (props.beforeSend && !props.beforeSend()) {
  79. return;
  80. }
  81. if (!props.phone) {
  82. uni.$ie.showToast('请输入手机号');
  83. return;
  84. }
  85. if (!validatePhone(props.phone)) {
  86. uni.$ie.showToast('请输入正确的手机号');
  87. return;
  88. }
  89. if (smsIsSending.value || isCountingDown.value) {
  90. return;
  91. }
  92. if (appConfig.isSmsCaptchaEnable.value) {
  93. captchaRef.value.open();
  94. } else {
  95. handleSubmit();
  96. }
  97. }
  98. onLoad(() => {
  99. sms.init();
  100. });
  101. </script>
  102. <style lang="scss" scoped></style>