index.js 6.0 KB

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