CheckInList.vue 9.69 KB
<template>
    <!-- 列表主体 -->
    <div :class="wrapper_class" :style="scroll_style">
        <button
            v-for="item in items"
            :key="item.id"
            class="CheckInListItem flex flex-col items-center p-2 rounded-lg border transition-colors bg-white/70 border-gray-100 hover:bg-white"
            :class="{ 'is-active': selected_item?.id === item.id }"
            @click="handle_select(item)"
        >
            <div class="Icon w-12 h-12 rounded-full flex items-center justify-center mb-1 transition-colors bg-gray-100"
                :class="{ 'is-active': selected_item?.id === item.id }"
            >
                <van-icon v-if="item.task_type === 'checkin'" name="edit" size="1.5rem" :color="item.is_gray ? 'gray' : ''" />
                <van-icon v-if="item.task_type === 'upload'" name="tosend" size="1.5rem" :color="item.is_gray ? 'gray' : ''" />
            </div>
            <span :class="['text-xs', item.is_gray ? 'text-gray-500' : '']">{{ item.name }}</span>
        </button>
    </div>

    <!-- 提交按钮 -->
    <div v-if="selected_item" class="mt-3">
        <button
            class="SubmitBtn mt-2 w-full bg-gradient-to-r from-green-500 to-green-600 text-white py-2 rounded-lg flex items-center justify-center"
            @click="handle_submit"
            :disabled="submitting"
        >
            <template v-if="submitting">
                <div class="w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin mr-2"></div>
                提交中...
            </template>
            <template v-else>提交打卡</template>
        </button>
    </div>

    <!-- 二级弹框(课程列表,使用mock数据) -->
    <van-popup
        :show="inner_popup_show"
        @update:show="(v) => inner_popup_show = v"
        round
        position="bottom"
        teleport="body"
        :close-on-click-overlay="false"
        :style="{ minHeight: '40%', maxHeight: '80%', width: '100%' }"
    >
        <div class="p-4">
            <div class="flex justify-between items-center mb-3">
                <h3 class="font-medium">课程列表</h3>
                <van-icon name="cross" @click="close_inner_popup" />
            </div>
            <div class="grid grid-cols-2 gap-4">
                <div v-for="course in inner_courses" :key="course.id" class="rounded-xl overflow-hidden bg-white/80">
                    <div class="h-24 relative">
                        <img
                            :src="format_cdn_image(course.imageUrl)"
                            :alt="course.title"
                            class="w-full h-full object-cover"
                        />
                        <div v-if="course.isPurchased" class="absolute top-0 left-0 bg-amber-500 text-white text-xs px-2 py-1 rounded-br-lg font-medium" style="background-color: rgba(249, 115, 22, 0.85)">
                            已购
                        </div>
                    </div>
                    <div class="p-2">
                        <h4 class="text-sm font-medium line-clamp-1">{{ course.title }}</h4>
                        <p class="text-xs text-gray-500 line-clamp-1">{{ course.subtitle }}</p>
                        <div class="flex justify-between items-center mt-2">
                            <span class="text-xs text-green-600">¥{{ course.price }}</span>
                            <button class="text-xs px-2 py-1 bg-green-600 text-white rounded" @click="select_inner_course(course)">选择</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </van-popup>
</template>

<script setup>
import { ref, computed, inject } from 'vue'
import { useRouter } from 'vue-router'
import { checkinTaskAPI } from '@/api/checkin'
import { showToast } from 'vant'
import { courses as mock_courses } from '@/utils/mockData'

/**
 * @typedef {Object} CheckInItem
 * @property {number|string} id - 任务ID。
 * @property {string} name - 任务名称。
 * @property {string} task_type - 任务类型,`checkin` 或 `upload`。
 * @property {boolean} [is_gray] - 是否置灰,表示今日已完成。
 */

/**
 * @function props
 * @description 组件接收的属性定义。
 */
const props = defineProps({
    items: { type: Array, default: () => [] },
    dense: { type: Boolean, default: false },
    scroll: { type: Boolean, default: false },
    plain: { type: Boolean, default: false },
})

/**
 * @function emits
 * @description 组件对外抛出的事件。
 */
const emit = defineEmits(['submit-success'])

const router = useRouter()
const selected_item = ref(null)
const submitting = ref(false)
// 父弹框联动(仅弹框模式下有效)
const parent_popup = inject('parent_popup_control', null)
// 二级弹框与数据
const inner_popup_show = ref(false)
const inner_courses = ref([])

/**
 * @function wrapper_class
 * @description 计算列表容器类名。
 * @returns {string[]}
 */
const wrapper_class = computed(() => [
    'CheckInListWrapper',
    props.dense ? 'grid grid-cols-2 gap-2 py-2' : 'grid grid-cols-2 gap-4 py-2',
])

