The application component in vue3 is written to obtain the verification code, and realize the case where the mobile phone can receive the verification code

Table of contents

1. Preparations

2. Write Vue3 components

3. Conclusion


When we are developing web applications, we often encounter situations where we need to obtain mobile phone verification codes. This article will introduce how to use the Vue3 component to realize the function of obtaining the verification code and allowing the user's mobile phone to receive the verification code.

1. Preparations

First, we need a third-party SMS platform to send SMS verification codes. Here we use the Alibaba Cloud SMS service provided by Alibaba Cloud .

After registering and logging into the Alibaba Cloud account, we need to create a SMS signature and SMS template. The SMS signature is the logo to show the enterprise or brand information to the user, and the SMS template is the SMS content template containing variable parameters.

For example, we can create a SMS signature as "My Application" and SMS template as "Verification code is ${code}, valid within 5 minutes" (where ${code} is a variable parameter), and then pass the review.

After completing the above preparations, we can start writing Vue3 components.

2. Write Vue3 components

We're going to create a VerificationCodeVue3 component called that takes the phone number entered by the user and sends it a verification code. Here is the complete code:

<template>
  <div>
    <label>手机号码:</label>
    <input v-model="phoneNumber" type="text">
    <button :disabled="sended" @click="sendCode">{
   
   { buttonText }}</button>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      phoneNumber: '',
      sended: false,
      buttonText: '获取验证码'
    }
  },
  methods: {
    sendCode() {
      // 发送验证码请求
      axios.post('/api/send_code', { phone_number: this.phoneNumber })
        .then(response => {
          // 请求成功后将 sended 设为 true,按钮禁用,并显示倒计时
          this.sended = true;
          this.buttonText = '已发送';
          let countDown = 60;
          const timer = setInterval(() => {
            if (countDown > 0) {
              this.buttonText = `${countDown}s 后重试`;
            } else {
              this.sended = false;
              this.buttonText = '获取验证码';
              clearInterval(timer);
            }
            countDown--;
          }, 1000);
          console.log('验证码已发送');
        })
        .catch(error => {
          console.error(`发送验证码失败:${error.message}`);
        });
    }
  }
}
</script>

In the above code, we use axiosthe library to send a POST request to /api/send_codethe interface, and pass the mobile phone number entered by the user. Set to true if the server successfully sent the captcha, sendedso the user can know if the captcha was sent.

Since the specific SMS platform and interface may be different, you need to modify the code according to the SMS platform you use. In addition, you also need to set up the code that listens to the SMS arrival event, so that the UI can be updated when the user receives the verification code.

3. Conclusion

In this article, we introduced how to use the Vue3 component to realize the function of obtaining the verification code and allowing the user's mobile phone to receive the verification code. We also introduced how to use Alibaba Cloud SMS service to send SMS verification codes.

Of course, this is just a simple example. In actual application, you may need to modify the code according to your own business logic. But I hope this article can help you understand the basic process of Vue3 component development, and provide some help for your future development work.

Guess you like

Origin blog.csdn.net/qq_70703397/article/details/130821858