| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <template>
- <view>
- <!-- 家长端首页 -->
- <parent-index v-if="currentRole === 'parent'" />
- <!-- 孩子端首页 -->
- <child-index v-else-if="currentRole === 'child'" />
- <!-- 成长规划师首页 -->
- <teacher-index v-else-if="currentRole === 'teacher'" />
- <!-- 默认显示加载中或错误提示 -->
- <view v-else class="loading-container">
- <text>加载中...</text>
- </view>
- </view>
- </template>
- <script>
- import ParentIndex from './parent-index.vue'
- import ChildIndex from './child-index.vue'
- import TeacherIndex from '../teacher/index.vue'
- import { acceptParentInvite } from '../../utils/api.js'
- export default {
- components: {
- ParentIndex,
- ChildIndex,
- TeacherIndex
- },
- data() {
- return {
- currentRole: ''
- }
- },
- onLoad(options) {
- this.syncRoleAndTabBar()
- // 检查是否有邀请码参数
- if (options.inviteCode && options.type === 'parent_invite') {
- this.handleParentInvite(options.inviteCode)
- } else {
- // 检查是否有存储的邀请码(登录后返回)
- const storedCode = uni.getStorageSync('inviteCode')
- const storedType = uni.getStorageSync('inviteType')
- if (storedCode && storedType === 'parent_invite') {
- this.handleParentInvite(storedCode)
- // 清除存储
- uni.removeStorageSync('inviteCode')
- uni.removeStorageSync('inviteType')
- }
- }
- },
- onShow() {
- this.syncRoleAndTabBar()
- },
- methods: {
- normalizeCurrentRole() {
- const role = uni.getStorageSync('role') || 'parent'
- const currentRole = uni.getStorageSync('currentRole')
- if (!currentRole) {
- return role
- }
- // 非规划师账号不允许落到规划师首页
- if (currentRole === 'teacher' && role !== 'teacher') {
- return role
- }
- // 只有家长账号允许切换到孩子视角
- if (currentRole === 'child' && role !== 'parent') {
- return role
- }
- return currentRole
- },
- syncTabBarText(role) {
- const secondText = role === 'teacher' ? '家庭管理' : '任务'
- const thirdText = role === 'teacher' ? '消息' : '心愿单'
- uni.setTabBarItem({ index: 1, text: secondText })
- uni.setTabBarItem({ index: 2, text: thirdText })
- },
- syncRoleAndTabBar() {
- this.currentRole = this.normalizeCurrentRole()
- uni.setStorageSync('currentRole', this.currentRole)
- this.syncTabBarText(this.currentRole)
- },
- async handleParentInvite(code) {
- // 延迟显示,等待登录完成
- setTimeout(async () => {
- const hasToken = uni.getStorageSync('token')
- if (!hasToken) {
- // 未登录,先去登录
- uni.reLaunch({
- url: '/pages/login/login?inviteCode=' + code
- })
- return
- }
- // 已登录,显示确认框
- uni.showModal({
- title: '加入家庭',
- content: '您已被邀请加入家庭,是否接受?',
- success: async (res) => {
- if (res.confirm) {
- try {
- uni.showLoading({ title: '加入中...' })
- const result = await acceptParentInvite(code)
- uni.hideLoading()
- if (result.code === 200) {
- const { familyId, role, token } = result.data
- uni.setStorageSync('familyId', familyId)
- uni.setStorageSync('role', role)
- uni.setStorageSync('token', token)
- uni.showToast({ title: '加入成功', icon: 'success' })
- // 刷新页面
- setTimeout(() => {
- uni.reLaunch({ url: '/pages/index/index' })
- }, 1500)
- }
- } catch (e) {
- uni.hideLoading()
- uni.showToast({ title: e.message || '加入失败', icon: 'none' })
- }
- }
- }
- })
- }, 500)
- }
- }
- }
- </script>
- <style>
- .loading-container {
- display: flex;
- justify-content: center;
- align-items: center;
- height: 100vh;
- }
- </style>
|