Blame view

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