vue---H5--获取短信验证码及完整登陆流程

需求:填写手机号--手机号码验证码--验证成功-获取验证码-----失败弹窗提示!--- 验证码位数校验 --- 登陆校验(输入框不为空&&校验通过)

 

 1、HTML

<template>
  <div style="padding:40px 30px">
    <div class="situLogo">
      <img src="@/assets/login/situlogo.png" alt="">
    </div>
    <div class="FormSubmit">
       <van-form ref="form" @submit="onSubmit">
                <van-field 
                class="iconfont icon-shoujihao1"
                    v-model="tel"
                    type="tel"
                    clearable
                    name="tel"
                    :rules="userTel"
                    placeholder="请输入手机号" />
                <van-field
                    v-model="sms"
                    center
                    ref="yanzhengCode"
                    :rules="userMsg"
                    clearable
                    class="iconfont icon-yanzhengyanzhengma"
                    placeholder="请输入手机验证码"
                    type="digit"
                    >
                    <template #button>
                        <van-button
                        type="primary"
                        size="small"
                        class="getCode"
                        @click="sendCode"
                        :disabled="ifdisabled"
                        native-type="button">
                        {{codeMsg}}
                        </van-button>
                    </template>
                </van-field>
                <div style="marginTop:119px">
                    <van-button
                    round
                    block
                    type="primary"
                    native-type="submit"
                    ><span style="margin: 0.4267rem;fontSize:20px">登&nbsp;录</span>
                    </van-button>
                </div>
    </van-form>
    </div>
   <div class="footCar">
    <img src="@/assets/login/carlogo.png" alt="">
   </div>
  </div>
</template>

2、JS(需要后端接口:1⃣️获取验证码:参为输入电话号码 2⃣️登陆按钮:参为电话号码&&验证码后端校验返回result)

<script>
import { Form, Field, Button, Toast, CellGroup,} from 'vant'
import {setSms} from '@/api/login'  
import { mapActions  } from 'vuex'
export default {
  components: {
    [Button.name]: Button,
    [Form.name]:Form,
    [Field.name]:Field,
    [Toast.name]:Toast,
    [CellGroup.name]:CellGroup,
    [Toast.name]:Toast
  },
  data() {
    return {
      tel: "",
      sms: "",
      ifdisabled: false,
      codeNum: 60,
      codeMsg: "获取短信验证码",
      count:'',
      userTel: [
        {
          required: true,
          message: "请输入手机号",
          trigger: "onBlur"
        },
        {
          validator: value => {
            return /^1[23456789]\d{9}$/.test(value);
          },
          message: "手机号格式错误,请检查!"
        }
      ],
      userMsg:[{
          required: true,
          message: "请输入验证码",
          trigger: "onBlur"
        },  {
          validator: value => {
            return /^\d{6}$/.test(value);
          },
          message: "验证码格式错误,请检查!"
        }]
    };
  },
  methods: {
   ...mapActions(['userLogin']),
    // 获取短信验证码
   async sendCode() {
      // 验证手机号格式是否正确
      if(!this.tel){
        Toast('请输入手机号!')
        return
      }
      if (!this.userTel[1].validator(this.tel)) return;
      //发送请求
    await setSms({phoneNo:this.tel}).then(res=>{
  if(res.code===10000){
     Toast.success('验证码已发送!');
       // 禁用按钮
      this.ifdisabled = true;
      // 倒计时
      this.count=this.codeNum
      let timer=setInterval(()=>{
        if(this.count>1 &&this.count<=this.codeNum){
          this.count--
          this.codeMsg=`重新发送(${this.count})`
        }else{
      // 判断什么时候停止定时器
          clearInterval(timer)
          this.codeMsg='获取短信验证码'
          this.ifdisabled=false
          this.count=''
        }
      },1000)
  }
  else{
    Toast.fail(res.msg);
  }
})
    },
    async onSubmit(){
      //登陆按钮
  // eslint-disable-next-line no-undef
    await this.userLogin({phoneNo:this.tel,validCode:this.sms}).then(()=>{
 this.$router.push( '/Sceneselection')
    })
    }
  }
    }
</script>

3、Vuex

  actions: {
    userLogin({ commit }, userInfo) {
      const { phoneNo, validCode } = userInfo
      return new Promise((resolve, reject) => {
        login({phoneNo, validCode}).then(res=> {
          if(res.code === 10000) {
            commit('SET_TOKEN', res.result.token)
            setToken(res.result.token)
            resolve()
          } else {
            Toast(res.msg)
            reject(res.msg)
          }
        })
      })
    },
}

4、CSS

<style>
.van-field__control{
  font-size: 16px;
  margin-left: 15px;
}
.situLogo{
  margin:40px auto;
  width:218px;
  height:108px;
}
.getCode{
  border-radius: 10px;
}
.FormSubmit{
  margin-top: 55px;
}
.van-button{
  background: #3A6DD4;
}
.footCar{
  position: fixed;
  bottom: 0px;
  right: 0px;
}
</style>

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-awesome-swiper是一个基于Vue.js的插件,用于实现H5滑动翻页效果。它是在swiper的基础上进行了封装,使得在Vue项目中使用swiper变得更加方便和灵活。 要在vue中使用vue-awesome-swiper,首先需要在main.js中引入VueAwesomeSwiper插件,并导入swiper的样式文件。具体的代码如下: import VueAwesomeSwiper from 'vue-awesome-swiper' import 'swiper/dist/css/swiper.css' Vue.use(VueAwesomeSwiper) 这样就可以在Vue组件中使用vue-awesome-swiper来实现H5滑动翻页效果了。你可以在需要使用swiper的组件中引入并注册vue-awesome-swiper的组件,然后使用组件的方式来配置和控制swiper的各种参数和功能。通过配置不同的选项,你可以实现各种不同的滑动翻页效果,满足你的需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue案例 - vue-awesome-swiper实现h5滑动翻页效果](https://blog.csdn.net/weixin_33881050/article/details/93267999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue移动端使用swiper+vue-awesome-swiper实现滑动选择](https://blog.csdn.net/weixin_45842078/article/details/119141775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值