index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. children: [],
  14. isSwitchedChild: false // 家长切换到孩子状态的标记
  15. },
  16. mutations: {
  17. setToken(state, token) {
  18. state.token = token
  19. uni.setStorageSync('token', token)
  20. },
  21. setUserInfo(state, userInfo) {
  22. state.userId = userInfo.userId
  23. state.role = userInfo.role
  24. state.currentRole = userInfo.role // 初始化当前角色为实际角色
  25. state.familyId = userInfo.familyId
  26. state.nickname = userInfo.nickname
  27. // 保存到本地存储
  28. uni.setStorageSync('userId', userInfo.userId)
  29. uni.setStorageSync('role', userInfo.role)
  30. uni.setStorageSync('currentRole', userInfo.role)
  31. uni.setStorageSync('familyId', userInfo.familyId)
  32. // 登录时清除切换标记
  33. state.isSwitchedChild = false
  34. uni.setStorageSync('isSwitchedChild', false)
  35. },
  36. setChildren(state, children) {
  37. state.children = children
  38. if (children.length > 0 && !state.currentChildId) {
  39. state.currentChildId = children[0].id
  40. }
  41. },
  42. setCurrentChild(state, childId) {
  43. state.currentChildId = childId
  44. uni.setStorageSync('currentChildId', childId)
  45. },
  46. // 切换到孩子模式(家长切换)
  47. switchToChild(state, childId) {
  48. state.currentRole = 'child'
  49. state.currentChildId = childId
  50. state.isSwitchedChild = true
  51. uni.setStorageSync('currentRole', 'child')
  52. uni.setStorageSync('currentChildId', childId)
  53. uni.setStorageSync('isSwitchedChild', true)
  54. },
  55. // 切换回家长模式
  56. switchBackToParent(state) {
  57. state.currentRole = 'parent'
  58. state.currentChildId = ''
  59. state.isSwitchedChild = false
  60. uni.setStorageSync('currentRole', 'parent')
  61. uni.removeStorageSync('currentChildId')
  62. uni.setStorageSync('isSwitchedChild', false)
  63. },
  64. // 切换视图模式(不修改数据库中的实际角色)
  65. switchViewMode(state, role) {
  66. state.currentRole = role
  67. uni.setStorageSync('currentRole', role)
  68. },
  69. // 兼容旧的方法名
  70. switchRole(state, role) {
  71. state.currentRole = role
  72. uni.setStorageSync('currentRole', role)
  73. },
  74. logout(state) {
  75. state.token = ''
  76. state.userId = ''
  77. state.role = 'parent'
  78. state.currentRole = 'parent'
  79. state.familyId = ''
  80. state.nickname = ''
  81. state.currentChildId = ''
  82. state.children = []
  83. state.isSwitchedChild = false
  84. state.isSwitchedTeacher = false
  85. // 清除全部登录态存储
  86. uni.removeStorageSync('token')
  87. uni.removeStorageSync('currentRole')
  88. uni.removeStorageSync('isSwitchedChild')
  89. uni.removeStorageSync('isSwitchedTeacher')
  90. uni.removeStorageSync('userId')
  91. uni.removeStorageSync('role')
  92. uni.removeStorageSync('familyId')
  93. uni.removeStorageSync('openid')
  94. uni.removeStorageSync('userInfo')
  95. uni.removeStorageSync('nickname')
  96. uni.removeStorageSync('currentChildId')
  97. }
  98. },
  99. actions: {
  100. login({ commit }, userInfo) {
  101. commit('setToken', userInfo.token)
  102. commit('setUserInfo', userInfo)
  103. },
  104. updateChildren({ commit }, children) {
  105. commit('setChildren', children)
  106. }
  107. }
  108. })
  109. export default store