vuex add namespaced

This commit is contained in:
Pan 2019-03-20 13:44:42 +08:00
parent 7c33568883
commit 0c50029cf7
20 changed files with 414 additions and 382 deletions

View File

@ -27,7 +27,7 @@ export default {
methods: { methods: {
handleSetLanguage(lang) { handleSetLanguage(lang) {
this.$i18n.locale = lang this.$i18n.locale = lang
this.$store.dispatch('setLanguage', lang) this.$store.dispatch('app/setLanguage', lang)
this.$message({ this.$message({
message: 'Switch Language Success', message: 'Switch Language Success',
type: 'success' type: 'success'

View File

@ -32,7 +32,7 @@ export default {
methods: { methods: {
handleSetSize(size) { handleSetSize(size) {
this.$ELEMENT.size = size this.$ELEMENT.size = size
this.$store.dispatch('setSize', size) this.$store.dispatch('app/setSize', size)
this.refreshView() this.refreshView()
this.$message({ this.$message({
message: 'Switch Size Success', message: 'Switch Size Success',
@ -41,7 +41,7 @@ export default {
}, },
refreshView() { refreshView() {
// In order to make the cached page re-rendered // In order to make the cached page re-rendered
this.$store.dispatch('delAllCachedViews', this.$route) this.$store.dispatch('tagsView/delAllCachedViews', this.$route)
const { fullPath } = this.$route const { fullPath } = this.$route

View File

@ -48,7 +48,7 @@ export default {
}, },
methods: { methods: {
handleClickOutside() { handleClickOutside() {
this.$store.dispatch('closeSideBar', { withoutAnimation: false }) this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
} }
} }
} }

View File

@ -75,10 +75,10 @@ export default {
}, },
methods: { methods: {
toggleSideBar() { toggleSideBar() {
this.$store.dispatch('toggleSideBar') this.$store.dispatch('app/toggleSideBar')
}, },
async logout() { async logout() {
await this.$store.dispatch('Logout') await this.$store.dispatch('user/logout')
// In order to re-instantiate the vue-router object to avoid bugs // In order to re-instantiate the vue-router object to avoid bugs
location.reload() location.reload()
} }

View File

@ -34,7 +34,7 @@ export default {
return this.$store.state.settings.tagsView return this.$store.state.settings.tagsView
}, },
set(val) { set(val) {
this.$store.dispatch('changeSetting', { this.$store.dispatch('settings/changeSetting', {
key: 'tagsView', key: 'tagsView',
value: val value: val
}) })

View File

@ -106,14 +106,14 @@ export default {
for (const tag of affixTags) { for (const tag of affixTags) {
// Must have tag name // Must have tag name
if (tag.name) { if (tag.name) {
this.$store.dispatch('addVisitedView', tag) this.$store.dispatch('tagsView/addVisitedView', tag)
} }
} }
}, },
addTags() { addTags() {
const { name } = this.$route const { name } = this.$route
if (name) { if (name) {
this.$store.dispatch('addView', this.$route) this.$store.dispatch('tagsView/addView', this.$route)
} }
return false return false
}, },
@ -125,7 +125,7 @@ export default {
this.$refs.scrollPane.moveToTarget(tag) this.$refs.scrollPane.moveToTarget(tag)
// when query is different then update // when query is different then update
if (tag.to.fullPath !== this.$route.fullPath) { if (tag.to.fullPath !== this.$route.fullPath) {
this.$store.dispatch('updateVisitedView', this.$route) this.$store.dispatch('tagsView/updateVisitedView', this.$route)
} }
break break
} }
@ -133,7 +133,7 @@ export default {
}) })
}, },
refreshSelectedTag(view) { refreshSelectedTag(view) {
this.$store.dispatch('delCachedView', view).then(() => { this.$store.dispatch('tagsView/delCachedView', view).then(() => {
const { fullPath } = view const { fullPath } = view
this.$nextTick(() => { this.$nextTick(() => {
this.$router.replace({ this.$router.replace({
@ -143,7 +143,7 @@ export default {
}) })
}, },
closeSelectedTag(view) { closeSelectedTag(view) {
this.$store.dispatch('delView', view).then(({ visitedViews }) => { this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
if (this.isActive(view)) { if (this.isActive(view)) {
this.toLastView(visitedViews) this.toLastView(visitedViews)
} }
@ -151,12 +151,12 @@ export default {
}, },
closeOthersTags() { closeOthersTags() {
this.$router.push(this.selectedTag) this.$router.push(this.selectedTag)
this.$store.dispatch('delOthersViews', this.selectedTag).then(() => { this.$store.dispatch('tagsView/delOthersViews', this.selectedTag).then(() => {
this.moveToCurrentTag() this.moveToCurrentTag()
}) })
}, },
closeAllTags(view) { closeAllTags(view) {
this.$store.dispatch('delAllViews').then(({ visitedViews }) => { this.$store.dispatch('tagsView/delAllViews').then(({ visitedViews }) => {
if (this.affixTags.some(tag => tag.path === view.path)) { if (this.affixTags.some(tag => tag.path === view.path)) {
return return
} }

View File

@ -7,7 +7,7 @@ export default {
watch: { watch: {
$route(route) { $route(route) {
if (this.device === 'mobile' && this.sidebar.opened) { if (this.device === 'mobile' && this.sidebar.opened) {
store.dispatch('closeSideBar', { withoutAnimation: false }) store.dispatch('app/closeSideBar', { withoutAnimation: false })
} }
} }
}, },
@ -17,8 +17,8 @@ export default {
mounted() { mounted() {
const isMobile = this.isMobile() const isMobile = this.isMobile()
if (isMobile) { if (isMobile) {
store.dispatch('toggleDevice', 'mobile') store.dispatch('app/toggleDevice', 'mobile')
store.dispatch('closeSideBar', { withoutAnimation: true }) store.dispatch('app/closeSideBar', { withoutAnimation: true })
} }
}, },
methods: { methods: {
@ -29,10 +29,10 @@ export default {
resizeHandler() { resizeHandler() {
if (!document.hidden) { if (!document.hidden) {
const isMobile = this.isMobile() const isMobile = this.isMobile()
store.dispatch('toggleDevice', isMobile ? 'mobile' : 'desktop') store.dispatch('app/toggleDevice', isMobile ? 'mobile' : 'desktop')
if (isMobile) { if (isMobile) {
store.dispatch('closeSideBar', { withoutAnimation: true }) store.dispatch('app/closeSideBar', { withoutAnimation: true })
} }
} }
} }

View File

@ -29,18 +29,18 @@ router.beforeEach((to, from, next) => {
if (store.getters.roles.length === 0) { if (store.getters.roles.length === 0) {
// 判断当前用户是否已拉取完user_info信息 // 判断当前用户是否已拉取完user_info信息
store store
.dispatch('getInfo') // dispatch @/store/modules/user login getInfo .dispatch('user/getInfo')
.then(res => { .then(res => {
// 拉取user_info // 拉取user_info
const { roles } = res // note: roles must be a object array! such as: [{id: '1', name: 'editor'}, {id: '2', name: 'developer'}] const { roles } = res // note: roles must be a object array! such as: [{id: '1', name: 'editor'}, {id: '2', name: 'developer'}]
store.dispatch('generateRoutes', { roles }).then(accessRoutes => { store.dispatch('permission/generateRoutes', { roles }).then(accessRoutes => {
// 根据roles权限生成可访问的路由表 // 根据roles权限生成可访问的路由表
router.addRoutes(accessRoutes) // 动态添加可访问路由表 router.addRoutes(accessRoutes) // 动态添加可访问路由表
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
}) })
}) })
.catch(err => { .catch(err => {
store.dispatch('resetToken').then(() => { store.dispatch('user/resetToken').then(() => {
Message.error(err) Message.error(err)
next({ path: '/' }) next({ path: '/' })
}) })

View File

@ -1,59 +1,64 @@
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
const app = { const state = {
state: { sidebar: {
sidebar: { opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true, withoutAnimation: false
withoutAnimation: false
},
device: 'desktop',
language: Cookies.get('language') || 'en',
size: Cookies.get('size') || 'medium'
}, },
mutations: { device: 'desktop',
TOGGLE_SIDEBAR: state => { language: Cookies.get('language') || 'en',
state.sidebar.opened = !state.sidebar.opened size: Cookies.get('size') || 'medium'
state.sidebar.withoutAnimation = false }
if (state.sidebar.opened) {
Cookies.set('sidebarStatus', 1) const mutations = {
} else { TOGGLE_SIDEBAR: state => {
Cookies.set('sidebarStatus', 0) state.sidebar.opened = !state.sidebar.opened
} state.sidebar.withoutAnimation = false
}, if (state.sidebar.opened) {
CLOSE_SIDEBAR: (state, withoutAnimation) => { Cookies.set('sidebarStatus', 1)
} else {
Cookies.set('sidebarStatus', 0) Cookies.set('sidebarStatus', 0)
state.sidebar.opened = false
state.sidebar.withoutAnimation = withoutAnimation
},
TOGGLE_DEVICE: (state, device) => {
state.device = device
},
SET_LANGUAGE: (state, language) => {
state.language = language
Cookies.set('language', language)
},
SET_SIZE: (state, size) => {
state.size = size
Cookies.set('size', size)
} }
}, },
actions: { CLOSE_SIDEBAR: (state, withoutAnimation) => {
toggleSideBar({ commit }) { Cookies.set('sidebarStatus', 0)
commit('TOGGLE_SIDEBAR') state.sidebar.opened = false
}, state.sidebar.withoutAnimation = withoutAnimation
closeSideBar({ commit }, { withoutAnimation }) { },
commit('CLOSE_SIDEBAR', withoutAnimation) TOGGLE_DEVICE: (state, device) => {
}, state.device = device
toggleDevice({ commit }, device) { },
commit('TOGGLE_DEVICE', device) SET_LANGUAGE: (state, language) => {
}, state.language = language
setLanguage({ commit }, language) { Cookies.set('language', language)
commit('SET_LANGUAGE', language) },
}, SET_SIZE: (state, size) => {
setSize({ commit }, size) { state.size = size
commit('SET_SIZE', size) Cookies.set('size', size)
}
} }
} }
export default app const actions = {
toggleSideBar({ commit }) {
commit('TOGGLE_SIDEBAR')
},
closeSideBar({ commit }, { withoutAnimation }) {
commit('CLOSE_SIDEBAR', withoutAnimation)
},
toggleDevice({ commit }, device) {
commit('TOGGLE_DEVICE', device)
},
setLanguage({ commit }, language) {
commit('SET_LANGUAGE', language)
},
setSize({ commit }, size) {
commit('SET_SIZE', size)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}

View File

@ -1,17 +1,23 @@
const errorLog = {
state: { const state = {
logs: [] logs: []
}, }
mutations: {
ADD_ERROR_LOG: (state, log) => { const mutations = {
state.logs.push(log) ADD_ERROR_LOG: (state, log) => {
} state.logs.push(log)
},
actions: {
addErrorLog({ commit }, log) {
commit('ADD_ERROR_LOG', log)
}
} }
} }
export default errorLog const actions = {
addErrorLog({ commit }, log) {
commit('ADD_ERROR_LOG', log)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}

View File

@ -34,32 +34,37 @@ export function filterAsyncRoutes(routes, roles) {
return res return res
} }
const permission = { const state = {
state: { routes: [],
routes: [], addRoutes: []
addRoutes: [] }
},
mutations: { const mutations = {
SET_ROUTES: (state, routes) => { SET_ROUTES: (state, routes) => {
state.addRoutes = routes state.addRoutes = routes
state.routes = constantRoutes.concat(routes) state.routes = constantRoutes.concat(routes)
}
},
actions: {
generateRoutes({ commit }, data) {
return new Promise(resolve => {
const { roles } = data
let accessedRoutes
if (roles.includes('admin')) {
accessedRoutes = asyncRoutes
} else {
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
}
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
})
}
} }
} }
export default permission const actions = {
generateRoutes({ commit }, data) {
return new Promise(resolve => {
const { roles } = data
let accessedRoutes
if (roles.includes('admin')) {
accessedRoutes = asyncRoutes
} else {
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
}
commit('SET_ROUTES', accessedRoutes)
resolve(accessedRoutes)
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}

View File

@ -1,23 +1,29 @@
import defaultSettings from '@/settings' import defaultSettings from '@/settings'
const { showSettings, tagsView } = defaultSettings const { showSettings, tagsView } = defaultSettings
const settings = { const state = {
state: { showSettings: showSettings,
showSettings: showSettings, tagsView: tagsView
tagsView: tagsView }
},
mutations: { const mutations = {
CHANGE_SETTING: (state, { key, value }) => { CHANGE_SETTING: (state, { key, value }) => {
if (state.hasOwnProperty(key)) { if (state.hasOwnProperty(key)) {
state[key] = value state[key] = value
}
}
},
actions: {
changeSetting({ commit }, data) {
commit('CHANGE_SETTING', data)
} }
} }
} }
export default settings const actions = {
changeSetting({ commit }, data) {
commit('CHANGE_SETTING', data)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}

View File

@ -1,161 +1,166 @@
const tagsView = {
state: {
visitedViews: [],
cachedViews: []
},
mutations: {
ADD_VISITED_VIEW: (state, view) => {
if (state.visitedViews.some(v => v.path === view.path)) return
state.visitedViews.push(
Object.assign({}, view, {
title: view.meta.title || 'no-name'
})
)
},
ADD_CACHED_VIEW: (state, view) => {
if (state.cachedViews.includes(view.name)) return
if (!view.meta.noCache) {
state.cachedViews.push(view.name)
}
},
DEL_VISITED_VIEW: (state, view) => { const state = {
for (const [i, v] of state.visitedViews.entries()) { visitedViews: [],
if (v.path === view.path) { cachedViews: []
state.visitedViews.splice(i, 1) }
break
}
}
},
DEL_CACHED_VIEW: (state, view) => {
for (const i of state.cachedViews) {
if (i === view.name) {
const index = state.cachedViews.indexOf(i)
state.cachedViews.splice(index, 1)
break
}
}
},
DEL_OTHERS_VISITED_VIEWS: (state, view) => { const mutations = {
state.visitedViews = state.visitedViews.filter(v => { ADD_VISITED_VIEW: (state, view) => {
return v.meta.affix || v.path === view.path if (state.visitedViews.some(v => v.path === view.path)) return
state.visitedViews.push(
Object.assign({}, view, {
title: view.meta.title || 'no-name'
}) })
}, )
DEL_OTHERS_CACHED_VIEWS: (state, view) => { },
for (const i of state.cachedViews) { ADD_CACHED_VIEW: (state, view) => {
if (i === view.name) { if (state.cachedViews.includes(view.name)) return
const index = state.cachedViews.indexOf(i) if (!view.meta.noCache) {
state.cachedViews = state.cachedViews.slice(index, index + 1) state.cachedViews.push(view.name)
break }
} },
}
},
DEL_ALL_VISITED_VIEWS: state => { DEL_VISITED_VIEW: (state, view) => {
// keep affix tags for (const [i, v] of state.visitedViews.entries()) {
const affixTags = state.visitedViews.filter(tag => tag.meta.affix) if (v.path === view.path) {
state.visitedViews = affixTags state.visitedViews.splice(i, 1)
}, break
DEL_ALL_CACHED_VIEWS: state => { }
state.cachedViews = [] }
}, },
DEL_CACHED_VIEW: (state, view) => {
UPDATE_VISITED_VIEW: (state, view) => { for (const i of state.cachedViews) {
for (let v of state.visitedViews) { if (i === view.name) {
if (v.path === view.path) { const index = state.cachedViews.indexOf(i)
v = Object.assign(v, view) state.cachedViews.splice(index, 1)
break break
}
} }
} }
}, },
actions: {
addView({ dispatch }, view) {
dispatch('addVisitedView', view)
dispatch('addCachedView', view)
},
addVisitedView({ commit }, view) {
commit('ADD_VISITED_VIEW', view)
},
addCachedView({ commit }, view) {
commit('ADD_CACHED_VIEW', view)
},
delView({ dispatch, state }, view) { DEL_OTHERS_VISITED_VIEWS: (state, view) => {
return new Promise(resolve => { state.visitedViews = state.visitedViews.filter(v => {
dispatch('delVisitedView', view) return v.meta.affix || v.path === view.path
dispatch('delCachedView', view) })
resolve({ },
visitedViews: [...state.visitedViews], DEL_OTHERS_CACHED_VIEWS: (state, view) => {
cachedViews: [...state.cachedViews] for (const i of state.cachedViews) {
}) if (i === view.name) {
}) const index = state.cachedViews.indexOf(i)
}, state.cachedViews = state.cachedViews.slice(index, index + 1)
delVisitedView({ commit, state }, view) { break
return new Promise(resolve => { }
commit('DEL_VISITED_VIEW', view) }
resolve([...state.visitedViews]) },
})
},
delCachedView({ commit, state }, view) {
return new Promise(resolve => {
commit('DEL_CACHED_VIEW', view)
resolve([...state.cachedViews])
})
},
delOthersViews({ dispatch, state }, view) { DEL_ALL_VISITED_VIEWS: state => {
return new Promise(resolve => { // keep affix tags
dispatch('delOthersVisitedViews', view) const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
dispatch('delOthersCachedViews', view) state.visitedViews = affixTags
resolve({ },
visitedViews: [...state.visitedViews], DEL_ALL_CACHED_VIEWS: state => {
cachedViews: [...state.cachedViews] state.cachedViews = []
}) },
})
},
delOthersVisitedViews({ commit, state }, view) {
return new Promise(resolve => {
commit('DEL_OTHERS_VISITED_VIEWS', view)
resolve([...state.visitedViews])
})
},
delOthersCachedViews({ commit, state }, view) {
return new Promise(resolve => {
commit('DEL_OTHERS_CACHED_VIEWS', view)
resolve([...state.cachedViews])
})
},
delAllViews({ dispatch, state }, view) { UPDATE_VISITED_VIEW: (state, view) => {
return new Promise(resolve => { for (let v of state.visitedViews) {
dispatch('delAllVisitedViews', view) if (v.path === view.path) {
dispatch('delAllCachedViews', view) v = Object.assign(v, view)
resolve({ break
visitedViews: [...state.visitedViews], }
cachedViews: [...state.cachedViews]
})
})
},
delAllVisitedViews({ commit, state }) {
return new Promise(resolve => {
commit('DEL_ALL_VISITED_VIEWS')
resolve([...state.visitedViews])
})
},
delAllCachedViews({ commit, state }) {
return new Promise(resolve => {
commit('DEL_ALL_CACHED_VIEWS')
resolve([...state.cachedViews])
})
},
updateVisitedView({ commit }, view) {
commit('UPDATE_VISITED_VIEW', view)
} }
} }
} }
export default tagsView const actions = {
addView({ dispatch }, view) {
dispatch('addVisitedView', view)
dispatch('addCachedView', view)
},
addVisitedView({ commit }, view) {
commit('ADD_VISITED_VIEW', view)
},
addCachedView({ commit }, view) {
commit('ADD_CACHED_VIEW', view)
},
delView({ dispatch, state }, view) {
return new Promise(resolve => {
dispatch('delVisitedView', view)
dispatch('delCachedView', view)
resolve({
visitedViews: [...state.visitedViews],
cachedViews: [...state.cachedViews]
})
})
},
delVisitedView({ commit, state }, view) {
return new Promise(resolve => {
commit('DEL_VISITED_VIEW', view)
resolve([...state.visitedViews])
})
},
delCachedView({ commit, state }, view) {
return new Promise(resolve => {
commit('DEL_CACHED_VIEW', view)
resolve([...state.cachedViews])
})
},
delOthersViews({ dispatch, state }, view) {
return new Promise(resolve => {
dispatch('delOthersVisitedViews', view)
dispatch('delOthersCachedViews', view)
resolve({
visitedViews: [...state.visitedViews],
cachedViews: [...state.cachedViews]
})
})
},
delOthersVisitedViews({ commit, state }, view) {
return new Promise(resolve => {
commit('DEL_OTHERS_VISITED_VIEWS', view)
resolve([...state.visitedViews])
})
},
delOthersCachedViews({ commit, state }, view) {
return new Promise(resolve => {
commit('DEL_OTHERS_CACHED_VIEWS', view)
resolve([...state.cachedViews])
})
},
delAllViews({ dispatch, state }, view) {
return new Promise(resolve => {
dispatch('delAllVisitedViews', view)
dispatch('delAllCachedViews', view)
resolve({
visitedViews: [...state.visitedViews],
cachedViews: [...state.cachedViews]
})
})
},
delAllVisitedViews({ commit, state }) {
return new Promise(resolve => {
commit('DEL_ALL_VISITED_VIEWS')
resolve([...state.visitedViews])
})
},
delAllCachedViews({ commit, state }) {
return new Promise(resolve => {
commit('DEL_ALL_CACHED_VIEWS')
resolve([...state.cachedViews])
})
},
updateVisitedView({ commit }, view) {
commit('UPDATE_VISITED_VIEW', view)
}
}
export default {
namespaced: true,
state,
mutations,
actions
}

View File

@ -1,118 +1,123 @@
import { login, logout, getInfo } from '@/api/user' import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth' import { getToken, setToken, removeToken } from '@/utils/auth'
const user = { const state = {
state: { token: getToken(),
token: getToken(), name: '',
name: '', avatar: '',
avatar: '', introduction: '',
introduction: '', roles: []
roles: [] }
const mutations = {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_INTRODUCTION: (state, introduction) => {
state.introduction = introduction
},
SET_NAME: (state, name) => {
state.name = name
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
},
SET_ROLES: (state, roles) => {
state.roles = roles
}
}
const actions = {
// user login
login({ commit }, userInfo) {
const { username, password } = userInfo
return new Promise((resolve, reject) => {
login({ username: username.trim(), password: password }).then(response => {
const { data } = response
commit('SET_TOKEN', data.token)
setToken(data.token)
resolve()
}).catch(error => {
reject(error)
})
})
}, },
mutations: { // get user info
SET_TOKEN: (state, token) => { getInfo({ commit, state }) {
state.token = token return new Promise((resolve, reject) => {
}, getInfo(state.token).then(response => {
SET_INTRODUCTION: (state, introduction) => { const { data } = response
state.introduction = introduction
}, if (!data) {
SET_NAME: (state, name) => { reject('Verification failed, please Login again.')
state.name = name }
},
SET_AVATAR: (state, avatar) => { const { roles, name, avatar, introduction } = data
state.avatar = avatar
}, // roles must be a non-empty array
SET_ROLES: (state, roles) => { if (!roles || roles.length <= 0) {
state.roles = roles reject('getInfo: roles must be a non-null array!')
} }
commit('SET_ROLES', roles)
commit('SET_NAME', name)
commit('SET_AVATAR', avatar)
commit('SET_INTRODUCTION', introduction)
resolve(data)
}).catch(error => {
reject(error)
})
})
}, },
actions: { // user logout
// user login logout({ commit, state }) {
login({ commit }, userInfo) { return new Promise((resolve, reject) => {
const { username, password } = userInfo logout(state.token).then(() => {
return new Promise((resolve, reject) => {
login({ username: username.trim(), password: password }).then(response => {
const { data } = response
commit('SET_TOKEN', data.token)
setToken(data.token)
resolve()
}).catch(error => {
reject(error)
})
})
},
// get user info
getInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo(state.token).then(response => {
const { data } = response
if (!data) {
reject('Verification failed, please Login again.')
}
const { roles, name, avatar, introduction } = data
// roles must be a non-empty array
if (!roles || roles.length <= 0) {
reject('getInfo: roles must be a non-null array!')
}
commit('SET_ROLES', roles)
commit('SET_NAME', name)
commit('SET_AVATAR', avatar)
commit('SET_INTRODUCTION', introduction)
resolve(data)
}).catch(error => {
reject(error)
})
})
},
// user logout
Logout({ commit, state }) {
return new Promise((resolve, reject) => {
logout(state.token).then(() => {
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
removeToken()
resolve()
}).catch(error => {
reject(error)
})
})
},
// remove token
resetToken({ commit }) {
return new Promise(resolve => {
commit('SET_TOKEN', '') commit('SET_TOKEN', '')
commit('SET_ROLES', []) commit('SET_ROLES', [])
removeToken() removeToken()
resolve() resolve()
}).catch(error => {
reject(error)
}) })
}, })
},
// 动态修改权限 // remove token
changeRoles({ commit, dispatch }, role) { resetToken({ commit }) {
return new Promise(resolve => { return new Promise(resolve => {
commit('SET_TOKEN', role) commit('SET_TOKEN', '')
setToken(role) commit('SET_ROLES', [])
getInfo(role).then(response => { removeToken()
const data = response.data resolve()
commit('SET_ROLES', data.roles) })
commit('SET_NAME', data.name) },
commit('SET_AVATAR', data.avatar)
commit('SET_INTRODUCTION', data.introduction) // 动态修改权限
dispatch('generateRoutes', data) // 动态修改权限后 重绘侧边菜单 changeRoles({ commit, dispatch }, role) {
resolve() return new Promise(resolve => {
}) const token = role + '-token'
commit('SET_TOKEN', token)
setToken(token)
getInfo(token).then(response => {
const { data } = response
const { roles, name, avatar, introduction } = data
commit('SET_ROLES', roles)
commit('SET_NAME', name)
commit('SET_AVATAR', avatar)
commit('SET_INTRODUCTION', introduction)
dispatch('permission/generateRoutes', data) // 动态修改权限后 重绘侧边菜单
resolve()
}) })
} })
} }
} }
export default user export default {
namespaced: true,
state,
mutations,
actions
}

View File

@ -23,7 +23,7 @@ if (checkNeed()) {
// Don't ask me why I use Vue.nextTick, it just a hack. // Don't ask me why I use Vue.nextTick, it just a hack.
// detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500 // detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500
Vue.nextTick(() => { Vue.nextTick(() => {
store.dispatch('addErrorLog', { store.dispatch('errorLog/addErrorLog', {
err, err,
vm, vm,
info, info,

View File

@ -57,7 +57,7 @@ service.interceptors.response.use(
// cancelButtonText: '取消', // cancelButtonText: '取消',
// type: 'warning' // type: 'warning'
// }).then(() => { // }).then(() => {
// store.dispatch('resetToken').then(() => { // store.dispatch('user/resetToken').then(() => {
// location.reload() // 为了重新实例化vue-router对象 避免bug // location.reload() // 为了重新实例化vue-router对象 避免bug
// }) // })
// }) // })

View File

@ -186,7 +186,7 @@ export default {
setTagsViewTitle() { setTagsViewTitle() {
const title = this.lang === 'zh' ? '编辑文章' : 'Edit Article' const title = this.lang === 'zh' ? '编辑文章' : 'Edit Article'
const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.postForm.id}` }) const route = Object.assign({}, this.tempRoute, { title: `${title}-${this.postForm.id}` })
this.$store.dispatch('updateVisitedView', route) this.$store.dispatch('tagsView/updateVisitedView', route)
}, },
submitForm() { submitForm() {
this.postForm.display_time = parseInt(this.display_time / 1000) this.postForm.display_time = parseInt(this.display_time / 1000)

View File

@ -110,7 +110,7 @@ export default {
}, },
set(lang) { set(lang) {
this.$i18n.locale = lang this.$i18n.locale = lang
this.$store.dispatch('setLanguage', lang) this.$store.dispatch('app/setLanguage', lang)
} }
} }
}, },

View File

@ -136,7 +136,7 @@ export default {
if (valid) { if (valid) {
this.loading = true this.loading = true
// dispatch @/store/modules/user login action // dispatch @/store/modules/user login action
this.$store.dispatch('login', this.loginForm).then(() => { this.$store.dispatch('user/login', this.loginForm).then(() => {
this.loading = false this.loading = false
this.$router.push({ path: this.redirect || '/' }) this.$router.push({ path: this.redirect || '/' })
}).catch(() => { }).catch(() => {

View File

@ -22,7 +22,7 @@ export default {
return this.roles[0] return this.roles[0]
}, },
set(val) { set(val) {
this.$store.dispatch('changeRoles', val).then(() => { this.$store.dispatch('user/changeRoles', val).then(() => {
this.$emit('change') this.$emit('change')
}) })
} }