Skip to content
This repository was archived by the owner on May 12, 2020. It is now read-only.

Commit 3fceb17

Browse files
committed
fix(login): correct login process
1 parent c5431c3 commit 3fceb17

9 files changed

Lines changed: 35 additions & 27 deletions

File tree

src/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Vue from 'vue'
22
import 'normalize.css'
33
import App from './App'
44

5-
// Including Vue prototype functions (eg. this.$messageBox())
5+
// Including Vue prototype functions (eg. this.$_plugins_messageBox())
66
import './plugins/element.js'
77

88
// Shouldn't use Router instance directly without 'PERMISSION/index' processing.

src/permission/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import store from 'STORE'
33
import loginTypes from 'STORE/modules/login/mutations/types'
44
import NProgress from 'nprogress'
55
import 'nprogress/nprogress.css'
6-
import { tokenFromStorage, usernameFromStorage } from 'UTILS/storage'
6+
import { tokenFromStorage, userInfoFromStorage } from 'UTILS/storage'
77
import { MessageBox } from 'element-ui'
88
import createDynamicRoutes from './create-routes'
99
import constantRoutes from 'ROUTER/routes/constant'
@@ -97,8 +97,8 @@ router.beforeEach((to, from, next) => {
9797
tokenFromStorage.getItem()
9898
)
9999
store.commit(
100-
`login/${loginTypes.SET_USERNAME}`,
101-
usernameFromStorage.getItem()
100+
`login/${loginTypes.SET_USER_INFO}`,
101+
JSON.parse(userInfoFromStorage.getItem()).username
102102
)
103103
})
104104
.then(() => createRoutesMap(to, next))

src/plugins/element.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import {
1111
FormItem,
1212
Input,
1313
MessageBox,
14+
Message,
1415
Menu,
1516
Submenu,
1617
MenuItem
1718
} from 'element-ui'
1819

1920
// Prototype functions
20-
Vue.prototype.$messageBox = MessageBox
21+
Vue.prototype.$_plugins_messageBox = MessageBox
22+
Vue.prototype.$_plugins_message = Message
2123

2224
// components using
2325
vueUsing([

src/store/modules/login/actions.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,27 @@ export default {
77
username,
88
password
99
})
10-
.then(res => {
11-
commit(types.SET_USERNAME, username)
12-
commit(types.SET_ACCESS_TOKEN, res.access_token)
13-
vm.toggleLoading(false)
14-
vm.$router.replace('/home')
10+
// eslint-disable-next-line
11+
.then(({ user_id, access_token }) => {
12+
commit(types.SET_USER_INFO, {
13+
username, /* 手机或者是邮箱 */
14+
userId: user_id
15+
})
16+
commit(types.SET_ACCESS_TOKEN, access_token)
1517
})
1618
.catch(e => {
1719
if (e.code === 5000) {
18-
vm.$messageBox({
19-
title: 'Error',
20-
message: 'Wrong username or password',
20+
vm.$_plugins_messageBox({
21+
title: '警告',
22+
message: '错误的用户名或密码',
2123
type: 'error'
2224
})
2325
}
24-
console.error(`[Login error]: ${e.code}, ${e.msg}`)
26+
console.error(`[Login error]: ${JSON.stringify(e)}`)
2527
})
2628
},
2729
userLogout ({ commit }) {
28-
commit(types.SET_USERNAME, '')
30+
commit(types.SET_USER_INFO, {})
2931
commit(types.SET_USER_ROLE, '')
3032
commit(types.SET_ACCESS_TOKEN, '')
3133
commit(types.SET_DYNAMIC_ROUTES, [])
@@ -36,6 +38,7 @@ export default {
3638
location.reload()
3739
},
3840
fetchUserAccess ({ commit }, token) {
41+
// ! 预留接口:请求用户的权限集合 roles,用于过滤用户的私有路由
3942
return fetchUserAccess(token)
4043
.then(({ roles }) => {
4144
commit(types.SET_USER_ROLE, roles)

src/store/modules/login/getters.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default {
22
username (state) {
3-
return state.username
3+
return (state.userInfo && state.userInfo.username) || '无用户名'
44
},
55
accessToken (state) {
66
return state.accessToken

src/store/modules/login/mutations/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import types from './types'
2-
import { tokenFromStorage, usernameFromStorage } from 'UTILS/storage'
2+
import { tokenFromStorage, userInfoFromStorage } from 'UTILS/storage'
33

44
export default {
5-
[types.SET_USERNAME] (state, username) {
6-
username
7-
? usernameFromStorage.setItem(username)
8-
: usernameFromStorage.removeItem()
9-
state.username = username
5+
[types.SET_USER_INFO] (state, userInfo) {
6+
userInfo
7+
? userInfoFromStorage.setItem(userInfo)
8+
: userInfoFromStorage.removeItem()
9+
state.userInfo = userInfo
1010
},
1111
[types.SET_ACCESS_TOKEN] (state, accessToken) {
1212
accessToken

src/store/modules/login/mutations/types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default {
2-
SET_USERNAME: 'SET_USERNAME',
2+
SET_USER_INFO: 'SET_USER_INFO',
33
SET_ACCESS_TOKEN: 'SET_ACCESS_TOKEN',
44
SET_USER_ROLE: 'SET_USER_ROLE',
55
SET_DYNAMIC_ROUTES: 'SET_DYNAMIC_ROUTES',

src/store/modules/login/state.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
export default {
2-
username: '',
2+
userInfo: {
3+
userId: '',
4+
username: ''
5+
},
36
accessToken: '',
47
role: '',
58
dynamicRoutes: [],

src/utils/storage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const TOKEN_KEY = '__tk__'
2-
const USERNAME_KEY = '__usn__'
1+
const TOKEN_KEY = '$_tk'
2+
const USER_INFO_KEY = '$_usn'
33

44
function setValueToLocal (key, val, storage) {
55
const normalizeVal = typeof val === 'string'
@@ -24,4 +24,4 @@ function createStorageUtils (key, storage = localStorage) {
2424

2525
export const tokenFromStorage = createStorageUtils(TOKEN_KEY, localStorage)
2626

27-
export const usernameFromStorage = createStorageUtils(USERNAME_KEY, localStorage)
27+
export const userInfoFromStorage = createStorageUtils(USER_INFO_KEY, localStorage)

0 commit comments

Comments
 (0)