105 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-04-18 13:44:38 +08:00
const InitUserInfo = {
roles: [],
};
// 定义的state初始值
const state = {
2024-05-17 16:49:23 +08:00
token: localStorage.getItem('associationToken') || '', // 默认token不走权限
2024-04-18 13:44:38 +08:00
userInfo: InitUserInfo,
2024-06-06 18:00:47 +08:00
association: localStorage.getItem('associationInfo') || '',
2024-05-11 18:14:00 +08:00
//apiUrl:'http://192.168.3.130',
2024-06-06 18:00:47 +08:00
apiUrl: 'https://ysx.0rui.cn'
2024-04-18 13:44:38 +08:00
};
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) {
2024-06-06 18:00:47 +08:00
localStorage.setItem('associationInfo', JSON.stringify(Association));
2024-04-18 13:44:38 +08:00
state.association = Association;
},
removeAssociation(state) {
localStorage.removeItem('associationInfo');
state.association = '';
},
};
const getters = {
token: (state) => state.token,
roles: (state) => state.userInfo?.roles,
};
const actions = {
2024-06-06 18:00:47 +08:00
async login({commit}, userInfo) {
2024-04-18 13:44:38 +08:00
const mockLogin = async (userInfo) => {
// 登录请求流程
console.log(userInfo);
2024-06-06 18:00:47 +08:00
commit('setAssociation', userInfo);
return {
2024-04-18 13:44:38 +08:00
code: 200,
}
};
const res = await mockLogin(userInfo);
if (res.code === 200) {
2024-06-06 18:00:47 +08:00
console.log('token', userInfo.token);
commit('setToken', userInfo.token);
2024-04-18 13:44:38 +08:00
} else {
return res;
}
},
2024-06-06 18:00:47 +08:00
async getUserInfo({commit, state}) {
2024-04-18 13:44:38 +08:00
const mockRemoteUserInfo = async (token) => {
2024-06-06 18:00:47 +08:00
if (typeof (token) == 'object') {
var key = token;
} else {
var key = JSON.parse(token);
}
if (key.group == 1) {
2024-04-18 13:44:38 +08:00
return {
name: 'td_main',
roles: ['ALL_ROUTERS'],
};
2024-06-06 18:00:47 +08:00
} else {
return {
name: 'td_dev',
roles: ['newsIndex', 'login', 'activityIndex', 'userEditShen', 'userIndex', 'userInfo', 'userEdit'],
};
}
2024-05-17 16:49:23 +08:00
// if (token === 'main_token') {
// return {
// name: 'td_main',
// roles: ['ALL_ROUTERS'],
// };
// }
2024-06-06 18:00:47 +08:00
2024-04-18 13:44:38 +08:00
};
2024-06-06 18:00:47 +08:00
const res = await mockRemoteUserInfo(state.association);
2024-04-18 13:44:38 +08:00
commit('setUserInfo', res);
},
2024-06-06 18:00:47 +08:00
async logout({commit}) {
2024-04-18 13:44:38 +08:00
commit('removeToken');
2024-06-06 18:00:47 +08:00
commit('setUserInfo', []);
2024-04-18 13:44:38 +08:00
},
};
export default {
namespaced: true,
state,
mutations,
actions,
getters,
};