index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. import tianpan from './modules/tianpan'
  4. import feedback from './feedback'
  5. import healthAlert from './health-alert'
  6. import healthScore from './health-score'
  7. Vue.use(Vuex)
  8. const store = new Vuex.Store({
  9. state: {
  10. token: '',
  11. userId: '',
  12. role: 'parent', // 用户的实际角色(数据库中的角色)
  13. currentRole: 'parent', // 当前视图模式(用于切换查看家长/孩子界面)
  14. familyId: '',
  15. nickname: '',
  16. currentChildId: '',
  17. currentMemberId: '', // 当前切换到的家庭成员ID(family_members表)
  18. currentMemberRole: '', // 当前切换到的家庭成员effectiveRole
  19. children: [],
  20. isSwitchedChild: false, // 家长切换到孩子状态的标记(已废弃,使用 currentView)
  21. isSwitchedMember: false, // 通过family_members切换的标记(已废弃,使用 currentView)
  22. currentView: 'self' // 当前视角: 'self' | 'member'
  23. },
  24. mutations: {
  25. setToken(state, token) {
  26. state.token = token
  27. uni.setStorageSync('token', token)
  28. },
  29. setUserInfo(state, userInfo) {
  30. state.userId = userInfo.userId
  31. state.role = userInfo.role
  32. state.currentRole = userInfo.role
  33. state.familyId = userInfo.familyId
  34. state.nickname = userInfo.nickname
  35. uni.setStorageSync('userId', userInfo.userId)
  36. uni.setStorageSync('role', userInfo.role)
  37. uni.setStorageSync('currentRole', userInfo.role)
  38. uni.setStorageSync('familyId', userInfo.familyId)
  39. state.isSwitchedChild = false
  40. state.currentView = 'self'
  41. uni.setStorageSync('isSwitchedChild', false)
  42. uni.setStorageSync('currentView', 'self')
  43. },
  44. setChildren(state, children) {
  45. state.children = children
  46. if (children.length > 0 && !state.currentChildId) {
  47. state.currentChildId = children[0].id
  48. }
  49. },
  50. setCurrentChild(state, childId) {
  51. state.currentChildId = childId
  52. uni.setStorageSync('currentChildId', childId)
  53. },
  54. // 切换到孩子模式(家长切换)
  55. switchToChild(state, childId) {
  56. state.currentRole = 'child'
  57. state.currentChildId = childId
  58. state.isSwitchedChild = true
  59. state.currentView = 'member'
  60. uni.setStorageSync('currentRole', 'child')
  61. uni.setStorageSync('currentChildId', childId)
  62. uni.setStorageSync('isSwitchedChild', true)
  63. uni.setStorageSync('currentView', 'member')
  64. },
  65. // 切换回家长模式
  66. switchBackToParent(state) {
  67. state.currentRole = 'parent'
  68. state.currentChildId = ''
  69. state.isSwitchedChild = false
  70. state.currentView = 'self'
  71. uni.setStorageSync('currentRole', 'parent')
  72. uni.removeStorageSync('currentChildId')
  73. uni.setStorageSync('isSwitchedChild', false)
  74. uni.setStorageSync('currentView', 'self')
  75. },
  76. // 通过family_members切换到某个成员
  77. switchToMember(state, payload) {
  78. state.currentRole = payload.effectiveRole
  79. state.currentMemberId = payload.memberId
  80. state.currentMemberRole = payload.effectiveRole
  81. state.isSwitchedMember = true
  82. state.currentView = 'member'
  83. if (payload.effectiveRole === 'child') {
  84. state.isSwitchedChild = true
  85. state.currentChildId = payload.memberId
  86. }
  87. uni.setStorageSync('currentRole', payload.effectiveRole)
  88. uni.setStorageSync('currentMemberId', payload.memberId)
  89. uni.setStorageSync('currentMemberRole', payload.effectiveRole)
  90. uni.setStorageSync('isSwitchedMember', true)
  91. uni.setStorageSync('currentView', 'member')
  92. if (payload.effectiveRole === 'child') {
  93. uni.setStorageSync('currentChildId', payload.memberId)
  94. uni.setStorageSync('isSwitchedChild', true)
  95. }
  96. },
  97. // 从family_members切换回自己
  98. switchBackFromMember(state) {
  99. var originalRole = uni.getStorageSync('role') || 'parent'
  100. state.currentRole = originalRole
  101. state.currentMemberId = ''
  102. state.currentMemberRole = ''
  103. state.isSwitchedMember = false
  104. state.isSwitchedChild = false
  105. state.currentChildId = ''
  106. state.currentView = 'self'
  107. uni.setStorageSync('currentRole', originalRole)
  108. uni.removeStorageSync('currentMemberId')
  109. uni.removeStorageSync('currentMemberRole')
  110. uni.setStorageSync('isSwitchedMember', false)
  111. uni.removeStorageSync('currentChildId')
  112. uni.setStorageSync('isSwitchedChild', false)
  113. uni.setStorageSync('currentView', 'self')
  114. },
  115. // 切换视图模式(不修改数据库中的实际角色)
  116. switchViewMode(state, role) {
  117. state.currentRole = role
  118. uni.setStorageSync('currentRole', role)
  119. },
  120. // 兼容旧的方法名
  121. switchRole(state, role) {
  122. state.currentRole = role
  123. uni.setStorageSync('currentRole', role)
  124. },
  125. logout(state) {
  126. state.token = ''
  127. state.userId = ''
  128. state.role = 'parent'
  129. state.currentRole = 'parent'
  130. state.familyId = ''
  131. state.nickname = ''
  132. state.currentChildId = ''
  133. state.currentMemberId = ''
  134. state.currentMemberRole = ''
  135. state.children = []
  136. state.isSwitchedChild = false
  137. state.isSwitchedMember = false
  138. state.isSwitchedTeacher = false
  139. state.currentView = 'self'
  140. uni.removeStorageSync('token')
  141. uni.removeStorageSync('currentRole')
  142. uni.removeStorageSync('isSwitchedChild')
  143. uni.removeStorageSync('isSwitchedTeacher')
  144. uni.removeStorageSync('userId')
  145. uni.removeStorageSync('role')
  146. uni.removeStorageSync('familyId')
  147. uni.removeStorageSync('openid')
  148. uni.removeStorageSync('userInfo')
  149. uni.removeStorageSync('nickname')
  150. uni.removeStorageSync('currentChildId')
  151. uni.removeStorageSync('currentMemberId')
  152. uni.removeStorageSync('currentMemberRole')
  153. uni.removeStorageSync('isSwitchedMember')
  154. uni.removeStorageSync('currentView')
  155. }
  156. },
  157. actions: {
  158. login({ commit }, userInfo) {
  159. commit('setToken', userInfo.token)
  160. commit('setUserInfo', userInfo)
  161. },
  162. updateChildren({ commit }, children) {
  163. commit('setChildren', children)
  164. },
  165. // 切换孩子前检查家庭中是否有孩子成员
  166. switchToChildWithCheck({ commit }, { childId, members }) {
  167. var hasChildren = members && members.some(function(m) {
  168. return m.effectiveRole === 'child'
  169. })
  170. if (!hasChildren) {
  171. uni.showToast({ title: '家庭中暂无孩子', icon: 'none' })
  172. return false
  173. }
  174. commit('switchToChild', childId)
  175. return true
  176. },
  177. // 切换到家庭成员前检查(仅 child 角色需要前置检查)
  178. switchToMemberWithCheck({ commit }, payload) {
  179. if (payload.effectiveRole === 'child') {
  180. var members = payload.members || []
  181. var hasChildren = members.some(function(m) {
  182. return m.effectiveRole === 'child'
  183. })
  184. if (!hasChildren) {
  185. uni.showToast({ title: '家庭中暂无孩子', icon: 'none' })
  186. return false
  187. }
  188. }
  189. commit('switchToMember', payload)
  190. return true
  191. }
  192. }
  193. ,
  194. modules: { tianpan, feedback, healthAlert, healthScore }
  195. })
  196. export default store