You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

verification-code.vue 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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,requestNew } from "@/utils/network/request";
  13. import { sendMessage,etcMobileChangeSmsCodeEtc,newMobileSmsCode} 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. type: {
  33. type: String,
  34. default: "1", //1正常发验证码 2 ETC预留手机号验证码发送
  35. },
  36. customerId: {
  37. type: String,
  38. default: "", //1正常发验证码 2 ETC预留手机号验证码发送 3 ETC预留手机号修改-新手机验证码发送
  39. },
  40. });
  41. let interval = null;
  42. const data = reactive({
  43. //倒计时时长
  44. codeDuration: 0,
  45. });
  46. /*发送验证码*/
  47. const sendSmsCode = async () => {
  48. // 有倒计时,则已发送
  49. if (data.codeDuration) {
  50. uni.showModal({
  51. content: `请在${data.codeDuration}秒后重试`,
  52. showCancel: false,
  53. });
  54. return;
  55. }
  56. if (!props.mobile) {
  57. msg("请输入手机号!");
  58. return;
  59. }
  60. if (!checkStr(props.mobile, "mobile")) {
  61. msg("手机号码格式不正确!");
  62. return;
  63. }
  64. if(props.type=="1"){
  65. const options = {
  66. type: 2,
  67. data: { mobile: props.mobile },
  68. method: "POST",
  69. showLoading: true,
  70. };
  71. requestNew(sendMessage, options).then((res) => {
  72. msg("验证码发送成功!");
  73. countdown();
  74. });
  75. }else if(props.type=="2"){
  76. const options1 = {
  77. type: 2,
  78. data: { mobile: props.mobile,
  79. customerId:props.customerId
  80. },
  81. method: "POST",
  82. showLoading: true,
  83. };
  84. requestNew(etcMobileChangeSmsCodeEtc, options1).then((res) => {
  85. msg("验证码发送成功!");
  86. countdown();
  87. });
  88. }else{
  89. const options1 = {
  90. type: 2,
  91. data: { mobile: props.mobile,
  92. customerId:props.customerId
  93. },
  94. method: "POST",
  95. showLoading: true,
  96. };
  97. requestNew(newMobileSmsCode, options1).then((res) => {
  98. msg("验证码发送成功!");
  99. countdown();
  100. });
  101. }
  102. };
  103. /* 倒计时 */
  104. const countdown = () => {
  105. data.codeDuration = 60;
  106. interval = setInterval(() => {
  107. data.codeDuration--;
  108. if (data.codeDuration === 0) {
  109. if (interval) {
  110. clearInterval(interval);
  111. interval = null;
  112. }
  113. }
  114. }, 1000);
  115. };
  116. </script>
  117. <style lang="scss">
  118. .code {
  119. width: 180rpx;
  120. height: 50rpx;
  121. text-align: right;
  122. }
  123. .bg {
  124. background-color: #2ce242;
  125. border-radius: 10rpx;
  126. color: #ffffff;
  127. font-size: 24rpx;
  128. }
  129. .noBg {
  130. color: #00b38b;
  131. background-color: transparent;
  132. font-size: 28rpx;
  133. }
  134. .code-btn {
  135. line-height: 50rpx;
  136. text-align: right;
  137. }
  138. </style>