import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { token: '', userId: '', role: 'parent', // 用户的实际角色(数据库中的角色) currentRole: 'parent', // 当前视图模式(用于切换查看家长/孩子界面) familyId: '', nickname: '', currentChildId: '', currentMemberId: '', // 当前切换到的家庭成员ID(family_members表) currentMemberRole: '', // 当前切换到的家庭成员effectiveRole children: [], isSwitchedChild: false, // 家长切换到孩子状态的标记 isSwitchedMember: false // 通过family_members切换的标记 }, mutations: { setToken(state, token) { state.token = token uni.setStorageSync('token', token) }, setUserInfo(state, userInfo) { state.userId = userInfo.userId state.role = userInfo.role state.currentRole = userInfo.role // 初始化当前角色为实际角色 state.familyId = userInfo.familyId state.nickname = userInfo.nickname // 保存到本地存储 uni.setStorageSync('userId', userInfo.userId) uni.setStorageSync('role', userInfo.role) uni.setStorageSync('currentRole', userInfo.role) uni.setStorageSync('familyId', userInfo.familyId) // 登录时清除切换标记 state.isSwitchedChild = false uni.setStorageSync('isSwitchedChild', false) }, setChildren(state, children) { state.children = children if (children.length > 0 && !state.currentChildId) { state.currentChildId = children[0].id } }, setCurrentChild(state, childId) { state.currentChildId = childId uni.setStorageSync('currentChildId', childId) }, // 切换到孩子模式(家长切换) switchToChild(state, childId) { state.currentRole = 'child' state.currentChildId = childId state.isSwitchedChild = true uni.setStorageSync('currentRole', 'child') uni.setStorageSync('currentChildId', childId) uni.setStorageSync('isSwitchedChild', true) }, // 切换回家长模式 switchBackToParent(state) { state.currentRole = 'parent' state.currentChildId = '' state.isSwitchedChild = false uni.setStorageSync('currentRole', 'parent') uni.removeStorageSync('currentChildId') uni.setStorageSync('isSwitchedChild', false) }, // 通过family_members切换到某个成员 switchToMember(state, payload) { state.currentRole = payload.effectiveRole state.currentMemberId = payload.memberId state.currentMemberRole = payload.effectiveRole state.isSwitchedMember = true if (payload.effectiveRole === 'child') { state.isSwitchedChild = true state.currentChildId = payload.memberId } uni.setStorageSync('currentRole', payload.effectiveRole) uni.setStorageSync('currentMemberId', payload.memberId) uni.setStorageSync('currentMemberRole', payload.effectiveRole) uni.setStorageSync('isSwitchedMember', true) if (payload.effectiveRole === 'child') { uni.setStorageSync('currentChildId', payload.memberId) uni.setStorageSync('isSwitchedChild', true) } }, // 从family_members切换回自己 switchBackFromMember(state) { var originalRole = uni.getStorageSync('role') || 'parent' state.currentRole = originalRole state.currentMemberId = '' state.currentMemberRole = '' state.isSwitchedMember = false state.isSwitchedChild = false state.currentChildId = '' uni.setStorageSync('currentRole', originalRole) uni.removeStorageSync('currentMemberId') uni.removeStorageSync('currentMemberRole') uni.setStorageSync('isSwitchedMember', false) uni.removeStorageSync('currentChildId') uni.setStorageSync('isSwitchedChild', false) }, // 切换视图模式(不修改数据库中的实际角色) switchViewMode(state, role) { state.currentRole = role uni.setStorageSync('currentRole', role) }, // 兼容旧的方法名 switchRole(state, role) { state.currentRole = role uni.setStorageSync('currentRole', role) }, logout(state) { state.token = '' state.userId = '' state.role = 'parent' state.currentRole = 'parent' state.familyId = '' state.nickname = '' state.currentChildId = '' state.currentMemberId = '' state.currentMemberRole = '' state.children = [] state.isSwitchedChild = false state.isSwitchedMember = false state.isSwitchedTeacher = false // 清除全部登录态存储 uni.removeStorageSync('token') uni.removeStorageSync('currentRole') uni.removeStorageSync('isSwitchedChild') uni.removeStorageSync('isSwitchedTeacher') uni.removeStorageSync('userId') uni.removeStorageSync('role') uni.removeStorageSync('familyId') uni.removeStorageSync('openid') uni.removeStorageSync('userInfo') uni.removeStorageSync('nickname') uni.removeStorageSync('currentChildId') uni.removeStorageSync('currentMemberId') uni.removeStorageSync('currentMemberRole') uni.removeStorageSync('isSwitchedMember') } }, actions: { login({ commit }, userInfo) { commit('setToken', userInfo.token) commit('setUserInfo', userInfo) }, updateChildren({ commit }, children) { commit('setChildren', children) } } }) export default store