Skip to content

Commit 9da6cae

Browse files
committed
Merge branch 'dev'
2 parents ec6ed1a + f2cdbde commit 9da6cae

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

src/App.vue

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@
1212
name: 'app',
1313
components: {
1414
},
15-
created: function () {
16-
this.$router.replace('/login')
17-
}
15+
// beforeCreate: function () {
16+
// let user = JSON.parse(localStorage.getItem('user'));
17+
// if (!user) {
18+
// this.$router.replace('/login')
19+
// }
20+
// }
21+
watch: {
22+
'$route'(to, from) {//监听路由改变
23+
let user = JSON.parse(sessionStorage.getItem('user'));
24+
if (!user) {
25+
this.$router.replace('/login')
26+
}
27+
}
28+
},
1829
}
1930
</script>
2031

src/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ const router = new VueRouter({
8686

8787
router.beforeEach((to, from, next) => {
8888
NProgress.start();
89+
let user = JSON.parse(sessionStorage.getItem('user'));
90+
if (!user && to.path != '/login') {
91+
next({ path: '/login' })
92+
}
8993
next()
9094
})
9195

src/mockdata/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const LoginUsers = [
44
id: 1,
55
username: 'admin',
66
password: '123456',
7-
avatar: '',
7+
avatar: 'https://raw.githubusercontent.com/taylorchen709/markdown-images/master/vueadmin/user.png',
88
name: '张某某'
99
}
1010
];

src/pages/Home.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</el-col>
88
<el-col :span="4" class="rightbar">
99
<el-dropdown trigger="click">
10-
<span class="el-dropdown-link" style="color:#c0ccda;cursor: pointer;"><img src="../assets/user.png" class="head"> 张某某
10+
<span class="el-dropdown-link" style="color:#c0ccda;cursor: pointer;"><img :src="this.sysUserAvatar" class="head"> {{sysUserName}}
1111
</span>
1212
<el-dropdown-menu slot="dropdown">
1313
<el-dropdown-item>我的消息</el-dropdown-item>
@@ -82,6 +82,8 @@
8282
currentPath: '/table',
8383
currentPathName: 'Table',
8484
currentPathNameParent: '导航一',
85+
sysUserName: '',
86+
sysUserAvatar: '',
8587
form: {
8688
name: '',
8789
region: '',
@@ -119,12 +121,25 @@
119121
this.$confirm('确认退出吗?', '提示', {
120122
//type: 'warning'
121123
}).then(() => {
124+
sessionStorage.removeItem('user');
122125
_this.$router.replace('/login');
123126
}).catch(() => {
124127
125128
});
126129
127130
131+
}
132+
},
133+
mounted() {
134+
this.currentPath = this.$route.path;
135+
this.currentPathName = this.$route.name;
136+
this.currentPathNameParent = this.$route.matched[0].name;
137+
138+
var user = sessionStorage.getItem('user');
139+
if (user) {
140+
user = JSON.parse(user);
141+
this.sysUserName = user.name || '';
142+
this.sysUserAvatar = user.avatar || '';
128143
}
129144
}
130145
}

src/pages/Login.vue

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@
6262
type: 'error'
6363
});
6464
} else {
65-
localStorage.setItem('user', JSON.stringify(user));
66-
if (this.$route.query.redirect) {
67-
this.$router.push({ path: this.$route.query.redirect });
68-
} else {
69-
this.$router.push({ path: '/table' });
70-
}
65+
sessionStorage.setItem('user', JSON.stringify(user));
66+
this.$router.push({ path: '/table' });
7167
}
7268
});
7369
} else {

0 commit comments

Comments
 (0)