手机发送验证码

<template>
  <div class="wrap">
    <el-form
      class="loginForm"
      :model="loginForm"
      :rules="loginFormRules"
      ref="loginFormRef"
      label-position="top"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="loginForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="手机验证码" prop="verificationCode">
        <el-input v-model="loginForm.verificationCode">
          <template slot="append">
            <el-button v-if="loginForm.showloginCode" type="primary" @click="getloginPhoneCode">获取验证码</el-button>
            <div v-else>{{ loginForm.count }}</div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item class="btns">
        <el-button type="primary" @click="login">登录dsfd</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    // 验证手机号是否合法
    var checkMobile = (rules, value, callback) => {
      const regMobile = /^(0|86|17951)?(13[0-9]|15[0123456789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
      if (regMobile.test(value) == true) {
        return callback();
      } else {
        callback(new Error("请输入合法的手机号"));
      }
    };
    // 验证输入的手机号验证码是否和存储的验证码相同
    var checkPhoneCode = (rules, value, callback) => {
      if (value == this.loginForm.contenttext) {
        return callback();
      } else {
        callback(new Error("验证码错误"));
      }
    };
    return {
      // 表单
      loginForm: {
        phone: "",
        verificationCode: "", //表单中展示的验证码
        contenttext: "", //向手机号发送的随机验证码
        timer: null,
        showloginCode: true, //判断展示‘获取验证码’或‘倒计时’
        count: "", //倒计时时间
      },
      // 验证规则
      loginFormRules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { validator: checkMobile, trigger: "blur" },
        ],
        verificationCode: [
          { required: true, message: "请输入手机验证码", trigger: "blur" },
          { validator: checkPhoneCode, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 获取手机验证码
    getloginPhoneCode() {
      // 如果未输入手机号,结束执行
      if (this.loginForm.phone == "") {
        return;
      }
      // 获取随机数(4位数字)
      var numCode = "";
      for (var i = 0; i < 4; i++) {
        numCode += Math.floor(Math.random() * 10);
      }
      // 存储发送的验证码,用于验证输入的手机验证码是否和本地存储的相同
      this.loginForm.contenttext = numCode;
      // 向手机号发送验证码传入的参数
      let phoneCode = {
        phonenum: this.loginForm.phone,
        contenttext: "您正在修改密码,验证码为:" + numCode + ",切勿将验证码泄露给他人。",
      };
      // 调用接口,向手机号发送验证码
      this.$axios.post("接口地址", phoneCode).then((res) => {
        if (res.status != 200) {
          return this.$message.error("验证码发送失败!");
        } else {
          // 当验证码发送成功,开始60秒倒计时
          const TIME_COUNT = 60;
          if (!this.loginForm.timer) {
            this.loginForm.showloginCode = false;
            this.loginForm.count = TIME_COUNT;
            this.loginForm.timer = setInterval(() => {
              if (
                this.loginForm.count > 0 &&
                this.loginForm.count <= TIME_COUNT
              ) {
                this.loginForm.count -= 1;
              } else {
                this.loginForm.showloginCode = true;
                clearInterval(this.loginForm.timer);
                this.loginForm.timer = null;
              }
            }, 1000);
          }
        }
      });
    },
    // 开始登录
    login() {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          console.log("开始登录");
        } else {
          console.log("error submit!!");
        }
      });
    },
  },
};
</script>
<style scoped>
.loginForm {
  width: 500px;
  margin: 0 auto;
}
.btns {
  text-align: right;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用 JavaScript手机发送验证码,需要借助短信平台提供的 API 接口。一般来说,需要先在短信平台注册账,并获取相应的 API Key 和 Secret Key。以阿里云短信服务为例,下面给出一个简单的示例代码: ``` // 引入阿里云 SDK const Core = require('@alicloud/pop-core'); // 配置 SDK const client = new Core({ accessKeyId: '<your-access-key-id>', accessKeySecret: '<your-access-key-secret>', endpoint: 'https://dysmsapi.aliyuncs.com', apiVersion: '2017-05-25' }); // 发送短信验证码 const params = { RegionId: 'cn-hangzhou', PhoneNumbers: '<your-phone-number>', SignName: '<your-sign-name>', TemplateCode: '<your-template-code>', TemplateParam: '{code:<your-verification-code>}' }; const requestOption = { method: 'POST' }; client.request('SendSms', params, requestOption).then((result) => { console.log(result); }, (ex) => { console.log(ex); }); ``` 在代码中,需要将 `<your-access-key-id>` 和 `<your-access-key-secret>` 替换成阿里云短信服务平台上的 API Key 和 Secret Key,将 `<your-phone-number>` 替换成目标手机码,将 `<your-sign-name>` 替换成已经注册的短信签名,将 `<your-template-code>` 替换成已经创建的短信模板编码,将 `<your-verification-code>` 替换成生成的验证码。 需要注意的是,短信平台一般会限制每天或者每小时发送短信的数量,需要根据实际情况进行调整。同时,短信发送也会产生一定的费用,需要开通相应的计费服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值