Skip to content

Commit 3931436

Browse files
committed
feat: 退出逻辑优化,并新增个人中心跳转功能
1 parent 4c45d12 commit 3931436

File tree

6 files changed

+20
-14
lines changed

6 files changed

+20
-14
lines changed

src/layouts/header/index.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
:src="userInfo.avatar"
1515
alt="">
1616
<el-dropdown-menu slot="dropdown">
17-
<el-dropdown-item command="personal">个人中心</el-dropdown-item>
17+
<el-dropdown-item command="personal">
18+
<router-link tag="span" to="/userCenter">个人中心</router-link>
19+
</el-dropdown-item>
1820
<el-dropdown-item command="logout">退出登录</el-dropdown-item>
1921
</el-dropdown-menu>
2022
</el-dropdown>
@@ -37,7 +39,6 @@
3739
...mapMutations(['CHANGE_NAV']),
3840
onCommand (command) {
3941
command = command[0].toUpperCase() + command.slice(1);
40-
console.log('command', command);
4142
this[`on${command}`]();
4243
},
4344
onLogout () {
@@ -46,9 +47,6 @@
4647
err => console.log(err)
4748
);
4849
},
49-
onPersonal () {
50-
console.log('个人中心');
51-
}
5250
}
5351
};
5452
</script>

src/router/permission.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ router.beforeEach((to, from, next) => {
3535
if (userInfo) {
3636
const { hasGetUserInfo } = store.state.user;
3737
if (!hasGetUserInfo) {
38-
store.commit('user/CHANGE_USER_INFO', userInfo);
38+
store.commit('user/SET_USER_INFO', userInfo);
3939
}
4040
const { hasGetRouter } = store.state.router;
4141
if (hasGetRouter) {
@@ -54,10 +54,10 @@ router.beforeEach((to, from, next) => {
5454
} else if (to.path === '/login') { // 如果是登录页面
5555
const userInfo = JSON.parse(localStorage.getItem('userInfo'));
5656
if (!userInfo) return next();
57-
store.commit('user/CHANGE_USER_INFO', userInfo);
57+
store.commit('user/SET_USER_INFO', userInfo);
5858
vm.$message.success('用户已登录');
59-
NProgress.done();
6059
next('/main');
60+
NProgress.done();
6161
} else {
6262
next();
6363
}

src/store/modules/router.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const router = {
3333
state.menus = menus;
3434
state.authInfo = authInfo;
3535
state.hasGetRouter = hasGetRouter;
36+
},
37+
SET_ROUTER_STATUS (state, status) {
38+
state.hasGetRouter = status;
3639
}
3740
},
3841
actions: {

src/store/modules/user.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ const user = {
99
},
1010
//同步更改state中的状态
1111
mutations: {
12-
CHANGE_USER_INFO (state, userInfo) {
12+
SET_USER_INFO (state, userInfo) {
1313
localStorage.setItem('userInfo', JSON.stringify(userInfo));
1414
state.userInfo = userInfo;
1515
state.hasGetUserInfo = true;
16+
},
17+
SET_LOGIN_STATUS (state, status) {
18+
state.hasGetUserInfo = status;
1619
}
1720
},
1821
//异步更改state中的状态
1922
actions: {
20-
SET_USER_INFO ({ commit }, { params }) {
23+
GET_USER_INFO ({ commit }, { params }) {
2124
return fetchLogin(params).then(
2225
res => {
23-
commit('CHANGE_USER_INFO', res.data);
26+
commit('SET_USER_INFO', res.data);
2427
vm.$router.push('/main');
2528
return Promise.resolve(res.data);
2629
},
@@ -30,7 +33,7 @@ const user = {
3033
AUTH_TOKEN ({ commit }) {
3134
return fetchAuthToken().then(
3235
res => {
33-
commit('CHANGE_USER_INFO', res.data);
36+
commit('SET_USER_INFO', res.data);
3437
return Promise.resolve();
3538
},
3639
err => Promise.reject(err)

src/utils/user.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export const getToken = () => {
1414
export const goLogin = (message) => {
1515
message && vm.$message.warning(message);
1616
localStorage.clear();
17+
store.commit('router/SET_ROUTER_STATUS', false);
18+
store.commit('user/SET_LOGIN_STATUS', false);
1719
vm.$router.push('/login');
1820
};
1921

src/views/login/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@
5959
};
6060
},
6161
methods: {
62-
...mapActions('user', ['SET_USER_INFO']),
62+
...mapActions('user', ['GET_USER_INFO']),
6363
...mapActions('router', ['GET_MENUS']),
6464
onSubmit () {
6565
this.$refs.formItem.validate((valid) => {
6666
if (valid) {
6767
this.loginLoading = true;
68-
this.SET_USER_INFO({ params: this.formItem })
68+
this.GET_USER_INFO({ params: this.formItem })
6969
.then(
7070
() => {
7171
this.loginLoading = false;

0 commit comments

Comments
 (0)