Commit 29f8fb905eb0464a41ac644568c7d0dd7bfd6a7e

Authored by 梁保满
1 parent 4c4f7640

刷新浏览器重新动态添加路由

src/api/apis/login.js
... ... @@ -5,10 +5,6 @@ import loginUrls from "../urls/login"
5 5 export default {
6 6 // 账号密码登陆
7 7 fetchLogin (data) {
8   - // return axios.post(loginUrls.login, data)
9   - return {
10   - status:200,
11   - data:[]
12   - }
  8 + return axios.post(loginUrls.login, data)
13 9 }
14 10 }
... ...
src/router/permission.js
1 1 import NProgress from "nprogress"
2   -
  2 +import en from "../i18n/lang/en"
  3 +import Layout from "@/views/layout/layout"
  4 +import CommerViews from "@/views/commerViews"
3 5 import router from "./index"
4 6 import store from "../store"
  7 +// let routeName = en.routeName
  8 +
  9 +const ExaminationPaper = () => import("@/views/examinationPaper/index")
  10 +const ExaminationPaperAdd = () => import("@/views/examinationPaper/add")
  11 +const ExaminationPaperEdit = () => import("@/views/examinationPaper/edit")
  12 +const ExaminationPaperRecycle = () => import("@/views/examinationPaper/recycle")
  13 +const Ask = () => import("@/views/ask/index")
  14 +const AskAnalysis = () => import("@/views/ask/analysis")
  15 +const Test = () => import("@/views/test/index")
  16 +const TestAnalysis = () => import("@/views/test/analysis")
  17 +const DataSync = () => import("@/views/dataSync/index")
  18 +const Portrait = () => import("@/views/portrait/index")
  19 +const Card = () => import("@/views/card/index")
  20 +const Device = () => import("@/views/device/index")
  21 +const Down = () => import("@/views/down/index")
  22 +const DownClient = () => import("@/views/down/client")
  23 +const SetUpAccount = () => import("@/views/setUp/account")
  24 +const SetUpConglomerate = () => import("@/views/setUp/conglomerate")
  25 +const SetUpSchool = () => import("@/views/setUp/school")
  26 +const SetUpStudent = () => import("@/views/setUp/student")
  27 +const SetUpTeacher = () => import("@/views/setUp/teacher")
  28 +let addrouters = [ //测试用,后续后端获取
  29 + {
  30 + path: "/examinationPaper",
  31 + iconCls: "fa fa-file-text", // 图标样式class
  32 + name: "备题组卷",
  33 + component: Layout,
  34 + alone: true,
  35 + children: [
  36 + {
  37 + path: "/examinationPaper",
  38 + iconCls: "fa fa-file-text", // 图标样式class
  39 + name: "",
  40 + component: ExaminationPaper,
  41 + children: []
  42 + },
  43 + {
  44 + path: "/examinationPaperAdd",
  45 + iconCls: "", // 图标样式class
  46 + name: "添加答题卡",
  47 + component: ExaminationPaperAdd,
  48 + children: []
  49 + },
  50 + {
  51 + path: "/examinationPaperEdit",
  52 + iconCls: "", // 图标样式class
  53 + name: "修改答题卡",
  54 + component: ExaminationPaperEdit,
  55 + children: []
  56 + },
  57 + {
  58 + path: "/examinationPaperRecycle",
  59 + iconCls: "", // 图标样式class
  60 + name: "已归档答题卡",
  61 + component: ExaminationPaperRecycle,
  62 + children: []
  63 + },
  64 + ]
  65 + },
  66 + {
  67 + path: "/ask",
  68 + iconCls: "fa fa-bar-chart", // 图标样式class
  69 + name: "随堂问报表",
  70 + component: Layout,
  71 + alone: true,
  72 + children: [
  73 + {
  74 + path: "/ask",
  75 + iconCls: "fa fa-bar-chart", // 图标样式class
  76 + name: "",
  77 + component: Ask,
  78 + children: []
  79 +
  80 + },
  81 + {
  82 + path: "/askAnalysis",
  83 + iconCls: "", // 图标样式class
  84 + name: "随堂问报表分析",
  85 + component: AskAnalysis,
  86 + children: []
  87 + }
  88 + ]
  89 + },
  90 + {
  91 + path: "/test",
  92 + iconCls: "fa fa-pie-chart", // 图标样式class
  93 + name: "即时测报表",
  94 + component: Layout,
  95 + alone: true,
  96 + children: [
  97 + {
  98 + path: "/test",
  99 + iconCls: "fa fa-pie-chart", // 图标样式class
  100 + name: "",
  101 + component: Test,
  102 + children: []
  103 + },
  104 + {
  105 + path: "/testAnalysis",
  106 + iconCls: "", // 图标样式class
  107 + name: "即时测报表分析",
  108 + component: TestAnalysis,
  109 + children: []
  110 + }
  111 +
  112 + ]
  113 + },
  114 + {
  115 + path: "/portrait",
  116 + iconCls: "fa fa-users", // 图标样式class
  117 + name: "学生画像",
  118 + component: Layout,
  119 + alone: true,
  120 + children: [
  121 + {
  122 + path: "/portrait",
  123 + iconCls: "fa fa-users", // 图标样式class
  124 + name: "",
  125 + component: Portrait,
  126 + children: []
  127 + }
  128 + ]
  129 + },
  130 + {
  131 + path: "/dataSync",
  132 + iconCls: "fa fa-random", // 图标样式class
  133 + name: "数据同步",
  134 + component: Layout,
  135 + alone: true,
  136 + children: [
  137 + {
  138 + path: "/dataSync",
  139 + iconCls: "fa fa-random", // 图标样式class
  140 + name: "",
  141 + component: DataSync,
  142 + children: []
  143 + }
  144 + ]
  145 + },
  146 + {
  147 + path: "/",
  148 + iconCls: "fa fa-cog",
  149 + name: '信息管理',
  150 + component: Layout,
  151 + children: [
  152 + {
  153 + path: "/setUpAccount",
  154 + iconCls: "fa fa-id-card-o",
  155 + name: '账号管理',
  156 + component: SetUpAccount,
  157 + children: []
  158 + },
  159 + {
  160 + path: "/setUpConglomerate",
  161 + iconCls: "fa fa-building",
  162 + name: '集团管理',
  163 + component: SetUpConglomerate,
  164 + children: []
  165 + },
  166 + {
  167 + path: "/setUpSchool",
  168 + iconCls: "fa fa-calculator",
  169 + name: '学校管理',
  170 + component: SetUpSchool,
  171 + children: []
  172 + },
  173 + {
  174 + path: "/setUpTeacher",
  175 + iconCls: "fa fa-male",
  176 + name: '教师管理',
  177 + component: SetUpTeacher,
  178 + children: []
  179 + },
  180 + {
  181 + path: "/setUpStudent",
  182 + iconCls: "fa fa-mortar-board",
  183 + name: '学生管理',
  184 + component: SetUpStudent,
  185 + children: []
  186 + },
  187 + ]
  188 + },
  189 + {
  190 + path: "/card",
  191 + iconCls: "fa fa-id-card", // 图标样式class
  192 + name: "发卡记录",
  193 + component: Layout,
  194 + alone: true,
  195 + children: [
  196 + {
  197 + path: "/card",
  198 + iconCls: "fa fa-id-card", // 图标样式class
  199 + name: "",
  200 + component: Card,
  201 + children: []
  202 + }
  203 + ]
  204 + },
  205 + {
  206 + path: "/device",
  207 + iconCls: "fa fa-dashboard", // 图标样式class
  208 + name: "设备状态",
  209 + component: Layout,
  210 + alone: true,
  211 + children: [
  212 + {
  213 + path: "/device",
  214 + iconCls: "fa fa-dashboard", // 图标样式class
  215 + name: "",
  216 + component: Device,
  217 + children: []
  218 + }
  219 + ]
  220 + },
  221 + {
  222 + path: "/down",
  223 + iconCls: "fa fa-download", // 图标样式class
  224 + name: "软件下载",
  225 + component: Layout,
  226 + alone: true,
  227 + children: [
  228 + {
  229 + path: "/down",
  230 + iconCls: "fa fa-download", // 图标样式class
  231 + name: "",
  232 + component: Down,
  233 + children: []
  234 + },
  235 + {
  236 + path: "/downClient",
  237 + iconCls: "", // 图标样式class
  238 + name: "",
  239 + component: DownClient,
  240 + children: []
  241 + }
  242 + ]
  243 + },
  244 +
  245 + {
  246 + path: "*",
  247 + redirect: "/404",
  248 + hidden: true,
  249 + children: []
  250 + }
  251 +]
  252 +
