/** * 用户状态管理 * 管理用户登录状态和信息 */ import { storage } from '../utils/index.js' import api from '../api/index.js' // 状态 const state = { token: storage.get('token') || null, userInfo: storage.get('userInfo') || null, tenantInfo: storage.get('tenantInfo') || null, menus: storage.get('menus') || [] } // 获取器 const getters = { /** * 是否已登录 */ isLoggedIn: (state) => { return !!state.token && !!state.userInfo }, /** * 获取用户信息 */ userInfo: (state) => { return state.userInfo }, /** * 获取租户信息 */ tenantInfo: (state) => { return state.tenantInfo }, /** * 获取菜单列表 */ menus: (state) => { return state.menus }, /** * 获取Token */ token: (state) => { return state.token }, /** * 是否超级管理员 */ isSuperAdmin: (state) => { return state.userInfo?.userType === 'super_admin' } } // 修改器 const mutations = { /** * 设置Token */ SET_TOKEN(state, token) { state.token = token storage.set('token', token) }, /** * 设置用户信息 */ SET_USER_INFO(state, userInfo) { state.userInfo = userInfo storage.set('userInfo', userInfo) }, /** * 设置租户信息 */ SET_TENANT_INFO(state, tenantInfo) { state.tenantInfo = tenantInfo storage.set('tenantInfo', tenantInfo) }, /** * 设置菜单 */ SET_MENUS(state, menus) { state.menus = menus storage.set('menus', menus) }, /** * 清除所有状态 */ CLEAR_ALL(state) { state.token = null state.userInfo = null state.tenantInfo = null state.menus = [] storage.remove('token') storage.remove('userInfo') storage.remove('tenantInfo') storage.remove('menus') } } // 动作 const actions = { /** * 用户登录 * @param {Object} context * @param {Object} credentials - 登录凭证 */ async login({ commit }, credentials) { try { const res = await api.auth.login(credentials) const { token, userInfo, tenant, menus } = res.data // 保存登录信息 commit('SET_TOKEN', token) commit('SET_USER_INFO', userInfo) commit('SET_TENANT_INFO', tenant) commit('SET_MENUS', menus) return res } catch (error) { throw error } }, /** * 用户登出 * @param {Object} context */ async logout({ commit }) { try { // 调用登出接口 await api.auth.logout() } catch (error) { console.error('登出接口调用失败:', error) } finally { // 清除本地状态 commit('CLEAR_ALL') } }, /** * 获取当前用户信息 * @param {Object} context */ async getCurrentUser({ commit }) { try { const res = await api.auth.getCurrentUser() commit('SET_USER_INFO', res.data) return res } catch (error) { throw error } }, /** * 更新用户信息 * @param {Object} context * @param {Object} userInfo - 用户信息 */ updateUserInfo({ commit }, userInfo) { commit('SET_USER_INFO', userInfo) } } // 创建 Store(简单实现,不使用 Vuex) const userStore = { state, getters, mutations, actions } // 创建响应式状态 const createReactiveState = () => { const reactiveState = { ...state } return { ...userStore, state: reactiveState, // 便捷方法:登录 login(credentials) { return this.actions.login({ commit: (mutation, payload) => { mutations[mutation](reactiveState, payload) }}, credentials) }, // 便捷方法:登出 logout() { return this.actions.logout({ commit: (mutation, payload) => { mutations[mutation](reactiveState, payload) }}) }, // 便捷方法:获取当前用户 getCurrentUser() { return this.actions.getCurrentUser({ commit: (mutation, payload) => { mutations[mutation](reactiveState, payload) }}) }, // 便捷方法:清除状态 clearAll() { mutations.CLEAR_ALL(reactiveState) }, // 便捷方法:检查登录状态 checkLoginStatus() { return !!reactiveState.token && !!reactiveState.userInfo } } } // 导出单例 export const userStoreInstance = createReactiveState() export default userStoreInstance