Skip to content

Commit

Permalink
feat(system-client): impl application & sign in/up pages;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Aug 31, 2021
1 parent 1fbf8cd commit 3bbb30f
Show file tree
Hide file tree
Showing 12 changed files with 450 additions and 161 deletions.
5 changes: 1 addition & 4 deletions packages/system-client/.env.development
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@
ENV = 'development'

# base api
VUE_APP_BASE_API = '/dev-api'

# app server 请求地址
VUE_APP_BASE_API_APP = '/dev-api/app'
VUE_APP_BASE_API = '/dev-api'
2 changes: 0 additions & 2 deletions packages/system-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"build:prod": "vue-cli-service build",
"build:stage": "vue-cli-service build --mode staging",
"preview": "node build/index.js --preview",
"new": "plop",
"svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml",
"commit": "commit"
},
Expand Down Expand Up @@ -56,7 +55,6 @@
"html-webpack-plugin": "3.2.0",
"lint-staged": "^11.1.0",
"monaco-editor-webpack-plugin": "^4.0.0",
"plop": "^2.7.4",
"runjs": "^4.1.3",
"sass": "1.26.2",
"sass-loader": "8.0.2",
Expand Down
5 changes: 0 additions & 5 deletions packages/system-client/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'
import './styles/element-variables.scss'
// import enLang from 'element-ui/lib/locale/lang/en'// 如果使用中文语言包请默认支持,无需额外引入,请删除该依赖

import '@/styles/index.scss' // global css

Expand All @@ -19,12 +18,8 @@ import './permission' // permission control
import './utils/error-log' // error log

import * as filters from './filters' // global filters
import permission_directive from '@/directive/permission/index.js' // 权限判断指令
import role_directive from '@/directive/role/index.js' // 权限判断指令
import clipboard from '@/directive/clipboard/index'

Vue.use(permission_directive)
Vue.use(role_directive)
Vue.use(clipboard)

Vue.use(Element, {
Expand Down
32 changes: 1 addition & 31 deletions packages/system-client/src/permission.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
Expand All @@ -26,35 +24,7 @@ router.beforeEach(async(to, from, next) => {
next({ path: '/' })
NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
} else {
// determine whether the user has obtained his permission roles through getInfo
const hasRoles = store.getters.roles && store.getters.roles.length > 0
const hasPermissions = store.getters.permissions && store.getters.permissions.length > 0
if (hasRoles || hasPermissions) {
next()
} else {
try {
// get user info
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
// note: permissions must be a object array! such as: ['admin.create'] or ,['role.delete','role.edit']
const { roles, permissions } = await store.dispatch('user/getInfo')

// generate accessible routes map based on roles
const accessRoutes = await store.dispatch('permission/generateRoutes', { roles, permissions })

// dynamically add accessible routes
router.addRoutes(accessRoutes)

// hack method to ensure that addRoutes is complete
// set the replace: true, so the navigation will not leave a history record
next({ ...to, replace: true })
} catch (error) {
// remove token and go to login page to re-login
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
next()
}
} else {
/* has no token*/
Expand Down
19 changes: 12 additions & 7 deletions packages/system-client/src/router/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ export const constantRoutes = [
},
{
path: '/login',
component: () => import('@/views/login/index'),
component: () => import('@/views/account/sign-in'),
hidden: true
},
{
path: '/signup',
component: () => import('@/views/account/sign-up'),
hidden: true
},
{
path: '/auth-redirect',
component: () => import('@/views/login/auth-redirect'),
component: () => import('@/views/account/auth-redirect'),
hidden: true
},
{
Expand All @@ -40,13 +45,13 @@ export const constantRoutes = [
{
path: '/',
component: Layout,
redirect: '/dashboard',
redirect: '/applications',
children: [
{
path: 'dashboard',
component: () => import('@/views/dashboard/index'),
name: 'Dashboard',
meta: { title: '数据看板', icon: 'dashboard', affix: true }
path: 'applications',
component: () => import('@/views/application/index'),
name: 'Application',
meta: { title: '主页', icon: 'dashboard', affix: true, noCache: true }
}
]
}
Expand Down
65 changes: 6 additions & 59 deletions packages/system-client/src/store/modules/user.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
import { login, getInfo } from '@/api/user'
import { getToken, setToken, removeToken, setDebugToken, removeDebugToken, setFileToken, removeFileToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const state = {
token: getToken(),
name: '',
avatar: '',
introduction: '',
roles: [],
permissions: []
avatar: ''
}

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 || []
},
SET_PERMISSIONS: (state, permissions) => {
state.permissions = permissions || []
}
}

Expand All @@ -41,12 +29,6 @@ const actions = {
const { data } = response
commit('SET_TOKEN', data.access_token)
setToken(data.access_token, data.expire)
if (data.debug_token) {
setDebugToken(data.debug_token)
}
if (data.file_token) {
setFileToken(data.file_token)
}
resolve()
}).catch(error => {
reject(error)
Expand All @@ -64,20 +46,10 @@ const actions = {
reject('Verification failed, please Login again.')
}

const admin = data
const roles = admin.roles
const permissions = admin.permissions

// roles must be a non-empty array
if (!roles || roles.length <= 0) {
reject('getInfo: roles must be a non-null array!')
}
const account = data

commit('SET_ROLES', roles)
commit('SET_PERMISSIONS', permissions)
commit('SET_NAME', admin.name)
commit('SET_AVATAR', admin.avatar)
commit('SET_INTRODUCTION', admin.description)
commit('SET_NAME', account.name || account.username)
commit('SET_AVATAR', account.avatar)

resolve(data)
}).catch(error => {
Expand All @@ -90,12 +62,8 @@ const actions = {
logout({ commit, state, dispatch }) {
return new Promise((resolve, reject) => {
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
commit('SET_PERMISSIONS', [])

removeToken()
removeDebugToken()
removeFileToken()
resetRouter()

// reset visited views and cached views
Expand All @@ -109,30 +77,9 @@ const actions = {
resetToken({ commit }) {
return new Promise(resolve => {
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
removeToken()
resolve()
})
},

// dynamically modify permissions
async changeRoles({ commit, dispatch }, role) {
const token = role + '-token'

commit('SET_TOKEN', token)
setToken(token)

const { roles } = await dispatch('getInfo')

resetRouter()

// generate accessible routes map based on roles
const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
// dynamically add accessible routes
router.addRoutes(accessRoutes)

// reset visited views and cached views
dispatch('tagsView/delAllViews', null, { root: true })
}
}

Expand Down
Loading

0 comments on commit 3bbb30f

Please sign in to comment.