Vue功能篇- 8.发送短信验证码组件封装

公用的短信验证码模块

无需多言,简单好用!

<template>
  <!-- <el-button class="loginright_btn nopadding" type="primary" @click="getPhoneCode()" :disabled="BtnSms">
    {{smsTip}}
  </el-button> -->
  <span class="yanzhengcode" @click="getPhoneCode()" :disabled="BtnSms">{{smsTip}}</span>
</template>

<script>
//可删除,是我引用的封装好的api方法
import LoginApi from "@/api_platform/login.js";
export default {
  props: {
    phoneNum: {
      type: String,
      default: ""
    },
    type: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      count: 60, //当前倒计时
      BtnSms: false, //控制验证码按钮是否可用
      timer: "", //定时器名称
      smsTip: "获取验证码" //验证码文本
    };
  },

  methods: {
    //获取手机验证码
    getPhoneCode: function() {
      if (!/^1[3456789]\d{9}$/.test(this.phoneNum)) {
        this.$notify.error({
          title: "温馨提示",
          message: "请检查手机号是否填写正确!",
          offset: 50,
          duration: 2500
        });
      } else {
        this.getSmsCode();
      }
    },
    /**
     * 获取手机验证码
     */
    getSmsCode: function() {
      var thisApp = this;
      //此处为请求后端的API方法,自行替换
      LoginApi.getPhoneCode(thisApp.phoneNum, thisApp.type)
        .then(response => {
          if (response.data.state == 1) {
            thisApp.$notify.warning({
              title: "温馨提示",
              message: response.data.message,
              offset: 50,
              duration: 2500
            });
            this.countDownTimer();
          } else {
            thisApp.$notify.warning({
              title: "温馨提示",
              message: response.data.message,
              offset: 50,
              duration: 2500
            });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },

    //获取验证码倒计时
    countDownTimer: function() {
      this.BtnSms = true;
      this.smsTip = this.count + "秒后重新获取";
      this.timer = setInterval(() => {
        this.count -= 1;
        if (this.count > 0) {
          this.smsTip = this.count + "秒后重新获取";
        } else {
          this.BtnSms = false;
          this.smsTip = "获取验证码";
          this.count = 60;
          clearInterval(this.timer);
        }
      }, 1000);
    }
  }
};
</script>
<style>
.loginright_btn {
  width: 98px;
  height: 36px;
  border-radius: 4px;
  background-color: rgba(140, 0, 255, 1);
  color: #fff;
  position: absolute;
  border: 0;
  right: 4px;
  top: 14px;
  text-align: center;
  text-decoration: none;
  line-height: 24px;
  font-size: 13px;
}

.el-table {
  padding: 0;
}
</style>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

情系半生e

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值