refactor: 整理代码结构

This commit is contained in:
huertian 2025-01-05 20:32:52 +08:00
parent e0995a9132
commit c0499114f2
2 changed files with 27 additions and 44 deletions

View File

@ -3,7 +3,7 @@ import BussApi from '@/api/BussApi';
import pageWrapper from '@/components/page-wrapper.vue';
import { useUser } from '@/stores/useUser';
import type { LessonTask } from '@/types/api/lesson';
import { calcLessonProgress } from '@/utils/lesson';
import { calcLessonProgress, getLessonSteps } from '@/utils/lesson';
import { onPageShow, onLoad, onPullDownRefresh } from '@dcloudio/uni-app';
import { useRouter } from 'uni-mini-router';
import { onMounted, ref, watch } from 'vue';
@ -13,7 +13,7 @@ import WdTag from 'wot-design-uni/components/wd-tag/wd-tag.vue';
const toast = useToast()
const router = useRouter()
const value = ref<number>(0)
const user = useUser()
const teacherFilterValue = ref<number>(0)
@ -44,6 +44,13 @@ const loadTeacherOptions = async () => {
}
}
const handleChange = ({ value: newValue }: { value: number }) => {
value.value = newValue
loadLessons().then(() => {
refreshKey.value++
})
}
onMounted(() => {
loadTeacherOptions()
})
@ -110,20 +117,18 @@ const loadLessons = async () => {
return acc
}, {})
//
if (value.value !== 0) {
Object.keys(groupData).forEach(courseName => {
groupData[courseName].sort((a: LessonTask, b: LessonTask) => {
const progressA = calcLessonProgress(a)
const progressB = calcLessonProgress(b)
return value.value === 1
? progressB - progressA //
: progressA - progressB //
? progressB - progressA
: progressA - progressB
})
})
}
console.log('分组后的课程数据:', JSON.stringify(groupData, null, 2))
groupedLessons.value = groupData
const expandedGroups = Object.keys(groupData).filter(courseName => {
@ -137,7 +142,6 @@ const loadLessons = async () => {
return (hasCompleted && hasNotStarted) || hasInProgress
})
//
expandedCourse.value = expandedGroups
} catch (err: unknown) {
if (err instanceof Error) {
@ -189,17 +193,6 @@ onLoad(() => {
loadLessons()
})
const value = ref<number>(0)
//
const handleChange = ({ value: newValue }: { value: number }) => {
console.log('排序值:', newValue)
value.value = newValue
loadLessons().then(() => {
//
refreshKey.value++
})
}
</script>
<template>
@ -221,17 +214,17 @@ const handleChange = ({ value: newValue }: { value: number }) => {
<template #title="{ expanded, disabled, isFirst }">
<div class="w-full flex justify-between items-center">
<div class="flex flex-col gap-1">
<p class="pt-1">
{{ courseName || '无标题课程' }}
</p>
<wd-badge is-dot>
<p class="pt-1">
{{ courseName || '无标题课程' }}
</p>
</wd-badge>
<div class="flex items-center gap-1">
<wd-tag v-if="(() => {
//
const hasCompleted = courses.some(lesson => calcLessonProgress(lesson) === 100)
const hasNotStarted = courses.some(lesson => calcLessonProgress(lesson) === 0)
if (hasCompleted && hasNotStarted) return true
// 0-100
const hasInProgress = courses.some(lesson => {
const progress = calcLessonProgress(lesson)
return progress > 0 && progress < 100
@ -290,22 +283,20 @@ const handleChange = ({ value: newValue }: { value: number }) => {
@click="openLessonDetail(lesson.id)"
class="w-full py-2 gap-12 flex justify-between items-center border-b border-b-solid border-neutral-100 last:border-b-0 first:pt-0 last:pb-0">
<div class="flex items-center gap-1 self-center">
<wd-badge is-dot hidden>
<div is-dot>
<div v-if="calcLessonProgress(lesson) === 100" class="i-tabler-circle-check"></div>
<div v-else-if="calcLessonProgress(lesson) === 0" class="i-tabler-circle-dashed"></div>
<div v-else class="i-tabler-hourglass-empty"></div>
</div>
<div>
<div v-if="calcLessonProgress(lesson) === 100" class="i-tabler-circle-check"></div>
<div v-else-if="calcLessonProgress(lesson) === 0" class="i-tabler-circle-dashed"></div>
<div v-else class="i-tabler-hourglass-empty"></div>
</div>
<wd-badge is-dot :hidden="true">
<span>{{ lesson.microLessonName || '无标题微课' }}</span>
</wd-badge>
<span>{{ lesson.microLessonName || '无标题微课' }}</span>
</div>
<div class="flex items-center gap-3 shrink-0">
<wd-badge hidden is-dot :top="-10">
<span v-if="!user.hasRole('teacher') && getUsernameById(lesson.userId)"
class="text-xs text-gray-400 ml-2 whitespace-nowrap">
{{ getUsernameById(lesson.userId) }}
</span>
</wd-badge>
<span v-if="!user.hasRole('teacher') && getUsernameById(lesson.userId)"
class="text-xs text-gray-400 ml-2 whitespace-nowrap">
{{ getUsernameById(lesson.userId) }}
</span>
<div class="w-16">
<wd-progress :percentage="calcLessonProgress(lesson)"
:color="calcLessonProgress(lesson) === 100 ? '#34d399' : '#60a5fa'" hide-text />

View File

@ -44,11 +44,6 @@ const selectedLessonStage = computed(() => {
return extractLessonStage(selectedLesson.value)
})
/**
* 判断advise是否为驳回状态
* @param advise 修改建议字符串
* @returns {boolean} 是否为驳回状态
*/
const isRejectAdvise = (advise: string | undefined): boolean => {
try {
if (!advise) return false
@ -313,15 +308,12 @@ const updateLessons = async () => {
try {
let res
//
if (user.canViewAllCourses()) {
res = await BussApi.getLessonTasks(1, 512)
} else {
//
res = await BussApi.getLessonTasks(1, 512, user.userinfo.id)
}
//
const groupData = res.data.content.sort((a: LessonTask, b: LessonTask) => {
return a.id - b.id
}).reduce((acc: any, cur: any) => {