index.js 5.9 KB

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