import { ptSaveAsDefault, ptResetAsDefault } from '@/api/pt/neConfig'; import { getPtClassStudents, stuPtNeConfigApply, updatePtNeConfigApply, } from '@/api/pt/neConfigApply'; import { RESULT_CODE_SUCCESS } from '@/constants/result-constants'; import { hasRoles } from '@/plugins/auth-user'; import { message } from 'ant-design-vue/lib'; import { computed, onMounted, reactive, ref } from 'vue'; /** * 实训教学函数 * @param param 父级传入 {t,fnActiveConfigNode} * @returns */ export default function usePtOptions({ t, fnActiveConfigNode }: any) { const ptConfigState = reactive({ saveLoading: false, restLoading: false, applyLoading: false, }); /**保存网元下所有配置为示例配置 */ function ptConfigSave(neType: string) { ptConfigState.saveLoading = true; ptSaveAsDefault(neType, '001') .then(res => { if (res.code === RESULT_CODE_SUCCESS) { message.success({ content: t('common.operateOk'), duration: 3, }); } else { message.error({ content: `${res.msg}`, duration: 3, }); } }) .finally(() => { ptConfigState.saveLoading = false; }); } /**重置网元下所有配置 */ function ptConfigReset(neType: string) { ptConfigState.restLoading = true; ptResetAsDefault(neType) .then(res => { if (res.code === RESULT_CODE_SUCCESS) { message.success({ content: t('common.operateOk'), duration: 3, }); } else { message.error({ content: `${res.msg}`, duration: 3, }); } }) .finally(() => { ptConfigState.restLoading = false; fnActiveConfigNode('#'); }); } /**配置下方应用(学生)申请撤回和(管理/教师)应用退回 */ function ptConfigApply(neType: string, status: '0' | '1' |'2'| '3' ) { let result: any; if (status === '2') { result = updatePtNeConfigApply({ neType, status: '2' }); } if (status === '0') { result = stuPtNeConfigApply({ neType, status: '0' }); } if (!result) return; ptConfigState.applyLoading = true; result .then((res: any) => { if (res.code === RESULT_CODE_SUCCESS) { message.success({ content: t('common.operateOk'), duration: 3, }); } else { message.error({ content: `${res.msg}`, duration: 3, }); } }) .finally(() => { ptConfigState.applyLoading = false; }); } const classState = reactive<{ /**学生账号 */ student: string | undefined; /**学生可选择列表 */ studentOptions: { value: string; label: string; status: string }[]; studentOptionsDef: { value: string; label: string; status: string }[]; }>({ student: undefined, studentOptions: [], studentOptionsDef: [], }); // 仅教师加载 if (hasRoles(['teacher'])) { onMounted(() => { // 初始学生列表 getPtClassStudents().then(res => { if (!Array.isArray(res.data) || res.data.length <= 0) { return; } classState.studentOptions = []; for (const s of res.data) { classState.studentOptions.push({ value: s.userName, label: s.userName, status: s.applyStatus, }); } Object.assign(classState.studentOptionsDef, classState.studentOptions); }); }); } let timeout: any; /**学生选择搜索 */ function studentSearch(val: string) { if (timeout) { clearTimeout(timeout); timeout = null; } if (!val) { Object.assign(classState.studentOptions, classState.studentOptionsDef); return; } function fake() { getPtClassStudents({ userName: val }).then(res => { classState.studentOptions = []; for (const s of res.data) { classState.studentOptions.push({ value: s.userName, label: s.userName, status: s.applyStatus, }); } }); } timeout = setTimeout(fake, 500); } // 学生状态 const studentStatus = computed(() => { const item = classState.studentOptions.find( s => s.value === classState.student ); if (item) return item.status; return ''; }); return { ptConfigState, ptConfigSave, ptConfigReset, ptConfigApply, classState, studentStatus, studentSearch, }; }