广告
返回顶部
首页 > 资讯 > 精选 >vue怎么实现手机验证码登录
  • 136
分享到

vue怎么实现手机验证码登录

2023-06-25 17:06:18 136人浏览 薄情痞子
摘要

这篇文章主要介绍“Vue怎么实现手机验证码登录”,在日常操作中,相信很多人在vue怎么实现手机验证码登录问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vue怎么实现手机验证码登录”的疑惑有所帮助!接下来,请跟

这篇文章主要介绍“Vue怎么实现手机验证码登录”,在日常操作中,相信很多人在vue怎么实现手机验证码登录问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vue怎么实现手机验证码登录”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

验证码

<template>  <div>    <el-main>      <el-fORM :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">        <el-form-item label="手机号" prop="phone">          <el-input v-model="ruleForm.phone" placeholder="请输入手机号" size=""                    maxlength="11"></el-input>        </el-form-item>        <el-form-item label="验证码" prop="code">          <el-row :span="24">            <el-col :span="12">              <el-input v-model="ruleForm.code" auto-complete="off" placeholder="请输入验证码" size=""                        maxlength="4"                        @keyup.enter.native="submitForm('ruleForm')"></el-input>            </el-col>            <el-col :span="12">              <div class="login-code">                <!--验证码组件-->                <el-button @click="getCode()" :class="{'disabled-style':getCodeBtnDisable}"                           :disabled="getCodeBtnDisable">{{ codeBtnWord }}                </el-button>              </div>            </el-col>          </el-row>        </el-form-item>        <!--滑动验证组件-->        <Verify></Verify>        <el-form-item>          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>        </el-form-item>      </el-form>    </el-main>  </div></template>

用到的vue验证工具

