Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

verification-code.vue 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!-- 获取验证码 -->
  2. <template>
  3. <view class="code as-gravity-center" :class="bg ? 'bg' : 'noBg'">
  4. <text class="code-btn" @click="sendSmsCode">
  5. {{ data.codeDuration ? data.codeDuration + "s" : "获取验证码" }}
  6. </text>
  7. </view>
  8. </template>
  9. <script setup lang="ts">
  10. import { reactive } from "vue";
  11. import { checkStr, msg } from "@/utils/utils";
  12. import { request } from "@/utils/network/request";
  13. import { sendCode } from "@/utils/network/api";
  14. const props = defineProps({
  15. //手机号
  16. mobile: {
  17. type: String,
  18. default: "",
  19. },
  20. //短信模板编码
  21. templateCode: {
  22. type: String,
  23. default: "",
  24. },
  25. //有没有背景色
  26. bg: {
  27. type: Boolean,
  28. default: () => {
  29. return true;
  30. },
  31. },
  32. });
  33. let interval = null;
  34. const data = reactive({
  35. //倒计时时长
  36. codeDuration: 0,
  37. });
  38. /*发送验证码*/
  39. const sendSmsCode = async () => {
  40. // 有倒计时,则已发送
  41. if (data.codeDuration) {
  42. uni.showModal({
  43. content: `请在${data.codeDuration}秒后重试`,
  44. showCancel: false,
  45. });
  46. return;
  47. }
  48. if (!props.mobile) {
  49. msg("请输入手机号!");
  50. return;
  51. }
  52. if (!checkStr(props.mobile, "mobile")) {
  53. msg("手机号码格式不正确!");
  54. return;
  55. }
  56. const options = {
  57. type: 2,
  58. data: { mobile: props.mobile },
  59. method: "POST",
  60. showLoading: true,
  61. };
  62. request(sendCode, options).then((res) => {
  63. msg("验证码发送成功!");
  64. countdown();
  65. });
  66. };
  67. /* 倒计时 */
  68. const countdown = () => {
  69. data.codeDuration = 60;
  70. interval = setInterval(() => {
  71. data.codeDuration--;
  72. if (data.codeDuration === 0) {
  73. if (interval) {
  74. clearInterval(interval);
  75. interval = null;
  76. }
  77. }
  78. }, 1000);
  79. };
  80. </script>
  81. <style lang="scss">
  82. .code {
  83. width: 180rpx;
  84. height: 50rpx;
  85. text-align: right;
  86. }
  87. .bg {
  88. background-color: #2ce242;
  89. border-radius: 10rpx;
  90. color: #ffffff;
  91. font-size: 24rpx;
  92. }
  93. .noBg {
  94. color: #00b38b;
  95. background-color: transparent;
  96. font-size: 28rpx;
  97. }
  98. .code-btn {
  99. line-height: 50rpx;
  100. text-align: right;
  101. }
  102. </style>