index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. getters: {
  25. hasFamily: function(state) {
  26. return state.familyId !== '' && state.familyId !== null && state.familyId !== 0
  27. }
  28. },
  29. mutations: {
  30. setToken(state, token) {
  31. state.token = token
  32. uni.setStorageSync('token', token)
  33. },
  34. setUserInfo(state, userInfo) {
  35. state.userId = userInfo.userId
  36. state.role = userInfo.role
  37. state.currentRole = userInfo.role
  38. state.familyId = userInfo.familyId
  39. state.nickname = userInfo.nickname
  40. uni.setStorageSync('userId', userInfo.userId)
  41. uni.setStorageSync('role', userInfo.role)
  42. uni.setStorageSync('currentRole', userInfo.role)
  43. state.isSwitchedChild = false
  44. state.currentView = 'self'
  45. uni.setStorageSync('isSwitchedChild', false)
  46. uni.setStorageSync('currentView', 'self')
  47. },
  48. setChildren(state, children) {
  49. state.children = children
  50. if (children.length > 0 && !state.currentChildId) {
  51. state.currentChildId = children[0].id
  52. }
  53. },
  54. setCurrentChild(state, memberId) {
  55. state.currentChildId = memberId
  56. uni.setStorageSync('currentChildId', memberId)
  57. },
  58. // 切换到孩子模式(家长切换)
  59. switchToChild(state, memberId) {
  60. state.currentRole = 'child'
  61. state.currentChildId = memberId
  62. state.isSwitchedChild = true
  63. state.currentView = 'member'
  64. uni.setStorageSync('currentRole', 'child')
  65. uni.setStorageSync('currentChildId', memberId)
  66. uni.setStorageSync('isSwitchedChild', true)
  67. uni.setStorageSync('currentView', 'member')
  68. },
  69. // 切换回家长模式
  70. switchBackToParent(state) {
  71. state.currentRole = 'parent'
  72. state.currentChildId = ''
  73. state.isSwitchedChild = false
  74. state.currentView = 'self'
  75. uni.setStorageSync('currentRole', 'parent')
  76. uni.removeStorageSync('currentChildId')
  77. uni.setStorageSync('isSwitchedChild', false)
  78. uni.setStorageSync('currentView', 'self')
  79. },
  80. // 通过family_members切换到某个成员
  81. switchToMember(state, payload) {
  82. state.currentRole = payload.effectiveRole
  83. state.currentMemberId = payload.memberId
  84. state.currentMemberRole = payload.effectiveRole
  85. state.isSwitchedMember = true
  86. state.currentView = 'member'
  87. if (payload.effectiveRole === 'child') {
  88. state.isSwitchedChild = true
  89. state.currentChildId = payload.memberId
  90. }
  91. uni.setStorageSync('currentRole', payload.effectiveRole)
  92. uni.setStorageSync('currentMemberId', payload.memberId)
  93. uni.setStorageSync('currentMemberRole', payload.effectiveRole)
  94. uni.setStorageSync('isSwitchedMember', true)
  95. uni.setStorageSync('currentView', 'member')
  96. if (payload.effectiveRole === 'child') {
  97. uni.setStorageSync('currentChildId', payload.memberId)
  98. uni.setStorageSync('isSwitchedChild', true)
  99. }
  100. },
  101. // 从family_members切换回自己
  102. switchBackFromMember(state) {
  103. var originalRole = uni.getStorageSync('role') || 'parent'
  104. state.currentRole = originalRole
  105. state.currentMemberId = ''
  106. state.currentMemberRole = ''
  107. state.isSwitchedMember = false
  108. state.isSwitchedChild = false
  109. state.currentChildId = ''
  110. state.currentView = 'self'
  111. uni.setStorageSync('currentRole', originalRole)
  112. uni.removeStorageSync('currentMemberId')
  113. uni.removeStorageSync('currentMemberRole')
  114. uni.setStorageSync('isSwitchedMember', false)
  115. uni.removeStorageSync('currentChildId')
  116. uni.setStorageSync('isSwitchedChild', false)
  117. uni.setStorageSync('currentView', 'self')
  118. },
  119. // 切换视图模式(不修改数据库中的实际角色)
  120. switchViewMode(state, role) {
  121. state.currentRole = role
  122. uni.setStorageSync('currentRole', role)
  123. },
  124. // 兼容旧的方法名
  125. switchRole(state, role) {
  126. state.currentRole = role
  127. uni.setStorageSync('currentRole', role)
  128. },
  129. logout(state) {
  130. state.token = ''
  131. state.userId = ''
  132. state.role = 'parent'
  133. state.currentRole = 'parent'
  134. state.familyId = ''
  135. state.nickname = ''
  136. state.currentChildId = ''
  137. state.currentMemberId = ''
  138. state.currentMemberRole = ''
  139. state.children = []
  140. state.isSwitchedChild = false
  141. state.isSwitchedMember = false
  142. state.isSwitchedTeacher = false
  143. state.currentView = 'self'
  144. uni.removeStorageSync('token')
  145. uni.removeStorageSync('currentRole')
  146. uni.removeStorageSync('isSwitchedChild')
  147. uni.removeStorageSync('isSwitchedTeacher')
  148. uni.removeStorageSync('userId')
  149. uni.removeStorageSync('role')
  150. uni.removeStorageSync('openid')
  151. uni.removeStorageSync('userInfo')
  152. uni.removeStorageSync('nickname')
  153. uni.removeStorageSync('currentChildId')
  154. uni.removeStorageSync('currentMemberId')
  155. uni.removeStorageSync('currentMemberRole')
  156. uni.removeStorageSync('isSwitchedMember')
  157. uni.removeStorageSync('currentView')
  158. },
  159. markNoFamily: function(state) {
  160. if (state.familyId !== '' && state.familyId !== null && state.familyId !== 0) {
  161. console.warn('[STORE] 后端返回 5001 但本地 familyId 非空,已同步本地为无家庭态', state.familyId)
  162. }
  163. state.familyId = ''
  164. },
  165. setFamilyId: function(state, familyId) {
  166. state.familyId = familyId
  167. }
  168. },
  169. actions: {
  170. login({ commit }, userInfo) {
  171. commit('setToken', userInfo.token)
  172. commit('setUserInfo', userInfo)
  173. },
  174. updateChildren({ commit }, children) {
  175. commit('setChildren', children)
  176. },
  177. // 切换孩子前检查家庭中是否有孩子成员
  178. switchToChildWithCheck({ commit }, { memberId, members }) {
  179. var hasChildren = members && members.some(function(m) {
  180. return m.effectiveRole === 'child'
  181. })
  182. if (!hasChildren) {
  183. uni.showToast({ title: '家庭中暂无孩子', icon: 'none' })
  184. return false
  185. }
  186. commit('switchToChild', memberId)
  187. return true
  188. },
  189. // 切换到家庭成员前检查(仅 child 角色需要前置检查)
  190. switchToMemberWithCheck({ commit }, payload) {
  191. if (payload.effectiveRole === 'child') {
  192. var members = payload.members || []
  193. var hasChildren = members.some(function(m) {
  194. return m.effectiveRole === 'child'
  195. })
  196. if (!hasChildren) {
  197. uni.showToast({ title: '家庭中暂无孩子', icon: 'none' })
  198. return false
  199. }
  200. }
  201. commit('switchToMember', payload)
  202. return true
  203. }
  204. }
  205. ,
  206. modules: { tianpan, feedback, healthAlert, healthScore }
  207. })
  208. export default store