登录的问题,正则表达式

146 阅读1分钟

点击tap进行onLogin登录

<button  :class="[shape=='round'?'round':'']"
@tap="onLogin">登录
 </button>

账户登录

  • 当点击登录时就会触发onlogin函数;
methods: {
onLogin() {
if (!this.userName) {				    this.$tip.toast('请填写用户名'); 
   return;
	}
        
 if(!this.password) {
     this.$tip.toast('请填写密码')
     return;
 }
 this.loading = true ;
 this.login({
 username:this.username,
 password:this.password
 }).then(()=>{
    this.loading = false;
    uni.$u.route({
    
    url:'/pages/index/index',
    type:'redirect'
    })
 }).catch(()=>{
 thi.loading = false;
 })
 },

手机验证码登录

正则表达式的使用

案例:let checkPhone = new RegExp(/^[1]([3-9])[0-9]{9}$/);

  • ^表示行的开头;
  • $表示行的结束
  • 第一个数字是1;
  • 第二个数字3-9中的一个数字;
  • 剩余的9个数字在0-9里面。
<button @tap="loginWay=1">
账户登录
</button>
  • 先判断是否填写手机号,若无提示请输入手机号;
  • 再判断手机号是否正确,用正则表达式来判断;
  • 再判断是否填写短信验证码;
onSMSLogin(){
let checkPhone = new RegExp(/^[1]([3-9])[0-9]{9}$/);
 if (!this.phoneNo || this.phoneNo.length == 0){
  this.$tip.toast('请填写手机号');
  return
 }
if (!checkPhone.test(this.phoneNo)){
this.$tip.toast('请输入正确的手机号')
 return false
}
if (!this.smsCode || this.smsCode.length == 0){
this.$tip.toast('请填写短信验证码')
   return

}
}
  • 把这个手机号phoneNo和验证码SMSCode进行封装在loginParams里面;
let loginParams = {
   mobile:this.phoneNo,
   captcha: this.smsCode
};
this.PhoneLogin(loginParams).then((res)=>{
if (res.datad.success){
   this.$tip.success('登录成功')
   替换¥Router.replaceAll({
   name:'index'
   })
}else {
this.$tip.error(res.data.message);
}
}).catch((err) => {
					let msg = ((err.response || {}).data || {}).message || err.data.message || "请求出现错误,请稍后再试"
					this.$tip.error(msg);
				});
			},