vue+element-ui 登录流程分析

登录流程分析

在这里插入图片描述

  1. 页面结构的搭建,样式的美化
<template>
<el-form class="login-form"
		 :model="loginForm"
		 auto-complete="on" //自动补全
		 label-position="left">
		 
	<!--手机号输入框 -->
	<el-form-item>
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="mobile"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      
      <!--密码输入框 -->
      <el-form-item>
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

	<el-button class="loginBtn" 
			   :loading="loading" 
			   type="primary" 
			   style="width:100%;margin-bottom:30px;">
			   @click.native.prevent="handleLogin"//这里绑定事件
			   登录
	</el-button>

</template>

//这个代码是从element-ui粘贴过来的,是表单中输入框中的数据
export default {
	name: 'Login',
	data() {
		return {
			loginForm: {
	        mobile: '13111111111',
	        password: '123456'
      		},
      		loading: false,
	        passwordType: 'password',
	        redirect: undefined
		}
	}
}
这里的样式一部分是全局的样式,一部分是当前组件的样式
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.7);
    border-radius: 5px;
    color: #454545;
  }
  .el-form-item__error {
	color: #fff
}
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
// $light_gray:#eee;
$light_gray: #68b0fe;
.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url('~@/assets/common/login.jpg'); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .loginBtn {
  background: #407ffe;
  height: 64px;
  line-height: 32px;
  font-size: 24px;
}
}
</style>

  • 当我们把基本的静态页面都写好以后,我们就可以根据需求去写代码了
  • 我们主要的需求是:用户点击登录按钮,输入手机号和密码,然后跳转到主页面,但是在程序员的角度,在这个过程中进行了很多细节上面的操作,接下来我们就一步一步的完善这个功能

1.表单获取的数据,在上面我们已经写好了
2.进行表单的验证,对于用户输入的内容我们需要做一个校验,是否输入的合法,正确,校验是一个比较繁琐的过程,在这里我们把校验分为3次

  1. 第一次我们先用element-ui做基础的校验
这是结构部分
|------- el-form     	 绑定model 和 rules规则
|------- el-form-item    绑定prop属性
|------- el-input  		 绑定v-model
<template>
<el-form class="login-form"
		 :model="loginForm"	//绑定model和rules
		 :rules="loginRules"
		 auto-complete="on" 
		 label-position="left">
	<!--手机号输入框 -->
	<el-form-item prop="mobile">//绑定prop属性
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"//绑定v-model
          placeholder="mobile"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <!--密码输入框 -->
      <el-form-item>
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
</templet>
      
script部分
<script>
data(){
  return{
	loginRules: {
	  mobile:[{required:true, trigger:'blur',message:'手机号不能为空'},
	  						//这里使用了校验规则
	          { validator: **validateMobile**, trigger: 'blur' }],
	  password:[{required: true,trigger:'blur',message:'密码不能为空'},
	      {min:6,max:16,message:'密码的长度在6-16位之间',trigger:'blur'}]
	      }
		}
	}
	
</script>

2.基本的校验完成后,我们还会单独定义做一个自定义的校验,以确保校验的准确,合法

增加校验方法
utils/validate.js添加一个函数

export function validMobile(str) {
  return /^1[3-9]\d{9}$/.test(str) // 校验手机号
}

登录组件使用校验方法

``
// 导入校验函数
import { validMobile } from '@/utils/validate' 
data() {
    // 自定义校验函数
    const validateMobile = function(rule, value, callback) {
      // 校验value
      if (validMobile(value)) {
      	callback()
      } else {
       	callback(new Error('手机号格式不正确'))
      }
    }
    }
`

3.紧接着,我们要向服务器发送请求,把信息提交给服务器

// 登录接口,根据接口文档传递参数,接口地址
export function login(data) {
  return request({
    url: '接口地址',
    method: '请求方式',
    //请求参数
    data
  })
}
//这里我把axios方法单独写了一个doLogin方法,可以让结构清晰一些
//先导入方法,这里把所有的接口封装在了js中,可以方便使用
import {login} from '@/api/user.js'
async doLogin() {
      try {
        await login(this.loginForm)
        // 登录成功,跳转到登录页面
        this.$router.push('/')
      } catch (err) {
        console.log('登录错误', err)
      }
    },
     handleLogin() {
      // 这里做了一个兜底校验
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.doLogin()
        } else {
          console.log('error')
          return false
        }
      })
    }

好啦,这里登录接口就到这里啦!!欢迎大佬指出不足,我会加以改进!!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值