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.2KB

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