| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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: '',
- children: [],
- isSwitchedChild: false // 家长切换到孩子状态的标记
- },
- 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)
- },
- // 切换视图模式(不修改数据库中的实际角色)
- 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.children = []
- state.isSwitchedChild = 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')
- }
- },
- actions: {
- login({ commit }, userInfo) {
- commit('setToken', userInfo.token)
- commit('setUserInfo', userInfo)
- },
- updateChildren({ commit }, children) {
- commit('setChildren', children)
- }
- }
- })
- export default store
|