const InitUserInfo = { roles: [], }; // 定义的state初始值 const state = { token: localStorage.getItem('associationToken') || '', // 默认token不走权限 userInfo: InitUserInfo, association: localStorage.getItem('associationInfo') || '', //apiUrl:'http://192.168.3.130', apiUrl: 'https://ysx.0rui.cn' }; const mutations = { setToken(state, token) { localStorage.setItem('associationToken', token); state.token = token; }, removeToken(state) { localStorage.removeItem('associationToken'); state.token = ''; }, setUserInfo(state, userInfo) { state.userInfo = userInfo; }, setAssociation(state, Association) { localStorage.setItem('associationInfo', JSON.stringify(Association)); state.association = Association; }, removeAssociation(state) { localStorage.removeItem('associationInfo'); state.association = ''; }, }; const getters = { token: (state) => state.token, roles: (state) => state.userInfo?.roles, }; const actions = { async login({commit}, userInfo) { const mockLogin = async (userInfo) => { // 登录请求流程 console.log(userInfo); commit('setAssociation', userInfo); return { code: 200, } }; const res = await mockLogin(userInfo); if (res.code === 200) { commit('setToken', userInfo.token); } else { throw res; } }, async getUserInfo({commit, state}) { const mockRemoteUserInfo = async (token) => { if (typeof (token) == 'object') { var key = token; } else { var key = JSON.parse(token); } if (key.group == 1) { return { name: 'td_main', roles: ['ALL_ROUTERS'], }; } else { return { name: 'td_dev', roles: ['DashboardBase','newsIndex', 'login', 'activityIndex', 'userEditShen', 'userIndex', 'userInfo', 'userEdit'], }; } }; const res = await mockRemoteUserInfo(state.association); commit('setUserInfo', res); }, async logout({commit}) { commit('removeToken'); commit('setUserInfo', []); }, }; export default { namespaced: true, state, mutations, actions, getters, };