Blame view

src/store/index.js 3.63 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
7
  import { encryptLoginPassword } from "@/utils";
  import { b64DecodeUnicode } from "@/utils";
4c4f7640   梁保满   路由表,路由前端文件
8
9
10
  
  import request from "@/api/index"
  import router from "@/router/index"
13b58a42   梁保满   备题组卷部分前端页面基本完成
11
  import {addrouters} from "@/router/index"
c1b532ad   梁保满   权限配置,路由基础设置
12
13
14
15
  Vue.use(Vuex)
  
  const store = new Vuex.Store({
    state: {
4c4f7640   梁保满   路由表,路由前端文件
16
17
18
19
      token: Cookies.get("token"),
      info: "", // 每次刷新都要通过token请求个人信息来筛选动态路由
      routers: [],
      addRouters: [],
65f592b6   梁保满   答题卡列表页
20
      tokenSources:new Map(),
c1b532ad   梁保满   权限配置,路由基础设置
21
22
    },
    mutations: {
4c4f7640   梁保满   路由表,路由前端文件
23
      setToken(state, token) {
c1b532ad   梁保满   权限配置,路由基础设置
24
25
        state.token = token
        Cookies.set("token", token, { expires: 1 / 24 })
4c4f7640   梁保满   路由表,路由前端文件
26
27
28
29
30
31
32
33
      },
      setInfo(state, data) {
        state.info = {
          role: data.role,
          permissions: data.permissions,
          name: data.name,
          authorityRouter: data.authorityRouter,
          avatar: data.avatar ? data.avatar : "",
13b58a42   梁保满   备题组卷部分前端页面基本完成
34
          uid: data.uid
4c4f7640   梁保满   路由表,路由前端文件
35
36
37
38
39
40
41
        }
        localStorage.setItem("info", JSON.stringify(store.getters.info))
      },
      setRouters: (state, routers) => {
        state.addRouters = routers // 保存动态路由用来addRouter
        state.routers = defaultRouter.concat(routers) // 所有有权限的路由表,用来生成菜单列表
      },
65f592b6   梁保满   答题卡列表页
42
43
44
45
46
47
48
49
50
51
      setTokenSources(state,data){
        if(data instanceof Array){
          state.tokenSources.set(data[0], data[1])
        }else{
          state.tokenSources = new Map()
        }
      },
      delTokenSources(state,data){
        state.tokenSources.delete(data)
      },
c1b532ad   梁保满   权限配置,路由基础设置
52
53
    },
    actions: {
13b58a42   梁保满   备题组卷部分前端页面基本完成
54
55
56
57
58
      Login({ state,commit }, params) {
        let loginForm={}
        loginForm.username = params.username;
        loginForm.password = encryptLoginPassword(params.password);
        // request.fetchLogin(loginForm).then(res => {
29f8fb90   梁保满   刷新浏览器重新动态添加路由
59
        // if (res.status == 200) {
13b58a42   梁保满   备题组卷部分前端页面基本完成
60
          // const dataJSON = JSON.parse(b64DecodeUnicode(res.data));
29f8fb90   梁保满   刷新浏览器重新动态添加路由
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        commit("setToken", "xxxx")
        commit("setInfo", {
          permissions: [
            {
              role: "superAdmin",
              roleName: "超级管理员",
            },
          ],
          name: "张老师",
          // avatar: data.avatar ? data.avatar : "",
          // uid: data.id,
          // authorityRouter:[],
        });
        commit("setRouters", addrouters)
13b58a42   梁保满   备题组卷部分前端页面基本完成
75
76
        state.addRouters.forEach((res) => {
          router.addRoute(res);
29f8fb90   梁保满   刷新浏览器重新动态添加路由
77
        });
13b58a42   梁保满   备题组卷部分前端页面基本完成
78
79
80
81
82
83
        console.log()
        if (params.url) {
          window.location.href = params.url;
        } else {
          router.push({ path: "/" })
        }
29f8fb90   梁保满   刷新浏览器重新动态添加路由
84
85
86
        // }
        // })
      },
13b58a42   梁保满   备题组卷部分前端页面基本完成
87
      permissions({ state,commit }, that) {
29f8fb90   梁保满   刷新浏览器重新动态添加路由
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        // 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,
          // authorityRouter:[],
        });
        commit("setRouters", addrouters)
13b58a42   梁保满   备题组卷部分前端页面基本完成
104
        state.addRouters.forEach((res) => {
29f8fb90   梁保满   刷新浏览器重新动态添加路由
105
106
107
108
          that.$router.addRoute(res);
        });
        that.$router.push({ path: "/" })
        // }
4c4f7640   梁保满   路由表,路由前端文件
109
        // })
c1b532ad   梁保满   权限配置,路由基础设置
110
111
112
      }
    },
    getters: {
4c4f7640   梁保满   路由表,路由前端文件
113
      addRouters: state => state.addRouters,
c1b532ad   梁保满   权限配置,路由基础设置
114
      token: state => state.token,
4c4f7640   梁保满   路由表,路由前端文件
115
116
117
118
119
120
121
      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   梁保满   权限配置,路由基础设置
122
123
    },
    modules: {
4c4f7640   梁保满   路由表,路由前端文件
124
      layoutStore
c1b532ad   梁保满   权限配置,路由基础设置
125
126
127
128
    }
  })
  
  export default store