vue2.0+element搭建后台系统(2)登录拦截

接上一篇,今天的主要目的是配置登录。

准备

vue-router有一个beforeEach,可以在所有路由执行之前,先执行这个方法。所以可以基于这个来实现验证是否登录。
sec/components/login.vue

<template>
  <div class="login-page-container">
    <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-position="left" label-width="0px" class="demo-ruleForm login-container">
      <h3 class="title">系统登录</h3>
      <el-form-item prop="account">
        <el-input type="text" v-model="ruleForm2.account" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>
      <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button type="primary" style="width:100%;" @click="handleSubmit2" :loading="logining">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
  },
  data () {
    return {
      logining: false,
      ruleForm2: {
        account: 'admin',
        checkPass: '123456'
      },
      rules2: {
        account: [{
          required: true,
          message: '请输入账号',
          trigger: 'blur'
        }
          // { validator: validaePass }
        ],
        checkPass: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }
          // { validator: validaePass2 }
        ]
      },
      checked: true
    }
  },
  methods: {
    handleSubmit2 (ev) {
      var _this = this
      _this.$refs.ruleForm2.validate((valid) => {
        if (valid) {
          _this.logining = true
          var loginParams = {
            username: this.ruleForm2.account,
            password: this.ruleForm2.checkPass
          }
          if (loginParams.username === 'admin' && loginParams.password === '123456') {
            _this.logining = false
            sessionStorage.setItem('user', JSON.stringify(loginParams))
            _this.$router.push({ path: '/' })
          } else {
            _this.logining = false
            _this.$alert('用户名或密码错误!', '提示信息', {
              confirmButtonText: '确定'
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
  .login-container {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  label.el-checkbox.remember {
    margin: 0px 0px 35px 0px;
  }
</style>

配置路由
router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import home from '@/components/home'

import login from '@/components/login'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/',
      name: 'home',
      component: home
    }
  ]
})

更改入口文件
src/main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)
Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    sessionStorage.removeItem('user')
  }
  let user = JSON.parse(sessionStorage.getItem('user'))
  if (!user && to.path !== '/login') {
    next({
      path: '/login'
    })
  } else {
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

启动项目:

npm run dev

已经可以看到效果了
vue2.0+element搭建后台系统(2)
输入账号密码:
admin/123456
可以进入主页。

相关文章: