Skip to content

Commit

Permalink
feat(server-client): add app to vuex store;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 2, 2021
1 parent a4a3923 commit 3abeb9d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 46 deletions.
4 changes: 2 additions & 2 deletions packages/system-client/src/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
title: 'LaF 开发运维控制台',
title: 'LaF 云开发',

/**
* @type {boolean} true | false
Expand All @@ -23,7 +23,7 @@ module.exports = {
* @type {boolean} true | false
* @description Whether show the logo in sidebar
*/
sidebarLogo: true,
sidebarLogo: false,

/**
* @type {string | array} 'production' | ['production', 'development']
Expand Down
12 changes: 6 additions & 6 deletions packages/system-client/src/store/getters.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const getters = {
sidebar: state => state.app.sidebar,
size: state => state.app.size,
device: state => state.app.device,
sidebar: state => state.settings.sidebar,
size: state => state.settings.size,
device: state => state.settings.device,
visitedViews: state => state.tagsView.visitedViews,
cachedViews: state => state.tagsView.cachedViews,
token: state => state.user.token,
avatar: state => state.user.avatar,
name: state => state.user.name,
introduction: state => state.user.introduction,
roles: state => state.user.roles,
permissions: state => state.user.permissions,
application: state => state.app.application,
roles: state => state.app.roles,
permissions: state => state.app.permissions,
permission_routes: state => state.permission.routes,
errorLogs: state => state.errorLog.logs
}
Expand Down
69 changes: 35 additions & 34 deletions packages/system-client/src/store/modules/app.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,51 @@
import Cookies from 'js-cookie'
import { getApplicationByAppid } from '@/api/application'
import { assert } from '@/utils/assert'

const state = {
sidebar: {
opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
withoutAnimation: false
},
device: 'desktop',
size: Cookies.get('size') || 'medium'
/**
* 当前应用对象
*/
application: null,
/**
* 用户在当前应用的角色
*/
roles: [],
/**
* 用户对当前应用的权限
*/
permissions: []
}

