| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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: []
- },
- 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)
- },
- 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)
- },
- // 切换视图模式(不修改数据库中的实际角色)
- 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 = []
- uni.removeStorageSync('token')
- uni.removeStorageSync('currentRole')
- }
- },
- actions: {
- login({ commit }, userInfo) {
- commit('setToken', userInfo.token)
- commit('setUserInfo', userInfo)
- },
- updateChildren({ commit }, children) {
- commit('setChildren', children)
- }
- }
- })
- export default store
|