index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view>
  3. <!-- 家长端首页 -->
  4. <parent-index v-if="currentRole === 'parent'" />
  5. <!-- 孩子端首页 -->
  6. <child-index v-else-if="currentRole === 'child'" />
  7. <!-- 成长规划师首页 -->
  8. <teacher-index v-else-if="currentRole === 'teacher'" />
  9. <!-- 默认显示加载中或错误提示 -->
  10. <view v-else class="loading-container">
  11. <text>加载中...</text>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import ParentIndex from './parent-index.vue'
  17. import ChildIndex from './child-index.vue'
  18. import TeacherIndex from '../teacher/index.vue'
  19. import { acceptParentInvite } from '../../utils/api.js'
  20. export default {
  21. components: {
  22. ParentIndex,
  23. ChildIndex,
  24. TeacherIndex
  25. },
  26. data() {
  27. return {
  28. currentRole: ''
  29. }
  30. },
  31. onLoad(options) {
  32. this.syncRoleAndTabBar()
  33. // 检查是否有邀请码参数
  34. if (options.inviteCode && options.type === 'parent_invite') {
  35. this.handleParentInvite(options.inviteCode)
  36. } else {
  37. // 检查是否有存储的邀请码(登录后返回)
  38. const storedCode = uni.getStorageSync('inviteCode')
  39. const storedType = uni.getStorageSync('inviteType')
  40. if (storedCode && storedType === 'parent_invite') {
  41. this.handleParentInvite(storedCode)
  42. // 清除存储
  43. uni.removeStorageSync('inviteCode')
  44. uni.removeStorageSync('inviteType')
  45. }
  46. }
  47. },
  48. onShow() {
  49. this.syncRoleAndTabBar()
  50. },
  51. methods: {
  52. normalizeCurrentRole() {
  53. const role = uni.getStorageSync('role') || 'parent'
  54. const currentRole = uni.getStorageSync('currentRole')
  55. if (!currentRole) {
  56. return role
  57. }
  58. // 非规划师账号不允许落到规划师首页
  59. if (currentRole === 'teacher' && role !== 'teacher') {
  60. return role
  61. }
  62. // 只有家长账号允许切换到孩子视角
  63. if (currentRole === 'child' && role !== 'parent') {
  64. return role
  65. }
  66. return currentRole
  67. },
  68. syncTabBarText(role) {
  69. const secondText = role === 'teacher' ? '家庭管理' : '任务'
  70. const thirdText = role === 'teacher' ? '消息' : '心愿单'
  71. uni.setTabBarItem({ index: 1, text: secondText })
  72. uni.setTabBarItem({ index: 2, text: thirdText })
  73. },
  74. syncRoleAndTabBar() {
  75. this.currentRole = this.normalizeCurrentRole()
  76. uni.setStorageSync('currentRole', this.currentRole)
  77. this.syncTabBarText(this.currentRole)
  78. },
  79. async handleParentInvite(code) {
  80. // 延迟显示,等待登录完成
  81. setTimeout(async () => {
  82. const hasToken = uni.getStorageSync('token')
  83. if (!hasToken) {
  84. // 未登录,先去登录
  85. uni.reLaunch({
  86. url: '/pages/login/login?inviteCode=' + code
  87. })
  88. return
  89. }
  90. // 已登录,显示确认框
  91. uni.showModal({
  92. title: '加入家庭',
  93. content: '您已被邀请加入家庭,是否接受?',
  94. success: async (res) => {
  95. if (res.confirm) {
  96. try {
  97. uni.showLoading({ title: '加入中...' })
  98. const result = await acceptParentInvite(code)
  99. uni.hideLoading()
  100. if (result.code === 200) {
  101. const { familyId, role, token } = result.data
  102. uni.setStorageSync('familyId', familyId)
  103. uni.setStorageSync('role', role)
  104. uni.setStorageSync('token', token)
  105. uni.showToast({ title: '加入成功', icon: 'success' })
  106. // 刷新页面
  107. setTimeout(() => {
  108. uni.reLaunch({ url: '/pages/index/index' })
  109. }, 1500)
  110. }
  111. } catch (e) {
  112. uni.hideLoading()
  113. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  114. }
  115. }
  116. }
  117. })
  118. }, 500)
  119. }
  120. }
  121. }
  122. </script>
  123. <style>
  124. .loading-container {
  125. display: flex;
  126. justify-content: center;
  127. align-items: center;
  128. height: 100vh;
  129. }
  130. </style>