Blame view

src/store/index.js 7.16 KB
42056450   阿宝   权限修改
1
2
3
4
5
  import Vue from "vue";
  import Vuex from "vuex";
  import Cookies from "js-cookie";
  import layoutStore from "./modules/layout/index";
  import { defaultRouter } from "@/router/index";
13b58a42   梁保满   备题组卷部分前端页面基本完成
6
  import { encryptLoginPassword } from "@/utils";
aeac66d4   阿宝   飞书测试bug
7
  import { Message } from "element-ui";
4c4f7640   梁保满   路由表,路由前端文件
8
  
42056450   阿宝   权限修改
9
10
  import request from "@/api/index";
  import router from "@/router/index";
3617eaad   梁保满   长水账号设置
11
  import { addrouters, addRoutersAdmin, addroutersPersonal, csAddrouters } from "@/router/index";
42056450   阿宝   权限修改
12
  Vue.use(Vuex);
c1b532ad   梁保满   权限配置,路由基础设置
13
14
15
  
  const store = new Vuex.Store({
    state: {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
16
17
      token: "",
      csCode: localStorage.getItem("csCode") || "",
bbc51d4b   梁保满   组卷添加修改分数,打开新开页面跳转...
18
19
      info: localStorage.getItem("info")
        ? JSON.parse(localStorage.getItem("info"))
42056450   阿宝   权限修改
20
21
22
        : "", // 每次刷新都要通过token请求个人信息来筛选动态路由
      routers: [], //左侧菜单
      addRouters:
bbc51d4b   梁保满   组卷添加修改分数,打开新开页面跳转...
23
        localStorage.getItem("addRouters") &&
3617eaad   梁保满   长水账号设置
24
          localStorage.getItem("addRouters") != "undefined"
bbc51d4b   梁保满   组卷添加修改分数,打开新开页面跳转...
25
          ? JSON.parse(localStorage.getItem("addRouters"))
42056450   阿宝   权限修改
26
27
          : [], //动态路由
      tokenSources: new Map(), //正在请求接口(切换取消请求)
7812e986   梁保满   班主任查看报表添加额外信息
28
      classes: localStorage.getItem("classes") || "", //班主任选择班级查询报表(用于教学班学生筛选)
ce278878   梁保满   2-2 bugfix
29
      refreshTestList: false, //重新计分刷新列表
c1b532ad   梁保满   权限配置,路由基础设置
30
31
    },
    mutations: {
4c4f7640   梁保满   路由表,路由前端文件
32
      setToken(state, token) {
42056450   阿宝   权限修改
33
        state.token = token;
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
34
35
36
      },
      setCode(state, code) {
        state.csCode = code;
42056450   阿宝   权限修改
37
        localStorage.setItem("csCode", code);
4c4f7640   梁保满   路由表,路由前端文件
38
39
      },
      setInfo(state, data) {
42056450   阿宝   权限修改
40
        state.info = { ...data };
bbc51d4b   梁保满   组卷添加修改分数,打开新开页面跳转...
41
        localStorage.setItem("info", JSON.stringify(data));
4c4f7640   梁保满   路由表,路由前端文件
42
43
      },
      setRouters: (state, routers) => {
77ebf04d   梁保满   个人版
44
        let addrouterList = []
3617eaad   梁保满   长水账号设置
45
        if (state.info.showRole == 'ROLE_PERSONAL') {
77ebf04d   梁保满   个人版
46
          addrouterList = [...addroutersPersonal]
3617eaad   梁保满   长水账号设置
47
        } else if (state.info.showRole == 'ROLE_PINGTAI') {
77ebf04d   梁保满   个人版
48
          addrouterList = [...addRoutersAdmin]
3617eaad   梁保满   长水账号设置
49
50
51
52
53
54
55
        } else {
          if (state.csCode) {
            addrouterList = [...csAddrouters]
          } else {
            addrouterList = [...addrouters]
          }
  
77ebf04d   梁保满   个人版
56
57
        }
        let aRouters = addrouterList.filter((item) => {
42056450   阿宝   权限修改
58
59
60
          let path = item.children[0]?.path.replace("/", "");
          return routers?.includes(path);
        });
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
61
  
42056450   阿宝   权限修改
62
63
        state.addRouters = aRouters; // 保存动态路由用来addRouter
        state.routers = defaultRouter.concat(aRouters); // 所有有权限的路由表,用来生成菜单列表
bbc51d4b   梁保满   组卷添加修改分数,打开新开页面跳转...
64
        localStorage.setItem("addRouters", JSON.stringify(routers));
4c4f7640   梁保满   路由表,路由前端文件
65
      },
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
66
67
      setTokenSources(state, data) {
        if (data instanceof Array) {
42056450   阿宝   权限修改
68
          state.tokenSources.set(data[0], data[1]);
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
69
        } else {
42056450   阿宝   权限修改
70
          state.tokenSources = new Map();
65f592b6   梁保满   答题卡列表页
71
72
        }
      },
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
73
      delTokenSources(state, data) {
42056450   阿宝   权限修改
74
        state.tokenSources.delete(data);
65f592b6   梁保满   答题卡列表页
75
      },
7812e986   梁保满   班主任查看报表添加额外信息
76
77
78
      setClasses(state, data) {
        state.classes = data;
        localStorage.setItem("classes", data);
ce278878   梁保满   2-2 bugfix
79
80
81
      },
      setRefreshTestList(state, data) {
        state.refreshTestList = data;
7812e986   梁保满   班主任查看报表添加额外信息
82
      }
c1b532ad   梁保满   权限配置,路由基础设置
83
84
    },
    actions: {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
85
      Login({ state, commit }, params) {
42056450   阿宝   权限修改
86
        let loginForm = {};
13b58a42   梁保满   备题组卷部分前端页面基本完成
87
88
        loginForm.username = params.username;
        loginForm.password = encryptLoginPassword(params.password);
42056450   阿宝   权限修改
89
90
91
92
93
94
        request
          .login({ ...loginForm })
          .then((res) => {
            let response = res;
            if (response.status == 0) {
              const userInfo = { ...response.data };
aeac66d4   阿宝   飞书测试bug
95
96
              if (userInfo.permissions && userInfo.permissions.length) {
                userInfo.showRoleName = response.data.permissions[0]?.roleName;
21dfdeae   梁保满   平台管理员
97
                userInfo.showRole = response.data.permissions[0]?.role;
aeac66d4   阿宝   飞书测试bug
98
99
                commit("setToken", "isLogin");
                commit("setInfo", { ...userInfo });
21dfdeae   梁保满   平台管理员
100
101
102
                commit("setRouters", [
                  ...userInfo.permissions[0]?.authorityRouter,
                ]);
aeac66d4   阿宝   飞书测试bug
103
104
105
106
107
108
109
110
111
                state.addRouters.forEach((res) => {
                  router.addRoute(res);
                });
                router.addRoute({
                  path: "*",
                  redirect: "/404",
                  hidden: true,
                  children: [],
                });
aeac66d4   阿宝   飞书测试bug
112
113
114
115
116
                if (params.url) {
                  window.location.href = params.url;
                } else {
                  router.push({ path: "/" });
                }
42056450   阿宝   权限修改
117
              } else {
aeac66d4   阿宝   飞书测试bug
118
119
120
121
122
                Message({
                  message: "该账号暂无权限,请联系管理员~",
                  type: "error",
                  duration: 3 * 1000,
                });
42056450   阿宝   权限修改
123
              }
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
124
            }
42056450   阿宝   权限修改
125
          })
54857fa3   梁保满   下载模板跨域,集团管理员选择角色范...
126
127
128
129
130
131
132
          .catch((err) => {
            Message({
              message: err,
              type: "error",
              duration: 3 * 1000,
            });
          });
29f8fb90   梁保满   刷新浏览器重新动态添加路由
133
      },
b21d90ef   梁保满   长水登录
134
135
      CSLogin({ state, commit }, code) {
        request
3617eaad   梁保满   长水账号设置
136
          .ssoLogin({ code: code })
b21d90ef   梁保满   长水登录
137
138
139
140
141
142
          .then((res) => {
            let response = res;
            if (response.status == 0) {
              const userInfo = { ...response.data };
              if (userInfo.permissions && userInfo.permissions.length) {
                userInfo.showRoleName = response.data.permissions[0]?.roleName;
77ebf04d   梁保满   个人版
143
                userInfo.showRole = response.data.permissions[0]?.role;
b21d90ef   梁保满   长水登录
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
                commit("setToken", "isLogin");
                commit("setCode", code);
                commit("setInfo", { ...userInfo });
                commit("setRouters", [
                  ...userInfo.permissions[0]?.authorityRouter,
                ]);
                state.addRouters.forEach((res) => {
                  router.addRoute(res);
                });
                router.addRoute({
                  path: "*",
                  redirect: "/404",
                  hidden: true,
                  children: [],
                });
3617eaad   梁保满   长水账号设置
159
160
                router.push({ path: "/" });
  
b21d90ef   梁保满   长水登录
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
              } else {
                Message({
                  message: "该账号暂无权限,请联系管理员~",
                  type: "error",
                  duration: 3 * 1000,
                });
              }
            } else {
              Message({
                message: response.info,
                type: "error",
                duration: 3 * 1000,
              });
            }
          })
3617eaad   梁保满   长水账号设置
176
          .catch(() => { });
b21d90ef   梁保满   长水登录
177
      },
42056450   阿宝   权限修改
178
179
180
181
182
183
      permissions({ state, commit }, role) {
        commit("setToken", "isLogin");
        let userInfo = state.info;
        let authorityRouterObj = userInfo.permissions.filter((item) => {
          return item.role == role;
        });
aeac66d4   阿宝   飞书测试bug
184
        userInfo.showRoleName = authorityRouterObj[0]?.roleName;
77ebf04d   梁保满   个人版
185
        userInfo.showRole = authorityRouterObj[0]?.role;
bb778c90   阿宝   设备状态
186
        commit("setInfo", userInfo);
42056450   阿宝   权限修改
187
        commit("setRouters", [...authorityRouterObj[0]?.authorityRouter]);
13b58a42   梁保满   备题组卷部分前端页面基本完成
188
        state.addRouters.forEach((res) => {
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
189
          router.addRoute(res);
29f8fb90   梁保满   刷新浏览器重新动态添加路由
190
        });
b769660c   梁保满   备课组题细节调整,随堂问列表页面开发完成
191
192
193
194
        router.addRoute({
          path: "*",
          redirect: "/404",
          hidden: true,
42056450   阿宝   权限修改
195
196
197
          children: [],
        });
        router.push({ path: "/" });
11a4e518   梁保满   背题组卷修改答案设置,即使测随堂问...
198
        window.location.reload()
42056450   阿宝   权限修改
199
      },
c1b532ad   梁保满   权限配置,路由基础设置
200
201
    },
    getters: {
42056450   阿宝   权限修改
202
203
      addRouters: (state) => state.addRouters,
      token: (state) => state.token,
7812e986   梁保满   班主任查看报表添加额外信息
204
      csCode: (state) => state.csCode,
42056450   阿宝   权限修改
205
206
      info: (state) => state.info,
      routers: (state) => state.routers,
7812e986   梁保满   班主任查看报表添加额外信息
207
      classes: (state) => state.classes,
42056450   阿宝   权限修改
208
209
210
211
212
      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,
ce278878   梁保满   2-2 bugfix
213
      refreshTestList: (state) => state.refreshTestList,
c1b532ad   梁保满   权限配置,路由基础设置
214
215
    },
    modules: {
42056450   阿宝   权限修改
216
217
218
      layoutStore,
    },
  });
c1b532ad   梁保满   权限配置,路由基础设置
219
  
42056450   阿宝   权限修改
220
  export default store;