import type { StudentAlert, StudentHomeMetrics, StudentHomeProfile } from '../api/student' // 仅在本次登录会话内缓存首页学生资料,切换一级页面时不重复请求。 export const homeStudentProfile = ref(null) export const homeStudentMetrics = ref(null) export const homeStudentAlerts = ref(null) export function setHomeStudentProfile(profile: StudentHomeProfile) { const userInfo = uni.getStorageSync('userInfo') as { userId?: string } | null const boundStudent = userInfo?.userId ? uni.getStorageSync(`selectedStudent_${userInfo.userId}`) as { id?: string, name?: string, account?: string, grade?: string, school?: string, className?: string, avatar?: string } | null : null const currentProfile = homeStudentProfile.value?.id === profile.id ? homeStudentProfile.value : null const savedStudent = boundStudent?.id === profile.id ? boundStudent : null homeStudentProfile.value = { ...profile, name: savedStudent?.name ?? currentProfile?.name ?? profile.name, account: savedStudent?.account ?? currentProfile?.account ?? profile.account, grade: savedStudent?.grade ?? currentProfile?.grade ?? profile.grade, school: savedStudent?.school ?? currentProfile?.school ?? profile.school, className: savedStudent?.className ?? currentProfile?.className ?? profile.className, avatar: savedStudent?.avatar ?? currentProfile?.avatar ?? profile.avatar } } export function saveSelectedStudent(student: { id: string, name: string, account: string, grade: string, school: string, className: string, avatar: string }) { const userInfo = uni.getStorageSync('userInfo') as { userId?: string } | null if (userInfo?.userId) uni.setStorageSync(`selectedStudent_${userInfo.userId}`, student) } export function getSavedSelectedStudent(): { id: string, name: string, account: string, grade: string, school: string, className: string, avatar: string } | null { const userInfo = uni.getStorageSync('userInfo') as { userId?: string } | null if (!userInfo?.userId) return null return uni.getStorageSync(`selectedStudent_${userInfo.userId}`) as { id: string, name: string, account: string, grade: string, school: string, className: string, avatar: string } | null } export function applyBoundStudent(student: { id: string, name: string, account: string, grade: string, school: string, className: string, avatar: string }) { const comprehensiveLevel = homeStudentProfile.value?.id === student.id ? homeStudentProfile.value.comprehensiveLevel : 'good' setHomeStudentProfile({ ...student, comprehensiveLevel }) } export function setHomeStudentMetrics(metrics: StudentHomeMetrics) { homeStudentMetrics.value = metrics } export function setHomeStudentAlerts(alerts: StudentAlert[]) { homeStudentAlerts.value = alerts } export function clearHomeStudentProfile() { homeStudentProfile.value = null homeStudentMetrics.value = null homeStudentAlerts.value = null }