فهرست منبع

添加登录权限

zhaoke 3 سال پیش
والد
کامیت
8e09561986
9فایلهای تغییر یافته به همراه36 افزوده شده و 25 حذف شده
  1. 7 3
      src/App.vue
  2. 6 1
      src/permission.js
  3. 3 2
      src/router/index.js
  4. 4 4
      src/store/modules/user.js
  5. 0 1
      src/store/permission.js
  6. 5 5
      src/utils/auth.js
  7. 7 7
      src/utils/request.js
  8. 3 1
      src/views/login/index.vue
  9. 1 1
      src/views/noPower/index.vue

+ 7 - 3
src/App.vue

@@ -1,7 +1,7 @@
 <!--
  * @Author: your name
  * @Date: 2021-10-14 17:17:53
- * @LastEditTime: 2022-01-06 12:03:00
+ * @LastEditTime: 2022-02-28 18:06:18
  * @LastEditors: Please set LastEditors
  * @Description: In User Settings Edit
  * @FilePath: \Foshan4A\src\App.vue
@@ -58,13 +58,17 @@ export default {
     };
   },
   computed: {
-    ...mapGetters(["dialog", "token", "name", "systemSet"]),
+    ...mapGetters(["dialog", "token", "name", "systemSet", "roles"]),
   },
   watch: {
     "$store.state.user.token": {
       handler (val) {
         if (val) {
-          this.handleInit();
+          if (this.roles && this.roles.length) {
+            this.handleInit();
+          } else {
+            this.clearAll();
+          }
         } else {
           this.clearAll();
         }

+ 6 - 1
src/permission.js

@@ -31,9 +31,14 @@ router.beforeEach(async (to, from, next) => {
         next()
       } else {
         try {
-          // get user info
           const { roles } = await store.dispatch('user/getInfo')
           const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
+          // if (roles && roles.length) {
+          //   router.addRoutes(accessRoutes)
+          // } else {
+          //   const noRoutes = [accessRoutes[0]]
+          //   router.addRoutes(noRoutes)
+          // }
           router.addRoutes(accessRoutes)
           next({ ...to, replace: true })
         } catch (error) {

+ 3 - 2
src/router/index.js

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2021-10-14 17:17:53
- * @LastEditTime: 2022-02-28 15:31:42
+ * @LastEditTime: 2022-02-28 17:54:47
  * @LastEditors: Please set LastEditors
  * @Description: In User Settings Edit
  * @FilePath: \Foshan4A\src\router\index.js
@@ -63,10 +63,11 @@ export const constantRoutes = [
   // { path: '*', redirect: '/404', hidden: true }
 ]
 // 插入路由
-const asyncRoutes = []
+export const asyncRoutes = []
 routesPush([...routesOne, ...routesTwo, ...routesThree, ...routesFour, ...routesFive], asyncRoutes)
 asyncRoutes.push({ path: '/', component: () => import('@/views/noPower'), hidden: true })
 asyncRoutes.push({ path: '*', component: () => import('@/views/404'), hidden: true })
+
 const createRouter = () => new Router({
   // mode: 'history', // require service support
   scrollBehavior: () => ({ y: 0 }),

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

@@ -1,6 +1,6 @@
 import { login, loginUp, getInfo } from '@/api/login'
-import { getToken, setToken, removeToken, setUserId } from '@/utils/auth'
-
+import { getToken, setToken, removeToken, setUserId, TokenKey } from '@/utils/auth'
+import { resetRouter } from '@/router'
 const getDefaultState = () => {
   return {
     token: getToken(),
@@ -53,7 +53,7 @@ const actions = {
         commit('SET_UserType', returnData.UserType)
         commit('SET_UserId', returnData.UserId)
         sessionStorage.setItem("userName", userInfo.LoginName);
-        setToken(returnData.Token)
+        setToken(TokenKey, returnData.Token)
         setUserId(returnData.UserId)
         let nowDate = new Date();
         let oldDate = new Date(returnData.ValidTime);
@@ -73,7 +73,7 @@ const actions = {
     return new Promise((resolve, reject) => {
 
       let data = {
-        roles: state.UserType,
+        roles: state.userPoewrList,
         introduction: '',
         avatar: '',
         name: sessionStorage.getItem("userName"),

+ 0 - 1
src/store/permission.js

@@ -20,7 +20,6 @@ function hasPermission (roles, route) {
  */
 export function filterAsyncRoutes (routes, roles) {
   const res = []
-
   routes.forEach(route => {
     const tmp = { ...route }
     if (hasPermission(roles, tmp)) {

+ 5 - 5
src/utils/auth.js

@@ -1,13 +1,13 @@
 import Cookies from 'js-cookie'
 
-const TokenKey = 'token'
+export const TokenKey = 'vue_admin_template_token'
 
-export function getToken () {
-  return Cookies.get(TokenKey)
+export function getToken (name = TokenKey) {
+  return Cookies.get(name)
 }
 
-export function setToken (token) {
-  return Cookies.set(TokenKey, token)
+export function setToken (name = TokenKey, token) {
+  return Cookies.set(name, token)
 }
 
 export function setUserId (id) {

+ 7 - 7
src/utils/request.js

@@ -1,7 +1,7 @@
 /*
  * @Author: your name
  * @Date: 2022-01-06 09:45:17
- * @LastEditTime: 2022-02-27 16:01:01
+ * @LastEditTime: 2022-02-28 18:00:46
  * @LastEditors: Please set LastEditors
  * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  * @FilePath: \vue-admin-template\src\utils\request.js
@@ -22,9 +22,9 @@ const service = axios.create({
     'Content-Type': 'application/json'
   },
 })
-const loadingInstance = Loading.service({
-  text: '数据加载中'
-})
+// const loadingInstance = Loading.service({
+//   text: '数据加载中'
+// })
 // request interceptor
 service.interceptors.request.use(
   config => {
@@ -91,10 +91,10 @@ service.interceptors.response.use(
           })
         })
       }
-      loadingInstance.close()
+      // loadingInstance.close()
       return Promise.reject(new Error(res.message || 'Error'))
     } else {
-      loadingInstance.close()
+      // loadingInstance.close()
       return res
     }
   },
@@ -105,7 +105,7 @@ service.interceptors.response.use(
       type: 'error',
       duration: 5 * 1000
     })
-    loadingInstance.close()
+    // loadingInstance.close()
     return Promise.reject(error)
   }
 )

+ 3 - 1
src/views/login/index.vue

@@ -59,7 +59,7 @@ import Identify from './identify.vue';
 import Dialog from "@/layout/components/Dialog/index.vue";
 import { GetCheckCode, getToken } from '@/api/login';
 import { GetSystemSet } from "@/api/systemConfiguration";
-import { setCodeToken } from '@/utils/auth';
+import { setCodeToken, setToken } from '@/utils/auth';
 import { getAuthListByUser } from '@/api/Account';
 export default {
   name: 'Login',
@@ -163,11 +163,13 @@ export default {
                     }
                     this.$router.push({ path: this.redirect || "/" });
                   } else {
+                    console.log('datas')
                     this.loading = false;
                     this.$store.dispatch("user/logout");
                     this.$message.error('当前用户获取权限失败,请联系管理员');
                   }
                 }).catch(err => {
+                  console.log(err)
                   this.loading = false;
                   this.$store.dispatch("user/logout");
                   this.$message.error('当前用户获取权限失败,请联系管理员');

+ 1 - 1
src/views/noPower/index.vue

@@ -1,6 +1,6 @@
 <template>
   <div class="app-wrapper">
-    <sidebar class="sidebar-container" style="width:88px" />
+    <!-- <sidebar class="sidebar-container" style="width:88px" /> -->
     <div class="main-container">
       <navbar />
     </div>