nebula/store/index.js

74 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-07-03 11:04:25 +08:00
const cookieparser = process.server ? require('cookieparser') : undefined
2020-07-02 23:45:27 +08:00
export const state = () => {
return {
2020-07-03 11:04:25 +08:00
activeMenu: 'index',
2020-07-31 16:17:12 +08:00
oauth: null,
locale: 'zh_CN'
2020-07-02 23:45:27 +08:00
}
}
2020-07-03 11:04:25 +08:00
export const mutations = {
setAuth (state, auth) {
state.oauth = auth
},
setActiveMenu (state, activeMenu) {
state.activeMenu = activeMenu
}
}
2020-06-22 17:13:42 +08:00
export const actions = {
2020-06-21 21:56:34 +08:00
nuxtServerInit(store, {req}) {
// 初始化时的全局任务
2020-07-03 11:04:25 +08:00
let auth = null
if (req.headers.cookie) {
const parsed = cookieparser.parse(req.headers.cookie)
try {
auth = JSON.parse(parsed.auth)
} catch (err) {
// No valid cookie found
}
store.commit('setAuth', auth)
}
2020-06-21 21:56:34 +08:00
const initFetchAppData = [
// 内容数据
2020-07-31 17:40:17 +08:00
store.dispatch('topic/fetchList'),
2020-06-21 21:56:34 +08:00
store.dispatch('article/fetchList')
]
return Promise.all(initFetchAppData)
2020-06-19 17:10:44 +08:00
}
2020-06-22 17:13:42 +08:00
}
2020-07-03 11:04:25 +08:00
export const getters = {
hasPermissions: (state) => (scenes) => {
let hasPermissions = false;
const role = state.oauth?.role
if (role) {
switch (scenes) {
case 'user':
hasPermissions = role < 5;
break;
case 'role':
hasPermissions = role < 2;
break;
case 'topic':
hasPermissions = role < 3;
break;
case 'tag':
hasPermissions = role < 3;
break;
case 'admin':
hasPermissions = role < 2;
break;
case 'blog_admin':
hasPermissions = role < 3;
break;
default:
hasPermissions = false;
this.commit('logout');
}
}
return hasPermissions;
}
}