Selaa lähdekoodia

feat():init

geek 4 vuotta sitten
vanhempi
commit
75227bd0ec

+ 3 - 2
src/layout/components/Sidebar/index.vue

@@ -12,7 +12,7 @@
         :collapse-transition="false"
         mode="vertical"
       >
-        <sidebar-item v-for="route in routes" :key="route.path" :item="route" :base-path="route.path" />
+        <sidebar-item v-for="route in permission_routes" :key="route.path" :item="route" :base-path="route.path" />
       </el-menu>
     </el-scrollbar>
   </div>
@@ -28,7 +28,8 @@ export default {
   components: { SidebarItem, Logo },
   computed: {
     ...mapGetters([
-      'sidebar'
+      'sidebar',
+      'permission_routes'
     ]),
     routes() {
       return this.$router.options.routes

+ 3 - 1
src/permission.js

@@ -28,6 +28,8 @@ router.beforeEach(async(to, from, next) => {
     } else {
       // determine whether the user has obtained his permission roles through getInfo
       const hasRoles = store.getters.roles && store.getters.roles.length > 0
+      // const hasRoles = store.getters.isRole
+      // debugger
       console.log(store.getters.roles)
       if (hasRoles) {
         next()
@@ -46,7 +48,7 @@ router.beforeEach(async(to, from, next) => {
           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()
+          next({ ...to, replace: true })
         } catch (error) {
           // remove token and go to login page to re-login
           await store.dispatch('user/resetToken')

+ 15 - 39
src/router/index.js

@@ -54,33 +54,38 @@ export const constantRoutes = [
       meta: { title: '首页', icon: 'dashboard' }
     }]
   },
+  // 404 page must be placed at the end !!!
+  { path: '*', redirect: '/404', hidden: true }
+]
+
+export const asyncRoutes = [
   {
     path: '/home',
     component: Layout,
     meta: {
       title: '图片列表',
       icon: 'el-icon-setting',
-      role: ['admin']
+      roles: ['admin']
     },
     children: [
       {
         path: 'list',
         name: 'home',
         component: () => import('@/views/home/list'),
-        meta: { title: '图片列表', icon: 'el-icon-s-help' }
+        meta: { title: '图片列表', icon: 'el-icon-s-help', roles: ['admin'] }
       },
       {
         path: 'edit/:id(\\d+)',
         name: 'edithome',
         component: () => import('@/views/home/edit'),
-        meta: { title: '查看图片', icon: 'el-icon-s-help' },
+        meta: { title: '查看图片', icon: 'el-icon-s-help', roles: ['admin'] },
         hidden: true
       },
       {
         path: 'create',
         name: 'createHome',
         component: () => import('@/views/home/create'),
-        meta: { title: '创建图片', icon: 'el-icon-s-help' }
+        meta: { title: '创建图片', icon: 'el-icon-s-help', roles: ['admin'] }
       }
     ]
   },
@@ -91,7 +96,7 @@ export const constantRoutes = [
     meta: {
       title: '基本设置',
       icon: 'el-icon-setting',
-      role: ['admin']
+      roles: ['admin']
     },
     children: [
       {
@@ -109,7 +114,7 @@ export const constantRoutes = [
     meta: {
       title: '基本设置',
       icon: 'el-icon-setting',
-      role: ['admin']
+      roles: ['admin']
     },
     children: [
       {
@@ -128,7 +133,7 @@ export const constantRoutes = [
     meta: {
       title: '轮播设置',
       icon: 'el-icon-s-help',
-      role: ['admin']
+      roles: ['admin']
     },
     children: [
       {
@@ -156,7 +161,7 @@ export const constantRoutes = [
     path: '/city',
     component: Layout,
     meta: {
-      role: ['admin']
+      roles: ['admin']
     },
     children: [
       {
@@ -171,7 +176,7 @@ export const constantRoutes = [
     path: '/join',
     component: Layout,
     meta: {
-      role: ['admin']
+      roles: ['admin']
     },
     children: [
       {
@@ -193,36 +198,7 @@ export const constantRoutes = [
         path: 'list',
         name: 'live',
         component: () => import('@/views/live/list'),
-        meta: { title: '日志列表', icon: 'el-icon-s-help' },
-        roles: ['admin', 'editor']
-      },
-      {
-        path: 'edit/:id(\\d+)',
-        component: () => import('@/views/live/edit'),
-        name: 'EditBanner',
-        meta: { title: '查看日志参数', noCache: true, activeMenu: '/live/list' },
-        hidden: true
-      }
-    ]
-  },
-  // 404 page must be placed at the end !!!
-  { path: '*', redirect: '/404', hidden: true }
-]
-
-export const asyncRoutes = [
-  {
-    path: '/live',
-    component: Layout,
-    meta: {
-      roles: ['admin', 'editor']
-    },
-    children: [
-      {
-        path: 'list',
-        name: 'live',
-        component: () => import('@/views/live/list'),
-        meta: { title: '日志列表', icon: 'el-icon-s-help' },
-        roles: ['admin', 'editor']
+        meta: { title: '日志列表', icon: 'el-icon-s-help' }
       },
       {
         path: 'edit/:id(\\d+)',

+ 2 - 1
src/store/getters.js

@@ -5,6 +5,7 @@ const getters = {
   avatar: state => state.user.avatar,
   name: state => state.user.name,
   roles: state => state.user.roles,
-  permission_routes: state => state.permission.routes,
+  isRole: state => state.user.isRole,
+  permission_routes: state => state.permission.routes
 }
 export default getters

+ 0 - 4
src/store/modules/permission.js

@@ -43,9 +43,6 @@ const mutations = {
   SET_ROUTES: (state, routes) => {
     state.addRoutes = routes
     state.routes = constantRoutes.concat(routes)
-  },
-  SET_ROLES: (state, roles) => {
-    state.roles = roles
   }
 }
 
@@ -59,7 +56,6 @@ const actions = {
         accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
       }
       commit('SET_ROUTES', accessedRoutes)
-      commit('SET_ROLES', roles)
       resolve(accessedRoutes)
     })
   }

+ 6 - 6
src/store/modules/user.js

@@ -6,7 +6,8 @@ const getDefaultState = () => {
   return {
     token: getToken(),
     name: '',
-    avatar: ''
+    avatar: '',
+    roles: []
   }
 }
 
@@ -39,7 +40,6 @@ const actions = {
         const { data } = response
         console.log(data)
         commit('SET_TOKEN', data.token)
-        // commit('SET_ROLES', data.role)
         setToken(data.token)
         resolve(data)
       }).catch(error => {
@@ -58,15 +58,15 @@ const actions = {
           return reject('Verification failed, please Login again.')
         }
 
-        const { name, avatar } = data
+        // const { name, avatar } = data
         const roles = data.role
-        console.log(roles)
+        console.log(data)
         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_NAME', data.name)
+        commit('SET_AVATAR', data.avatar)
         resolve(data)
       }).catch(error => {
         reject(error)