22
33# ⚡️ Introduction
44
5- A basic solution for middle and background management system, based on vue3, typescript, element-plus and vue-cli 5.x
5+ A basic solution for middle and background management system, based on vue3, typescript, element-plus and pinia
66
77- Electron: [ v3-admin-electron] ( https://github.com/un-pany/v3-admin-electron )
88
@@ -219,17 +219,19 @@ import NProgress from 'nprogress'
219219import ' nprogress/nprogress.css'
220220import router from ' @/router'
221221import { RouteLocationNormalized } from ' vue-router'
222- import { store } from ' @/store'
222+ import { useUserStoreHook } from ' @/store/modules/user'
223+ import { usePermissionStoreHook } from ' @/store/modules/permission'
223224import { ElMessage } from ' element-plus'
224225import { whiteList } from ' @/config/white-list'
225226import rolesSettings from ' @/config/roles'
226227import { getToken } from ' @/utils/cookies'
227228
229+ const userStore = useUserStoreHook ()
230+ const permissionStore = usePermissionStoreHook ()
228231NProgress .configure ({ showSpinner: false })
229232
230233router .beforeEach (async (to : RouteLocationNormalized , _ : RouteLocationNormalized , next : any ) => {
231234 NProgress .start ()
232- const store = useStore ()
233235 // Determine if the user is logged in
234236 if (getToken ()) {
235237 if (to .path === ' /login' ) {
@@ -238,30 +240,30 @@ router.beforeEach(async(to: RouteLocationNormalized, _: RouteLocationNormalized,
238240 NProgress .done ()
239241 } else {
240242 // Check if the user has obtained its permissions role
241- if (store . state . user .roles .length === 0 ) {
243+ if (userStore .roles .length === 0 ) {
242244 try {
243245 if (rolesSettings .openRoles ) {
244246 // Note: The role must be an array! E.g: ['admin'] 或 ['developer', 'editor']
245- await store . dispatch ( ' user/ getInfo' )
247+ await userStore . getInfo ( )
246248 // Fetch the Roles returned by the interface
247- const roles = store . state . user .roles
249+ const roles = userStore .roles
248250 // Generate accessible Routes based on roles
249- store . dispatch ( ' permission/ setRoutes' , roles )
251+ permissionStore . setRoutes ( roles )
250252 } else {
251253 // Enable the default role without turning on the role function
252- store . commit ( ' user/SET_ROLES ' , rolesSettings .defaultRoles )
253- store . dispatch ( ' permission/ setRoutes' , rolesSettings .defaultRoles )
254+ userStore . setRoles ( rolesSettings .defaultRoles )
255+ permissionStore . setRoutes ( rolesSettings .defaultRoles )
254256 }
255257 // Dynamically add accessible Routes
256- store . state . permission .dynamicRoutes .forEach ((route ) => {
258+ permissionStore .dynamicRoutes .forEach ((route ) => {
257259 router .addRoute (route )
258260 })
259261 // Ensure that the added route has been completed
260262 // Set replace: true, so navigation will not leave a history
261263 next ({ ... to , replace: true })
262264 } catch (err : any ) {
263265 // Delete token and redirect to the login page
264- store . dispatch ( ' user/ resetToken' )
266+ userStore . resetToken ( )
265267 ElMessage .error (err || ' Has Error' )
266268 next (' /login' )
267269 NProgress .done ()
0 commit comments