/**
 * @function scroll_style
 * @description 当 `scroll` 为真时启用滚动区域样式。
 * @returns {Object}
 */
const scroll_style = computed(() => {
    if (!props.scroll) return {}
    return { maxHeight: '13rem', overflow: 'auto' }
})

/**
 * @function handle_select
 * @description 处理打卡类型选择:已完成提示;上传型跳转;否则选中。
 * @param {CheckInItem} item - 当前点击的打卡项。
 * @returns {void}
 */
const handle_select = (item) => {
    // TODO: 想要判断是否有二级菜单
    const has_submenu = item.children && item.children.length > 0;
    // 如果有二级菜单需要特殊处理
    if (has_submenu) {
        // 不同模式下弹框的显示逻辑是不一样的
        if (props.plain) {
            // 普通模式:直接弹出本组件的popup
            open_inner_popup()
        } else {
            // 弹框模式:先隐藏父级弹框,再弹出本组件的popup,关闭后重新打开父级弹框
            if (parent_popup && typeof parent_popup.hideParent === 'function') {
                parent_popup.hideParent()
            }
            // 略微延迟以确保父弹框状态切换完成
            setTimeout(() => open_inner_popup(), 50)
        }
        return
    }
    // 点击已完成打卡项提示
    if (item.is_gray && item.task_type === 'checkin') {
        showToast('您已经完成了今天的打卡')
        return
    }
    // 点击上传项跳转上传页
    if (item.task_type === 'upload') {
        router.push({
            path: '/checkin/index',
            query: { id: item.id },
        })
        return
    }
    selected_item.value = item
}

/**
 * @function handle_submit
 * @description 提交打卡调用接口,成功后抛出事件并复位。
 * @returns {Promise<void>}
 */
const handle_submit = async () => {
    if (!selected_item.value) {
        showToast('请选择打卡项目')
        return
    }
    submitting.value = true
    try {
        const { code } = await checkinTaskAPI({ task_id: selected_item.value.id })
        if (code) {
            emit('submit-success')
            showToast('打卡成功')
            selected_item.value = null
        }
    } catch (e) {
        // showToast('打卡失败,请重试')
    } finally {
        submitting.value = false
    }
}

/**
 * @function open_inner_popup
 * @description 打开二级弹框并填充课程列表(mock 数据)。
 * @returns {void}
 */
const open_inner_popup = () => {
    inner_courses.value = build_course_list()
    inner_popup_show.value = true
}

/**
 * @function close_inner_popup
 * @description 关闭二级弹框;若处于弹框模式则重新打开父级弹框。
 * @returns {void}
 */
const close_inner_popup = () => {
    inner_popup_show.value = false
    if (!props.plain && parent_popup && typeof parent_popup.reopenParent === 'function') {
        // 略微延迟,避免与二级弹框关闭动画冲突
        setTimeout(() => parent_popup.reopenParent(), 150)
    }
}

/**
 * @function build_course_list
 * @description 构造课程列表(来源于 mock 数据)。
 * @returns {Array}
 */
const build_course_list = () => {
    return (mock_courses || []).map(c => ({
        id: c.id,
        title: c.title,
        subtitle: c.subtitle,
        imageUrl: c.imageUrl,
        price: c.price,
        isPurchased: !!c.isPurchased
    }))
}

/**
 * @function select_inner_course
 * @description 选择二级弹框中的课程(占位行为:提示并关闭二级弹框)。
 * @param {Object} course - 课程对象。
 * @returns {void}
 */
const select_inner_course = (course) => {
    showToast(`已选择课程:${course.title}`)
    close_inner_popup()
}

/**
 * @function format_cdn_image
 * @description 若图片来自 cdn.ipadbiz.cn,则追加压缩参数;否则原样返回。
 * @param {string} url - 图片地址。
 * @returns {string}
 */
const format_cdn_image = (url) => {
    if (!url) return ''
    const host = 'cdn.ipadbiz.cn'
    if (url.includes(host)) {
        return `${url}?imageMogr2/thumbnail/200x/strip/quality/70`
    }
    return url
}
</script>

<style lang="less" scoped>
.CheckInListWrapper {
    // 列表项样式
    .CheckInListItem {
        // 选中态样式
        &.is-active {
            border-color: #bbf7d0; // 绿色边框
            background-color: rgba(16, 185, 129, 0.1); // 轻微绿色背景
        }

        // 图标样式
        .Icon {
            &.is-active {
                background-color: #10b981; // 绿色激活背景
                color: #ffffff; // 白色图标
            }
        }
    }

    // 提交按钮样式
    .SubmitBtn {
        &:disabled {
            opacity: 0.7; // 禁用态透明度
        }
    }
}
</style>