const mutations = {
TOGGLE_SIDEBAR: state => {
state.sidebar.opened = !state.sidebar.opened
state.sidebar.withoutAnimation = false
if (state.sidebar.opened) {
Cookies.set('sidebarStatus', 1)
} else {
Cookies.set('sidebarStatus', 0)
}
SET_APPLICATION: (state, payload) => {
state.application = payload
},
CLOSE_SIDEBAR: (state, withoutAnimation) => {
Cookies.set('sidebarStatus', 0)
state.sidebar.opened = false
state.sidebar.withoutAnimation = withoutAnimation
SET_APP_ROLES: (state, payload) => {
state.roles = payload || []
},
TOGGLE_DEVICE: (state, device) => {
state.device = device
SET_APP_PERMISSIONS: (state, payload) => {
state.permissions = payload || []
},
SET_SIZE: (state, size) => {
state.size = size
Cookies.set('size', size)
CLEAR_STATE: (state) => {
state.application = null
state.roles = []
state.permissions = []
}
}

const actions = {
toggleSideBar({ commit }) {
commit('TOGGLE_SIDEBAR')
},
closeSideBar({ commit }, { withoutAnimation }) {
commit('CLOSE_SIDEBAR', withoutAnimation)
},
toggleDevice({ commit }, device) {
commit('TOGGLE_DEVICE', device)
async loadCurrentApplication({ commit }, appid) {
const res = await getApplicationByAppid(appid)
assert(res.data?.application, 'empty `res.data?.application` got')
assert(res.data?.roles, 'empty `res.data?.roles` got')
assert(res.data?.permissions, 'empty `res.data?.permissions` got')

commit('SET_APPLICATION', res.data?.application)
commit('SET_APP_ROLES', res.data?.roles)
commit('SET_APP_PERMISSIONS', res.data?.permissions)
},
setSize({ commit }, size) {
commit('SET_SIZE', size)
clearStates({ commit }) {
commit('CLEAR_STATE')
}
}

Expand Down
18 changes: 16 additions & 2 deletions packages/system-client/src/store/modules/permission.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { asyncRoutes, constantRoutes } from '@/router'

/**
* Use meta.permission to determine if the current user has permission
* @param roles
* @param permissions
* @param route
*/
function hasPermission(permissions, route) {
Expand Down Expand Up @@ -47,6 +47,19 @@ export function filterAsyncRoutes(routes, { roles, permissions }) {
return res
}

/**
* render appid into routes' path
* @param {any[]} routes
* @param {string} appid
*/
function fillAppId2Routes(routes, appid) {
for (const route of routes) {
if (route.path.startsWith('/app/:appid/')) {
route.path = route.path.replace(`/app/:appid/`, `/app/${appid}/`)
}
}
}

const state = {
routes: [],
addRoutes: []
Expand All @@ -60,8 +73,9 @@ const mutations = {
}

const actions = {
async generateRoutes({ commit }, { roles, permissions }) {
async generateRoutes({ commit }, { appid, roles, permissions }) {
const accessedRoutes = filterAsyncRoutes(asyncRoutes, { roles, permissions })
fillAppId2Routes(accessedRoutes, appid)
commit('SET_ROUTES', accessedRoutes)
return accessedRoutes
}
Expand Down
42 changes: 41 additions & 1 deletion packages/system-client/src/store/modules/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import variables from '@/styles/element-variables.scss'
import defaultSettings from '@/settings'
import Cookies from 'js-cookie'

const { showSettings, tagsView, fixedHeader, sidebarLogo } = defaultSettings

Expand All @@ -8,7 +9,13 @@ const state = {
showSettings: showSettings,
tagsView: tagsView,
fixedHeader: fixedHeader,
sidebarLogo: sidebarLogo
sidebarLogo: sidebarLogo,
sidebar: {
opened: true,
withoutAnimation: true
},
device: 'desktop',
size: Cookies.get('size') || 'medium'
}

const mutations = {
Expand All @@ -17,12 +24,45 @@ const mutations = {
if (state.hasOwnProperty(key)) {
state[key] = value
}
},
TOGGLE_SIDEBAR: state => {
state.sidebar.opened = !state.sidebar.opened
state.sidebar.withoutAnimation = false
if (state.sidebar.opened) {
Cookies.set('sidebarStatus', 1)
} else {
Cookies.set('sidebarStatus', 0)
}
},
CLOSE_SIDEBAR: (state, withoutAnimation) => {
Cookies.set('sidebarStatus', 0)
state.sidebar.opened = false
state.sidebar.withoutAnimation = withoutAnimation
},
TOGGLE_DEVICE: (state, device) => {
state.device = device
},
SET_SIZE: (state, size) => {
state.size = size
Cookies.set('size', size)
}
}

const actions = {
changeSetting({ commit }, data) {
commit('CHANGE_SETTING', data)
},
toggleSideBar({ commit }) {
commit('TOGGLE_SIDEBAR')
},
closeSideBar({ commit }, { withoutAnimation }) {
commit('CLOSE_SIDEBAR', withoutAnimation)
},
toggleDevice({ commit }, device) {
commit('TOGGLE_DEVICE', device)
},
setSize({ commit }, size) {
commit('SET_SIZE', size)
}
}

Expand Down
7 changes: 6 additions & 1 deletion packages/system-client/src/utils/get-page-title.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import defaultSettings from '@/settings'
import store from '@/store'

const title = defaultSettings.title || 'Less Admin'
const title = defaultSettings.title

export default function getPageTitle(pageTitle) {
const app = store.state.app.application
if (app) {
return `${pageTitle} - ${app.name}`
}
if (pageTitle) {
return `${pageTitle} - ${title}`
}
Expand Down

0 comments on commit 3abeb9d

Please sign in to comment.