Blame view

src/store/index.js 4.78 KB
c1b532ad   梁保满   权限配置,路由基础设置
1
2
3
  import Vue from "vue"
  import Vuex from "vuex"
  import Cookies from "js-cookie"
4c4f7640   梁保满   路由表,路由前端文件
4
5
  import layoutStore from "./modules/layout/index"
  import { defaultRouter } from "@/router/index"
13b58a42   梁保满   备题组卷部分前端页面基本完成
6
  import { encryptLoginPassword } from "@/utils";
4c4f7640   梁保满   路由表,路由前端文件
7
8
9
  
  import request from "@/api/index"
  import router from "@/router/index"
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
10
  import { addrouters } from "@/router/index"
c1b532ad   梁保满   权限配置,路由基础设置
11
12
13
14
  Vue.use(Vuex)
  
  const store = new Vuex.Store({
    state: {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
15
16
17
18
19
20
      token: "",
      csCode: localStorage.getItem("csCode") || "",
      info: sessionStorage.getItem("info") ? JSON.parse(sessionStorage.getItem("info")) : "", // 每次刷新都要通过token请求个人信息来筛选动态路由
      routers: [],//左侧菜单
      addRouters: sessionStorage.getItem("addRouters") ? JSON.parse(sessionStorage.getItem("addRouters")) : [],//动态路由
      tokenSources: new Map(),//正在请求接口(切换取消请求)
c1b532ad   梁保满   权限配置,路由基础设置
21
22
    },
    mutations: {
4c4f7640   梁保满   路由表,路由前端文件
23
      setToken(state, token) {
c1b532ad   梁保满   权限配置,路由基础设置
24
        state.token = token
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
25
26
27
28
      },
      setCode(state, code) {
        state.csCode = code;
        localStorage.setItem("csCode", code)
4c4f7640   梁保满   路由表,路由前端文件
29
30
31
32
33
34
35
36
      },
      setInfo(state, data) {
        state.info = {
          role: data.role,
          permissions: data.permissions,
          name: data.name,
          authorityRouter: data.authorityRouter,
          avatar: data.avatar ? data.avatar : "",
13b58a42   梁保满   备题组卷部分前端页面基本完成
37
          uid: data.uid
4c4f7640   梁保满   路由表,路由前端文件
38
        }
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
39
        sessionStorage.setItem("info", JSON.stringify(state.info))
4c4f7640   梁保满   路由表,路由前端文件
40
41
      },
      setRouters: (state, routers) => {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
42
43
44
45
46
47
48
49
        let aRouters = addrouters.filter(item => {
          let path = item.children[0]?.path.replace("/", "")
          return routers?.includes(path)
        })
  
        state.addRouters = aRouters // 保存动态路由用来addRouter
        state.routers = defaultRouter.concat(aRouters) // 所有有权限的路由表,用来生成菜单列表
        sessionStorage.setItem("addRouters", JSON.stringify(routers))
4c4f7640   梁保满   路由表,路由前端文件
50
      },
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
51
52
      setTokenSources(state, data) {
        if (data instanceof Array) {
65f592b6   梁保满   答题卡列表页
53
          state.tokenSources.set(data[0], data[1])
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
54
        } else {
65f592b6   梁保满   答题卡列表页
55
56
57
          state.tokenSources = new Map()
        }
      },
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
58
      delTokenSources(state, data) {
65f592b6   梁保满   答题卡列表页
59
60
        state.tokenSources.delete(data)
      },
c1b532ad   梁保满   权限配置,路由基础设置
61
62
    },
    actions: {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
63
64
      Login({ state, commit }, params) {
        let loginForm = {}
13b58a42   梁保满   备题组卷部分前端页面基本完成
65
66
        loginForm.username = params.username;
        loginForm.password = encryptLoginPassword(params.password);
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        request.fetchLogin({ ...loginForm }).then(res => {
          let response = res.data
          if (response.status == 0) {
            const userInfo = { ...response.data }
            commit("setToken", "isLogin");
            commit("setInfo", { ...userInfo });
            commit("setRouters", [...userInfo.authorityRouter])
            state.addRouters.forEach((res) => {
              router.addRoute(res);
            });
            router.addRoute({
              path: "*",
              redirect: "/404",
              hidden: true,
              children: []
            })
            console.log()
            if (params.url) {
              window.location.href = params.url;
            } else {
              router.push({ path: "/" })
            }
          }
        }).catch(() => {
          // commit("setToken", Cookies.get("JSESSIONID"));
        })
29f8fb90   梁保满   刷新浏览器重新动态添加路由
93
      },
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
94
      permissions({ state, commit }, that) {
29f8fb90   梁保满   刷新浏览器重新动态添加路由
95
96
97
98
99
100
101
102
103
104
105
106
107
        // request.fetchLogin(that.loginForm).then(res => {
        // if (res.status == 200) {
        commit("setToken", "xxxx")
        commit("setInfo", {
          permissions: [
            {
              role: "superAdmin",
              roleName: "超级管理员",
            },
          ],
          name: "李老师",
          // avatar: data.avatar ? data.avatar : "",
          // uid: data.id,
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
          authorityRouter:[
            "examinationPaper",
            "examinationPaperAdd",
            "examinationPaperEdit",
            "examinationPaperRecycle",
            "ask",
            "askAnalysis",
            "test",
            "testAnalysis",
            "portrait",
            "setUpConglomerate",
            "setUpAccount",
            "setUpSchool",
            "setUpTeacher",
            "setUpStudent",
            "card",
            "device",
            "analysis",
            "down",
            "downClient",
            "dataSync"
          ],
29f8fb90   梁保满   刷新浏览器重新动态添加路由
130
        });
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
131
        commit("setRouters", authorityRouter)
13b58a42   梁保满   备题组卷部分前端页面基本完成
132
        state.addRouters.forEach((res) => {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
133
          router.addRoute(res);
29f8fb90   梁保满   刷新浏览器重新动态添加路由
134
        });
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
135
136
137
138
139
140
        router.addRoute({
          path: "*",
          redirect: "/404",
          hidden: true,
          children: []
        })
29f8fb90   梁保满   刷新浏览器重新动态添加路由
141
        // }
4c4f7640   梁保满   路由表,路由前端文件
142
        // })
c1b532ad   梁保满   权限配置,路由基础设置
143
144
145
      }
    },
    getters: {
4c4f7640   梁保满   路由表,路由前端文件
146
      addRouters: state => state.addRouters,
c1b532ad   梁保满   权限配置,路由基础设置
147
      token: state => state.token,
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
148
      code: state => state.csCode,
4c4f7640   梁保满   路由表,路由前端文件
149
150
151
152
153
154
155
      info: state => state.info,
      routers: state => state.routers,
      logoShow: state => state.layoutStore.logoShow,
      isCollapse: state => state.layoutStore.isCollapse,
      uniquerouter: state => state.layoutStore.uniquerouter,
      tabnavBox: state => state.layoutStore.tabnavBox,
      rightNav: state => state.layoutStore.rightNav
c1b532ad   梁保满   权限配置,路由基础设置
156
157
    },
    modules: {
4c4f7640   梁保满   路由表,路由前端文件
158
      layoutStore
c1b532ad   梁保满   权限配置,路由基础设置
159
160
161
162
    }
  })
  
  export default store