element十 权限设计

2019-11-28  本文已影响0人  jiahzhon

一、登录页面

  1. login.vue
<template>
  <div style="padding: 20px">
    <el-form :model="form" label-width="120">
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    login() {
      this.$http.post('api/permission/getMenu', this.form).then(res => {
        res = res.data
        if (res.code === 20000) {
          //避免二次登陆
          this.$store.commit('clearMenu')
          //传菜单
          this.$store.commit('setMenu', res.data.menu)
          //生成路由
          this.$store.commit('addMenu', this.$router)
          this.$router.push({ name: 'home' })
        } else {
          this.$message.warning(res.data.message)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  width: 50%;
  margin: auto;
  padding: 45px;
  height: 450px;
  background-color: #fff;
}
</style>

2.路由

    {
      path:"/login",
      name:'login',
      component: ()=> import('@/views/Login/Login.vue')
    }

二、动态返回菜单


  1. 侧边栏 commonAside.vue


    image.png

2.routes index,js只留下无需权限的路由

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

export default new Router({
  routes: [
    {
      path:"/login",
      name:'login',
      component: ()=> import('@/views/Login/Login.vue')
    }
  ]
})

3.操作cookie包,js-cookie

4.tab.js

import Cookie from 'js-cookie'
export default{
    state:{
        //侧栏收缩
        isCollapse:false,
        menu:[],
        currentMenu:null,
        tabList:[
            {
                path:'/',
                name:'home',
                label:'首页',
                icon: 's-home'
            },
        ]
    },
    mutations:{
        //保存菜单
        setMenu(state,val){
            state.menu =val
            //stringify序列化
            Cookie.set('menu',JSON.stringify(val))
        },
        //清除菜单
        clearMenu(state){
            state.menu = []
            Cookie.remove('menu')
        },
        //设置路由
        addMenu(state,router){
            if (!Cookie.get('menu')) {
                return
              }
              //要反序列化
              let menu = JSON.parse(Cookie.get('menu'))
              state.menu = menu
              let currentMenu = [
                {
                  path: '/',
                  component: () => import(`@/views/Main`),
                  children: []
                }
              ]
              console.log(menu)
              menu.forEach(item => {
                if (item.children) {
                  item.children = item.children.map(item => {
                    item.component = () => import(`@/views/${item.url}`)
                    return item
                  })
                  currentMenu[0].children.push(...item.children)
                } else {
                  item.component = () => import(`@/views/${item.url}`)
                  currentMenu[0].children.push(item)
                }
              })
              console.log(currentMenu, 'cur')
              router.addRoutes(currentMenu)
        },
        selectMenu(state, val){
            //过滤掉首页
            if(val.name !== 'home'){
                state.currentMenu = val
                //不让tabs显示重复的东西
                let result = state.tabList.findIndex(item => item.name === val.name)
                result === -1 ? state.tabList.push(val) : ''
            }else{
                state.currentMenu = null
            }
        },
        closeTab(state,val){
            //点击tag上关闭的按钮
            let result = state.tabList.findIndex(item => item.name === val.name)
            state.tabList.splice(result,1)
        },
        collapseMenu(state){
            state.isCollapse = ! state.isCollapse
        }
    },
    actions:{
        
    }
}

5.刷新页面时,在main,js中

new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
  //刷新时动态路由
  created() {
    store.commit('addMenu',router)
  },
})

三、路由守卫判断(判断登录状态)

import Cookie from 'js-cookie'
export default {
  state: {
    token: ''
  },
  mutations: {
    setToken(state, val) {
      state.token = val
      Cookie.set('token', val)
    },
    clearToken(state) {
      state.token = ''
      Cookie.remove('token')
    },
    getToken(state) {
      state.token = Cookie.get('token')
    }
  },
  actions: {}
}
router.beforeEach((to, from, next) => {
  // 防止刷新后vuex里丢失token
  store.commit('getToken')
  // 防止刷新后vuex里丢失标签列表tagList
  store.commit('getMenu')
  let token = store.state.user.token
  // 过滤登录页,防止死循环
  if (!token && to.name !== 'login') {
    next({ name: 'login' })
  } else {
    next()
  }
})

vue-devtools调试工具

上一篇下一篇

猜你喜欢

热点阅读