| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import Vue from 'vue'
- import Vuex from 'vuex'
- import tianpan from './modules/tianpan'
- 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, // 家长切换到孩子状态的标记(已废弃,使用 currentView)
- isSwitchedMember: false, // 通过family_members切换的标记(已废弃,使用 currentView)
- currentView: 'self' // 当前视角: 'self' | 'member'
- },
- 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
- state.currentView = 'self'
- uni.setStorageSync('isSwitchedChild', false)
- uni.setStorageSync('currentView', 'self')
- },
- 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
- state.currentView = 'member'
- uni.setStorageSync('currentRole', 'child')
- uni.setStorageSync('currentChildId', childId)
- uni.setStorageSync('isSwitchedChild', true)
- uni.setStorageSync('currentView', 'member')
- },
- // 切换回家长模式
- switchBackToParent(state) {
- state.currentRole = 'parent'
- state.currentChildId = ''
- state.isSwitchedChild = false
- state.currentView = 'self'
- uni.setStorageSync('currentRole', 'parent')
- uni.removeStorageSync('currentChildId')
- uni.setStorageSync('isSwitchedChild', false)
- uni.setStorageSync('currentView', 'self')
- },
- // 通过family_members切换到某个成员
- switchToMember(state, payload) {
- state.currentRole = payload.effectiveRole
- state.currentMemberId = payload.memberId
- state.currentMemberRole = payload.effectiveRole
- state.isSwitchedMember = true
- state.currentView = 'member'
- 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)
- uni.setStorageSync('currentView', 'member')
- 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 = ''
- state.currentView = 'self'
- uni.setStorageSync('currentRole', originalRole)
- uni.removeStorageSync('currentMemberId')
- uni.removeStorageSync('currentMemberRole')
- uni.setStorageSync('isSwitchedMember', false)
- uni.removeStorageSync('currentChildId')
- uni.setStorageSync('isSwitchedChild', false)
- uni.setStorageSync('currentView', 'self')
- },
- // 切换视图模式(不修改数据库中的实际角色)
- 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
- state.currentView = 'self'
- 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')
- uni.removeStorageSync('currentView')
- }
- },
- actions: {
- login({ commit }, userInfo) {
- commit('setToken', userInfo.token)
- commit('setUserInfo', userInfo)
- },
- updateChildren({ commit }, children) {
- commit('setChildren', children)
- }
- }
- ,
- modules: { tianpan }
- })
- export default store
|