5 253  
6 254 // 获取角色信息,根据用户权限动态加载路由
7 255 router.beforeEach((to, from, next) => {
... ... @@ -10,20 +258,33 @@ router.beforeEach((to, from, next) => {
10 258 if (to.path === "/login") {
11 259 next()
12 260 } else {
13   - // if (!store.getters.info.permissions) {
14   - // next({ path: to.path })
15   - // } else {
16   - // let is404 = to.matched.some(record => {
17   - // if (record.meta.role) {
18   - // return store.getters.info.authorityRouter === -1
19   - // }
20   - // })
21   - // if (is404) {
22   - // next({ path: "/404" })
23   - // return false
24   - // }
  261 + if (!store.getters.info.permissions) {
  262 + !(async function getAddRouters() {
  263 + // 省略 axios 请求代码 通过 token 向后台请求用户权限等信息,这里用假数据赋值
  264 + await store.commit("setInfo", {
  265 + permissions: [
  266 + {
  267 + role: "superAdmin",
  268 + roleName: "超级管理员"
  269 + }
  270 + ],
  271 + name: "张老师",
  272 + // avatar: data.avatar ? data.avatar : "",
  273 + // uid: data.id,
  274 + // authorityRouter:[],
  275 +
  276 + })
  277 + await store.commit("setRouters", addrouters)
  278 + let newAddRouters = store.getters.addRouters
  279 + // await router.addRoutes(newAddRouters)
  280 + newAddRouters.forEach(res => {
  281 + router.addRoute(res)
  282 + })
  283 + next({ path: to.path })
  284 + }())
  285 + } else {
25 286 next()
26   - // }
  287 + }
27 288 }
28 289 } else {
29 290 if (to.path === "/login") {
... ...
src/store/index.js
... ... @@ -285,31 +285,55 @@ const store = new Vuex.Store({
285 285 },
286 286 actions: {
287 287 Login({ commit }, that) {
288   - console.log(that.$router)
289   - debugger
290 288 // request.fetchLogin(that.loginForm).then(res => {
291   - // if (res.status == 200) {
292   - commit("setToken", "xxxx")
293   - commit("setInfo", {
294   - permissions: [
295   - {
296   - role: "superAdmin",
297   - roleName: "超级管理员",
298   - },
299   - ],
300   - name: "张老师",
301   - // avatar: data.avatar ? data.avatar : "",
302   - // uid: data.id,
303   - // authorityRouter:[],
304   - });
305   - commit("setRouters", addrouters)
306   - // let newAddRouters = getters.addRouters;
307   - // await router.addRoutes(newAddRouters)
308   - addrouters.forEach((res) => {
309   - that.$router.addRoute(res);
310   - });
311   - that.$router.push({ path: "/" })
312   - // }
  289 + // if (res.status == 200) {
  290 + commit("setToken", "xxxx")
  291 + commit("setInfo", {
  292 + permissions: [
  293 + {
  294 + role: "superAdmin",
  295 + roleName: "超级管理员",
  296 + },
  297 + ],
  298 + name: "张老师",
  299 + // avatar: data.avatar ? data.avatar : "",
  300 + // uid: data.id,
  301 + // authorityRouter:[],
  302 + });
  303 + commit("setRouters", addrouters)
  304 + // let newAddRouters = getters.addRouters;
  305 + // await router.addRoutes(newAddRouters)
  306 + addrouters.forEach((res) => {
  307 + that.$router.addRoute(res);
  308 + });
  309 + that.$router.push({ path: "/" })
  310 + // }
  311 + // })
  312 + },
  313 + permissions({ commit }, that) {
  314 + // request.fetchLogin(that.loginForm).then(res => {
  315 + // if (res.status == 200) {
  316 + commit("setToken", "xxxx")
  317 + commit("setInfo", {
  318 + permissions: [
  319 + {
  320 + role: "superAdmin",
  321 + roleName: "超级管理员",
  322 + },
  323 + ],
  324 + name: "李老师",
  325 + // avatar: data.avatar ? data.avatar : "",
  326 + // uid: data.id,
  327 + // authorityRouter:[],
  328 + });
  329 + commit("setRouters", addrouters)
  330 + // let newAddRouters = getters.addRouters;
  331 + // await router.addRoutes(newAddRouters)
  332 + addrouters.forEach((res) => {
  333 + that.$router.addRoute(res);
  334 + });
  335 + that.$router.push({ path: "/" })
  336 + // }
313 337 // })
314 338 }
315 339 },
... ...