66 lines
3.1 KiB
TypeScript
66 lines
3.1 KiB
TypeScript
import type { StudentAlert, StudentHomeMetrics, StudentHomeProfile } from '../api/student'
|
|
|
|
type BoundStudentLike = { id: string, name: string, account: string, grade: string, school: string, className: string, avatar: string }
|
|
|
|
// 仅在本次登录会话内缓存首页学生资料,切换一级页面时不重复请求。
|
|
export const homeStudentProfile = ref<StudentHomeProfile | null>(null)
|
|
export const homeStudentMetrics = ref<StudentHomeMetrics | null>(null)
|
|
export const homeStudentAlerts = ref<StudentAlert[] | null>(null)
|
|
export const selectedStudentChangeVersion = ref(0)
|
|
|
|
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: BoundStudentLike) {
|
|
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: BoundStudentLike) {
|
|
const comprehensiveLevel = homeStudentProfile.value?.id === student.id ? homeStudentProfile.value.comprehensiveLevel : 'good'
|
|
setHomeStudentProfile({ ...student, comprehensiveLevel })
|
|
}
|
|
|
|
export function switchBoundStudent(student: BoundStudentLike) {
|
|
saveSelectedStudent(student)
|
|
applyBoundStudent(student)
|
|
homeStudentMetrics.value = null
|
|
homeStudentAlerts.value = null
|
|
selectedStudentChangeVersion.value++
|
|
}
|
|
|
|
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
|
|
}
|