export default {  // 限制只能输入数字(可以输入两位小数点)  limitInputPointNumber(val) {    if (val === 0 || val === "0" || val === "" || val === undefined) {      return "";    } else {      let value = null;      value = val.replace(/[^\d.]/g, ""); // 清除“数字”和“.”以外的字符      value = value.replace(/\.{2,}/g, "."); // 只保留第一个. 清除多余的      value = value        .replace(".", "$#$")        .replace(/\./g, "")        .replace("$#$", ".");      value = value.replace(/^(-)*(\d+)\.(\d\d).*$/, "$1$2.$3"); // 只能输入两个小数      return value;    }  },  handleRouteToArray(route) {    const matchs = route.path.split('/')    matchs.shift()    let newMatch = []    matchs.map((item, i) => {      if (matchs[i - 1]) {        item = newMatch[i - 1] + '/' + item      }      newMatch.push(item)    })    newMatch = newMatch.map(item => {      return `/${item}`    })    return newMatch  },  //  密码长度8位以上,须包含大写、小写、数字、特殊符号中的任意3种  testPassWord: function (str) {    var rC = {      lW: '[a-z]',      uW: '[A-Z]',      nW: '[0-9]',      sW: '[\\u0020-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E]'    }    function Reg (str, rStr) {      var reg = new RegExp(rStr)      if (reg.test(str)) return true      else return false    }    if (str.length < 8) {      return false    } else {      var tR = {        l: Reg(str, rC.lW),        u: Reg(str, rC.uW),        n: Reg(str, rC.nW),        s: Reg(str, rC.sW)      }      if ((tR.l && tR.u && tR.n) || (tR.l && tR.u && tR.s) || (tR.s && tR.u && tR.n) || (tR.s && tR.l && tR.n)) {        // document.title = "密码符合要求"        return true      } else {        return false      }    }  },  // 密码验证8-30位任意字符  pwdReg: /^([0-9a-zA-Z]|(?:&)|(?:~)|(?:!)|(?:@)|(?:#)|(?:\$)|(?:%)|(?:\^)|(?:\*)){8,30}$/,  // 电话号码验证  phoneReg: /^1[3|4|5|6|7|8][0-9]{9}$/,  // 格式化金钱  formatUSD (val, currency) {    if (val === '' || val === '--' || val === undefined) {      return '--'    }    // 先判断数据是否有小数点    let newVal = String(Number(val).toFixed(2))    // 将科学计数转为正常的字符串显示    if (newVal.includes('e+')) {      newVal = Number(newVal).toLocaleString()      newVal = this.unFormatAmount(newVal)    }    let dotIdx = newVal.lastIndexOf('.')    let dotVal = '.00' // 保留小数点后面的数据    if (dotIdx >= 0) {      dotVal = newVal.substr(dotIdx, newVal.length)      newVal = newVal.slice(0, dotIdx)    }    let len = newVal.length    let arr = []    let lastIndex = null    while (len > 0) {      lastIndex = len      len -= 3      arr.unshift(newVal.substring(len, lastIndex))    }    val = arr.join(',')    if (currency) {      newVal = `${currency} ${val}${dotVal}`    } else {      // newVal = `$ ${val}${dotVal}`      newVal = `¥ ${val}${dotVal}` // 默认人民币    }    return newVal  },  // 格式化金额数字,不包含小数点,金额符等 输入整数  formatAmount (val) {    if (val === '' || val === '--' || val === undefined) {      return '--'    }    if (val === 0 || val === '0') {      return 0    }    // 先判断数据是否有小数点    let newVal = String(val)    let dotIdx = newVal.lastIndexOf('.')    let dotLength = 0    if (newVal.split('.').length > 1) {      dotLength = newVal.split('.')[1].length    }    let dotVal = '' // 保留小数点后面的数据    if (dotIdx >= 0) {      newVal = String(Number(val).toFixed(5))      dotVal = newVal.substr(dotIdx, dotLength + 1)      newVal = newVal.slice(0, dotIdx)    }    let len = newVal.length    let arr = []    let lastIndex = null    while (len > 0) {      lastIndex = len      len -= 3      arr.unshift(newVal.substring(len, lastIndex))    }    val = arr.join(',')    if (dotVal.length < 2) {      dotVal = ''    }    return val + dotVal  },  // 判断数据是否为空  isEmptyVal (val) {    if (val === undefined || val === '') {      return '--'    } else {      return val    }  },    // 格式化年月日   type: 中国显示方式(ch)及拼接的方式  // 注: 只有在接口传参时才需要中国的显示方式,其它为美式  formatYMD (now, type='ch') {    if (!now || now === 'null' || now === '--' || now === undefined) {      return '--'    }    if (Number(now)) {      now = new Date(now)    }    // 兼容IE浏览器 , YY-MM-DD 格式    if (typeof now === 'string' && now.includes('-')) {      now = this.NewDate(now)    }    if (now) {      let year = ''      let month = ''      let date = ''      // 这里的8位用于返回如 20180423 这样的格式      if (String(now).length === 8 && String(now).indexOf('-') === -1 && String(now).indexOf('/') === -1) {        const getNow = String(now)        return `${getNow.substring(4, 6)}/${getNow.substring(6, 8)}/${getNow.substring(0, 4)}`      } else {        now = new Date(now)        year = now.getFullYear()        month = now.getMonth() + 1        date = now.getDate()      }      if (month < 10) {        month = `0${month}`      }      if (date < 10) {        date = `0${date}`      }      if (type === 'ch') {        return `${year}-${month}-${date}`      } else if (type) {        return `${year}${type}${month}${type}${date}`      } else {        return `${month}/${date}/${year}`      }    } else {      return ''    }  },  // 格式化时间 年,月,日,时,分,秒  formatDate (now, type) {    if (!now || now === 'null' || now === '--' || now === undefined) {      return '--'    }    if (now) {      now = new Date(now)      let year = now.getFullYear()      let month = now.getMonth() + 1      let date = now.getDate()      let hour = now.getHours()      let minute = now.getMinutes()      let second = now.getSeconds()      if (month < 10) {        month = `0${month}`      }      if (date < 10) {        date = `0${date}`      }      if (hour < 10) {        hour = `0${hour}`      }      if (minute < 10) {        minute = `0${minute}`      }      if (second < 10) {        second = `0${second}`      }      if (type) {        return `${month}/${date}/${year} ${hour}:${minute}:${second}`      } else {        return `${month}-${date}-${year}`      }    } else {      return ''    }  },}

直接放上完整的这样有助于看

<template>  <div>    <el-main>      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">        <el-form-item label="手机号" prop="phone">          <el-input v-model="ruleForm.phone" placeholder="请输入手机号" size=""                    maxlength="11"></el-input>        </el-form-item>        <el-form-item label="验证码" prop="code">          <el-row :span="24">            <el-col :span="12">              <el-input v-model="ruleForm.code" auto-complete="off" placeholder="请输入验证码" size=""                        maxlength="4"                        @keyup.enter.native="submitForm('ruleForm')"></el-input>            </el-col>            <el-col :span="12">              <div class="login-code">                <!--验证码组件-->                <el-button @click="getCode()" :class="{'disabled-style':getCodeBtnDisable}"                           :disabled="getCodeBtnDisable">{{ codeBtnWord }}                </el-button>              </div>            </el-col>          </el-row>        </el-form-item>        <!--滑动验证组件-->        <Verify></Verify>        <el-form-item>          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>        </el-form-item>      </el-form>    </el-main>  </div></template><script>//导入工具类import Verify from "@/components/Verify";import event from "../utils/event"import common from "@/utils/common";let params;export default {  name: "LoginIphone",  components: {Verify},  data() {    //使用正则表达式验证手机号    const checkPhone = (rule, value, callback) => {      if (!value) {        return callback(new Error('手机号不能为空'));      } else {        //获取工具类中的手机号正则表达式        const reg = common.phoneReg        // console.log(reg.test(value));        if (reg.test(value)) {          callback();        } else {          //如果验证输入错误就清空          this.ruleForm.phone = ''          return callback(new Error('请输入正确的手机号'));        }      }    };    return {      ruleForm: {        phone: '',        code: '',      },      codeBtnWord: '获取验证码', // 获取验证码按钮文字      // waitTime: 61, // 获取验证码按钮失效时间      waitTime: 2, // 获取验证码按钮失效时间      // 校验      rules: {        phone: [          {validator: checkPhone, trigger: 'blur'}        ],        code: [          {required: true, message: '请输入验证密码', trigger: 'blur'}        ]      }    };  },  //计算属性computed  computed: {    // 控制获取验证码按钮是否可点击    getCodeBtnDisable: {      //设置按钮61s      // get() {      //   if (this.waitTime === 61) {      //     if (this.ruleForm.phone) {      //       return false      //     }      //     return true      //   }      //   return true      // }      get() {        if (this.waitTime === 2) {          if (this.ruleForm.phone) {            return false          }          return true        }        return true      },      // 注意:因为计算属性本身没有set方法,不支持在方法中进行修改,而下面我要进行这个操作,所以需要手动添加      set() {      }    },  }, methods: {    getCode() {      const _this = this      params = {}      params.phone = this.ruleForm.phone      // 调用获取短信验证码接口      _this.$axiOS.post('/sendMessage', params).then(res => {        console.log("--------查看后台响应的值-----", res)        //把所有的数据存在        const mydata = res.data.data        console.log("我在短信接口这利-->", mydata)        //保存验证码        params.yz = mydata.vCode        console.log("我是查看验证码-------" + mydata.vCode)        console.log("我是查看调用的次数-------" + mydata.count)        if (res.data.code === 200) {          this.$message({            message: '验证码已发送,请稍候...',            type: 'success',            center: true          })        }        if (res.data.data.count >= 5) {          //调用滑块验证的组件          event.$emit("VERIFY")        }      })      // 因为下面用到了定时器,需要保存this指向      let that = this      that.waitTime--      that.getCodeBtnDisable = true      this.codeBtnWord = `${this.waitTime}s 后重新获取`      let timer = setInterval(function () {        if (that.waitTime > 1) {          that.waitTime--          that.codeBtnWord = `${that.waitTime}s 后重新获取`        } else {          clearInterval(timer)          that.codeBtnWord = '获取验证码'          that.getCodeBtnDisable = false          // that.waitTime = 61          that.waitTime = 2        }      }, 1000)    },    submitForm(formName) {      const _this = this      //判断输入的验证码是否为空      if (this.ruleForm.code != null) {        this.$refs[formName].validate((valid) => {          if (valid) {            _this.$axios.post("/iosLogin", {              "phone": this.ruleForm.phone,              "Verification": this.ruleForm.code            }).then(res => {              console.log(res.data)            })            // console.log("我是提交里面的:", par)            //            // const Jwt = par.headers['authorization']            // console.log("我是token->", jwt)            // const userInfo = par.data.data            // console.log("查看用户信息=", userInfo)            //            // // 把数据共享出去            // _this.$store.commit("SET_TOKEN", jwt)            // _this.$store.commit("SET_USERINFO", userInfo)            //            // // 获取            // console.log("我是获取的_this.$store.getters.getUser")            // console.log(_this.$store.getters.getUser)            // _this.$router.push("/blogs")          } else {            console.log('error submit!!');            return false;          }        });      } else {        this.$message({          showClose: true,          message: '请输入错误',          type: 'error'        });      }    }  }}</script><style scoped>.el-button.disabled-style {  background-color: #EEEEEE;  color: #CCCCCC;}.demo-ruleForm {  max-width: 500px;  margin: 0 auto;}</style>

到此,关于“vue怎么实现手机验证码登录”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: vue怎么实现手机验证码登录

本文链接: https://www.lsjlt.com/news/306156.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • vue怎么实现手机验证码登录
    这篇文章主要介绍“vue怎么实现手机验证码登录”,在日常操作中,相信很多人在vue怎么实现手机验证码登录问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vue怎么实现手机验证码登录”的疑惑有所帮助!接下来,请跟...
    99+
    2023-06-25
  • vue实现手机验证码登录
    本文实例为大家分享了vue实现手机验证码登录的具体代码,供大家参考,具体内容如下 验证码 <template> <div> <el-ma...
    99+
    2022-11-12
  • vue+Element怎么实现登录随机验证码
    今天小编给大家分享一下vue+Element怎么实现登录随机验证码的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。验证码验证只...
    99+
    2023-06-29
  • vue+Element实现登录随机验证码
    本文实例为大家分享了vue+Element实现登录随机验证码的具体代码,供大家参考,具体内容如下 验证码验证只是前端,无需后台交互 首先,创建一个identify.vue页面,用于写...
    99+
    2022-11-13
  • vue实现登录验证码
    本文实例为大家分享了vue实现登录验证码的具体代码,供大家参考,具体内容如下 先来demo效果图 canvas验证码组件(可直接复制,无需改动) <template>...
    99+
    2022-11-12
  • vue+springboot实现登录验证码
    本文实例为大家分享了vue+springboot实现登录验证码的具体代码,供大家参考,具体内容如下 先看效果图 在login页面添加验证码html 在后端pom文件添加kaptc...
    99+
    2022-11-12
  • Vue如何实现验证码登录
    本文小编为大家详细介绍“Vue如何实现验证码登录”,内容详细,步骤清晰,细节处理妥当,希望这篇“Vue如何实现验证码登录”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。效果展示第一步:创建验证码组件这里是组件的代码...
    99+
    2023-06-29
  • vue实现图形验证码登录
    本文实例为大家分享了vue实现图形验证码登录的具体代码,供大家参考,具体内容如下 1、效果图 2、在components下面新建文件identify.vue,内容: <t...
    99+
    2022-11-12
  • 微信小程序实现手机验证码登录
    我们的微信小程序里面,手机验证码登录已经成为不可缺少的一部门,为此,我写的这个手机验证码登录,这里我结合thinkphp6+微信小程序实现 首先我们进入小程序页面: wxml页面: ...
    99+
    2022-11-13
  • springboot整合shiro多验证登录功能的实现(账号密码登录和使用手机验证码登录)
    1. 首先新建一个shiroConfig shiro的配置类,代码如下: @Configuration public class SpringShiroConfig { ...
    99+
    2022-11-12
  • vue实现登录时图形验证码
    本文实例为大家分享了vue实现登录时图形验证码的具体代码,供大家参考,具体内容如下 效果图: 点击图案可以切换字符 1.新建 Identify.vue 组件 <templat...
    99+
    2022-11-13
  • vue+springboot如何实现登录验证码
    这篇文章主要介绍vue+springboot如何实现登录验证码,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!先看效果图在login页面添加验证码html在后端pom文件添加kaptcha依赖<dependenc...
    99+
    2023-06-15
  • VUE实现token登录验证
    本文实例为大家分享了VUE实现token登录验证的具体代码,供大家参考,具体内容如下 实现这个登录功能的过程真是一波三折,中途出现了bug,整了两三天才解决了问题,心力交瘁,简直一个...
    99+
    2022-11-12
  • TP6+vue-element-admin怎么实现后台登录验证码
    本篇内容介绍了“TP6+vue-element-admin怎么实现后台登录验证码”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!tp6+vue...
    99+
    2023-06-22
  • php怎么实现密码登录验证
    在PHP中,可以使用以下步骤来实现密码登录验证:1. 创建一个HTML表单,包含一个用户名输入框和一个密码输入框。用户输入用户名和密...
    99+
    2023-10-10
    php
  • 微信小程序如何实现手机验证码登录
    本篇内容介绍了“微信小程序如何实现手机验证码登录”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!首先我们进入小程序页面:wxml页面:<...
    99+
    2023-06-30
  • vue实现通过手机号发送短信验证码登录的示例代码
    本文主要介绍了vue实现通过手机号发送短信验证码登录的示例代码,分享给大家,具体如下: <template> <div class="get-mobile...
    99+
    2022-11-13
  • vue实现登录时的图片验证码
    本文实例为大家分享了vue实现登录时的图片验证码的具体代码,供大家参考,具体内容如下 效果图 一、新建vue组件components/identify/identify.vue ...
    99+
    2022-11-12
  • react怎么实现手机验证码
    本教程操作环境:Windows10系统、react18.0.0版、Dell G3电脑。react怎么实现手机验证码?React结合 antd 实现手机或者邮箱获取验证码60秒倒计时我这边是使用了antd button 和input 组件,若...
    99+
    2023-05-14
    验证码 React
  • Android实现验证码登录
    本文实例为大家分享了Android实现验证码登录的具体代码,供大家参考,具体内容如下 结果展示 1.导包 1.1在项目的gradle中导入 maven { url "https...
    99+
    2022-11-11
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作