import { requestStudentInfo } from './actions/request' ; import pack from '@/../package.json' ; import { LocalStore } from './types' ; import local from './local' ; import { state } from '.' ; import { actions , Request } from './actions' ; import { allList as pluginList } from '@/plugins' ; import { convertSemesterNumberToName } from '@/helper/converter' ; import { getUserId } from '@/helper/getter' ; import { Num } from '@/helper/util' ; const { version } = pack ; interface AcademicInfo { courseNumber : number ; currentSemester : string ; gpa : number ; currentSemesterCourseNumber : number ; failedCourseNumber : number ; } let academicInfo ; let studentInfos ; let userSemesterNumberList ; const data = { } as { [ key ] : unknown ; } ; async function initPluginEnabledStates ( ) { const key = 'pluginEnabledStates' ; let pluginEnabledStates = state . getData ( key ) as Record < string , boolean > ; if ( ! pluginEnabledStates ) { pluginEnabledStates = Object . fromEntries ( pluginList . map ( ( { name , isNecessary , defaultEnabledState } ) => [ name , isNecessary || defaultEnabledState ] ) ) ; } else { pluginList . forEach ( ( { name , isNecessary , defaultEnabledState } ) => { if ( pluginEnabledStates [ name ] === undefined ) { pluginEnabledStates [ name ] = isNecessary || defaultEnabledState ; } } ) ; } state . setData ( key , pluginEnabledStates ) ; await local . saveData ( { key , payload : pluginEnabledStates } ) ; } async function init ( localStore ) { if ( localStore ) { for ( const key of Object . keys ( localStore . state . data ) ) { data [ key ] = local . get ( key ) ; } } await initPluginEnabledStates ( ) ; const res = await Promise . all ( [ actions [ Request . CURRENT_SEMESTER_STUDENT_ACADEMIC_INFO ] ( ) , requestStudentInfo ( ) , actions [ Request . STUDENT_SEMESTER_CODE_LIST ] ( ) ] ) ; academicInfo = res [ 0 ] ; studentInfos = res [ 1 ] ; userSemesterNumberList = res [ 2 ] ; } type User = { id : string ; programPlanNumber : number ; programPlanName : string ; semesterNumberList : string [ ] ; courseNumber : number ; gpa : number ; currentSemesterCourseNumber : number ; failedCourseNumber : number ; } ; type Core = { version : string ; clientType : 'urp' ; } ; type Basic = { currentSemesterNumber : string ; currentSemesterName : string ; } ; export default { init , get core ( ) : Core { return { version , clientType : 'urp' } ; } , get user ( ) : User { return { id : getUserId ( studentInfos ) , programPlanNumber : Num ( studentInfos . get ( '培养方案代码' ) ) , programPlanName : studentInfos . get ( '培养方案名称' ) || '' , semesterNumberList : userSemesterNumberList , courseNumber : academicInfo . courseNumber , gpa : academicInfo . gpa , currentSemesterCourseNumber : academicInfo . currentSemesterCourseNumber , failedCourseNumber : academicInfo . failedCourseNumber } ; } , get basic ( ) : Basic { return { currentSemesterNumber : academicInfo . currentSemester , currentSemesterName : convertSemesterNumberToName ( academicInfo . currentSemester ) } ; } , getData ( key ) { return data [ key ] ; } , setData ( key , payload ) { data [ key ] = payload ; } } ;