| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- import Vue from 'vue'
- import Vuex from 'vuex'
- import tianpan from './modules/tianpan'
- import feedback from './feedback'
- import healthAlert from './health-alert'
- import healthScore from './health-score'
- import { switchBackVerify } from '../utils/api.js'
- 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'
- },
- getters: {
- hasFamily: function(state) {
- return state.familyId !== '' && state.familyId !== null && state.familyId !== 0
- }
- },
- 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)
- 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, memberId) {
- state.currentChildId = memberId
- uni.setStorageSync('currentChildId', memberId)
- },
- // 切换到孩子模式(家长切换)
- switchToChild(state, memberId) {
- state.currentRole = 'child'
- state.currentChildId = memberId
- state.isSwitchedChild = true
- state.currentView = 'member'
- uni.setStorageSync('currentRole', 'child')
- uni.setStorageSync('currentChildId', memberId)
- 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切换到某个成员(member 语义统一入口)
- // 注:effectiveRole 为 'child' 时同步设置 currentChildId/isSwitchedChild 兼容旧页面
- 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('openid')
- uni.removeStorageSync('userInfo')
- uni.removeStorageSync('nickname')
- uni.removeStorageSync('currentChildId')
- uni.removeStorageSync('currentMemberId')
- uni.removeStorageSync('currentMemberRole')
- uni.removeStorageSync('isSwitchedMember')
- uni.removeStorageSync('currentView')
- uni.removeStorageSync('familyId')
- },
- markNoFamily: function(state) {
- if (state.familyId !== '' && state.familyId !== null && state.familyId !== 0) {
- console.warn('[STORE] 后端返回 5001 但本地 familyId 非空,已同步本地为无家庭态', state.familyId)
- }
- state.familyId = ''
- uni.removeStorageSync('familyId')
- },
- setFamilyId: function(state, familyId) {
- state.familyId = familyId
- // 持久化供冷启动恢复(hasFamily getter 依赖 state.familyId)
- uni.setStorageSync('familyId', familyId || '')
- }
- },
- actions: {
- login({ commit }, userInfo) {
- commit('setToken', userInfo.token)
- commit('setUserInfo', userInfo)
- },
- updateChildren({ commit }, children) {
- commit('setChildren', children)
- },
- // 兼容旧 action:切换成员前检查(effectiveRole 年龄语义保留,用于过滤未成年成员)
- switchToChildWithCheck({ commit }, { memberId, members }) {
- var hasChildren = members && members.some(function(m) {
- return m.effectiveRole === 'child'
- })
- if (!hasChildren) {
- uni.showToast({ title: '家庭中暂无可切换的未成年成员', icon: 'none' })
- return false
- }
- commit('switchToChild', memberId)
- return true
- },
- // 切换到家庭成员前检查(isSwitchable 由后端统一计算:非本人即可切换,含可登录成员)
- switchToMemberWithCheck({ commit }, payload) {
- var member = payload.member || {}
- if (member.isSwitchable === false) {
- uni.showToast({ title: '该成员暂不可切换', icon: 'none' })
- return false
- }
- commit('switchToMember', payload)
- return true
- },
- // 带密码校验退出成员切换(返回原用户身份)
- async switchBackWithPassword({ commit }, password) {
- if (!password) {
- uni.showToast({ title: '请输入原用户密码', icon: 'none' })
- return false
- }
- try {
- const res = await switchBackVerify(password)
- if (res.code === 200) {
- commit('switchBackFromMember')
- return true
- }
- uni.showToast({ title: res.message || '密码错误', icon: 'none' })
- return false
- } catch (e) {
- uni.showToast({ title: '校验失败', icon: 'none' })
- return false
- }
- }
- }
- ,
- modules: { tianpan, feedback, healthAlert, healthScore }
- })
- export default store
|