Commit 411f96a5af8a69c3f68a27ce62f4ecb2fc5f2724
1 parent
13061512
登录状态管理
Showing
4 changed files
with
68 additions
and
13 deletions
webintroduce/src/components/VHeader.vue
webintroduce/src/main.js
| ... | ... | @@ -3,14 +3,33 @@ |
| 3 | 3 | import Vue from 'vue' |
| 4 | 4 | import App from './App' |
| 5 | 5 | import router from './router' |
| 6 | - | |
| 6 | +import Cookies from 'js-cookie' | |
| 7 | 7 | require ('./assets/css/reset.css') |
| 8 | 8 | Vue.config.productionTip = false |
| 9 | 9 | |
| 10 | 10 | router.beforeEach((to, from, next) =>{ |
| 11 | - console.log(to) | |
| 12 | - document.title = to.meta.title | |
| 13 | - next() | |
| 11 | + | |
| 12 | + if(to.matched.some(record => record.meta.requiresAuth)){ | |
| 13 | + if(Cookies.get('username')){ | |
| 14 | + document.title = to.meta.title | |
| 15 | + next() | |
| 16 | + }else{ | |
| 17 | + next({ | |
| 18 | + path:'/login', | |
| 19 | + query: { redirect: to.fullPath } | |
| 20 | + }) | |
| 21 | + document.title = '登录' | |
| 22 | + } | |
| 23 | + | |
| 24 | + | |
| 25 | + }else { | |
| 26 | + document.title = to.meta.title | |
| 27 | + next() | |
| 28 | + } | |
| 29 | + | |
| 30 | + // console.log(to) | |
| 31 | + // | |
| 32 | + // next() | |
| 14 | 33 | }) |
| 15 | 34 | |
| 16 | 35 | /* eslint-disable no-new */ | ... | ... |
webintroduce/src/router/index.js
| ... | ... | @@ -19,7 +19,8 @@ export default new Router({ |
| 19 | 19 | name: 'home', |
| 20 | 20 | component: home, |
| 21 | 21 | meta: { |
| 22 | - title: '首页' | |
| 22 | + title: '首页', | |
| 23 | + requiresAuth: false | |
| 23 | 24 | } |
| 24 | 25 | }, |
| 25 | 26 | { |
| ... | ... | @@ -27,23 +28,37 @@ export default new Router({ |
| 27 | 28 | name: 'solution', |
| 28 | 29 | component: solution, |
| 29 | 30 | meta: { |
| 30 | - title: '解决方案' | |
| 31 | + title: '解决方案', | |
| 32 | + requiresAuth: false | |
| 31 | 33 | } |
| 32 | 34 | }, |
| 33 | 35 | { |
| 34 | - path: '/enterprise/:id', | |
| 36 | + path: '/enterprise', | |
| 35 | 37 | name: 'enterprise', |
| 36 | 38 | component: enterprise, |
| 37 | 39 | meta: { |
| 38 | - title: '企业' | |
| 39 | - } | |
| 40 | + title: '企业', | |
| 41 | + requiresAuth: true | |
| 42 | + }, | |
| 43 | + child:[ | |
| 44 | + { | |
| 45 | + path: '/enterprise/:id', | |
| 46 | + name: 'enterprise', | |
| 47 | + component: enterprise, | |
| 48 | + meta: { | |
| 49 | + title: '企业', | |
| 50 | + requiresAuth: true | |
| 51 | + }, | |
| 52 | + } | |
| 53 | + ] | |
| 40 | 54 | }, |
| 41 | 55 | { |
| 42 | 56 | path: '/login', |
| 43 | 57 | name: 'login', |
| 44 | 58 | component: login, |
| 45 | 59 | meta: { |
| 46 | - title: '登录' | |
| 60 | + title: '登录', | |
| 61 | + requiresAuth: false | |
| 47 | 62 | } |
| 48 | 63 | } |
| 49 | 64 | ] | ... | ... |
webintroduce/src/views/login.vue
| 1 | 1 | <template> |
| 2 | - <div>登录页面没有公共的头部底部</div> | |
| 2 | + <div>登录页面没有公共的头部底部 | |
| 3 | + {{username}} | |
| 4 | + <input type="text" v-model="username"> | |
| 5 | + <input type="button" name="" value="登录" @click="loginBtn"> | |
| 6 | + </div> | |
| 3 | 7 | </template> |
| 4 | 8 | |
| 5 | 9 | <script> |
| 10 | +import Cookies from 'js-cookie' | |
| 6 | 11 | export default { |
| 7 | - name: 'login' | |
| 12 | + name: 'login', | |
| 13 | + data() { | |
| 14 | + return { | |
| 15 | + username: '' | |
| 16 | + } | |
| 17 | + }, | |
| 18 | + methods: { | |
| 19 | + loginBtn() { | |
| 20 | + Cookies.set('username',this.username) | |
| 21 | + console.log(this.$route) | |
| 22 | + let redirect = decodeURIComponent(this.$route.query.redirect || '/'); | |
| 23 | + this.$router.push({//你需要接受路由的参数再跳转 | |
| 24 | + path: redirect | |
| 25 | + }); | |
| 26 | + } | |
| 27 | + } | |
| 8 | 28 | } |
| 9 | 29 | </script> |
| 10 | 30 | ... | ... |