PassedExam.vue 8 KB
<template>
  <view class="passed-exam">
    <scroll-view scroll-y ref="scrollViewRef" :scroll-top="scrollTop" @scrolltolower="pageonReachBottom"
      :class="getScrollHeight">
      <template v-if="examList.length">
        <view class="exam-list" :class="{ 'fixed-width': cardWidth > 0 }">
          <view class="exam-item" v-for="(item, index) in examList" :key="index"
            @click="viewExam(item, index)" :style="cardWidth > 0 ? `width: ${cardWidth}rpx` : ''">
            <image class="exam-item-img"
              src="../../../../assets/images/common/course-default-bg.webp" mode="scaleToFill" />
            <view class="exam-content">
              <view class="exam-item-title">
                {{ item.title }}
              </view>
              <view class="exam-item-text">
                <text class="duration-value">考试时间</text>
                <text>{{ filterValue(item.duration, 'min') }} | {{ filterValue(item.questionCount, '题') }}</text>
              </view>

              <!-- 考试次数 -->
              <view class="exam-times">
                <text class="times-value">剩余考试次数</text>
                <text>{{ filterValue(item.remainingTimes) }}/{{ filterValue(item.examTimes) }}次</text>
              </view>

              <!-- 最高得分 -->
              <view class="exam-score">
                <text class="score-label">最高得分</text>
                <text class="score-value passed">{{ filterValue(item.highestScore, '分') }}|已通过</text>
              </view>

              <!-- 重新考试按钮 -->
              <view class="retake-btn" @click.stop="retakeExam(item)">
                <text class="retake-text">重新考试</text>
              </view>
            </view>
          </view>
        </view>
      </template>
      <template v-else>
        <view class="nothing flex">
          <image class="icon" src="../../../../assets/images/common/noData.png" />
          暂无数据
        </view>
      </template>
      <template v-if="nothing">
        <view class="end-text">到底了!</view>
      </template>
    </scroll-view>
  </view>
</template>

<script>
import utils from '@/utils/utils'
export default {
  name: 'PassedExam',
  props: {
    cardWidth: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      examList: [],
      nothing: false,
      total: 0,
      pageNum: 1,
      pageSize: 12,
      scrollTop: 0,
    }
  },
  computed: {
    getScrollHeight() {
      const list = this.examList;
      if (!list || list.length === 0) {
        return '';
      }
      return 'scroll-view'
    }
  },
  mounted() {
    this.getExamList()
  },
  methods: {
    // val:判断的值,unit单位
    filterValue(val, unit) {
      return utils.filterValue(val, unit)
    },
    async getExamList() {
      console.log(11)
      try {
        uni.showLoading({
          mask: true
        })
        const { pageNum, pageSize } = this
        const params = {
          examResult: 1, // 已通过
          pageNum,
          pageSize,
          staffPhone: uni.$storage.get('userInfo').phone
        }
        // TODO: 替换为实际的考试列表接口
        const res = await uni.$api.commonApi.getExamPage(params)
        console.log(res)
        if (pageNum == 1) {
          this.examList = res.list.concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list).concat(res.list)
        } else {
          this.examList = this.examList.concat(res.list)
        }
        this.total = res.total
        this.nothing = this.examList.length >= this.total && this.pageNum > 1
        uni.hideLoading()
      } catch (err) {
        console.log(err)
        uni.hideLoading()
      }
    },
    pageonReachBottom() {
      if (this.nothing) return
      if (this.examList.length >= this.total && this.pageNum > 1) {
        this.nothing = true
        return
      }
      this.pageNum++
      this.getExamList()
    },
    viewExam(item) {
      console.log('查看考试', item)
      // TODO: 跳转到考试详情页
    },
    retakeExam(item) {
      console.log('重新考试', item)
      // TODO: 跳转到考试页面
    }
  }
}
</script>

<style lang="scss" scoped>
.passed-exam {
  width: 100%;
  // max-height: toRpx(1200);

  .scroll-view {
    max-height: toRpx(1225);
  }

  .exam-list {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: toRpx(32);
    // padding: 0 toRpx(40) toRpx(32);
    font-size: 0;

    &.fixed-width {
      grid-template-columns: repeat(4, toRpx(530));
      justify-content: center;
      
      .exam-item {
        width: toRpx(530) !important;
        max-width: toRpx(530);
        min-width: toRpx(530);
      }
    }

    .exam-item {
      width: 100%;
      min-height: toRpx(376);
      box-shadow: 0 toRpx(8) toRpx(24) 0 #2a61eb1a;
      border-radius: toRpx(24);
      overflow: hidden;
      cursor: pointer;
      background: #ffffff;
      position: relative;

      .exam-item-img {
        width: 100%;
        height: toRpx(180);
      }

      .exam-content {
        padding: toRpx(32);

        .exam-item-title {
          width: 100%;
          height: toRpx(36);
          color: #292d33;
          font-size: toRpx(30);
          font-weight: 500;
          line-height: toRpx(36);
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 1;
          line-clamp: 1;
          overflow: hidden;
          text-overflow: ellipsis;
          margin-bottom: toRpx(20);
        }

        .exam-item-text {
          display: flex;
          align-items: center;
          margin-bottom: toRpx(12);
          height: toRpx(36);
          color: #666;
          font-size: toRpx(24);
          font-weight: 400;
          line-height: toRpx(36);

          .duration-value {
            margin-right: toRpx(24);
            color: #999ea8;
          }
        }

        // 考试次数
        .exam-times {
          display: flex;
          align-items: center;
          margin-bottom: toRpx(12);
          height: toRpx(36);
          color: #666;
          font-size: toRpx(24);
          font-weight: 400;
          line-height: toRpx(36);

          .times-value {
            margin-right: toRpx(24);
            color: #999ea8;
          }
        }

        // 最高得分
        .exam-score {
          display: flex;
          align-items: center;
          margin-bottom: toRpx(20);
          font-size: toRpx(24);
          font-weight: 400;

          .score-label {
            // width: toRpx(96);
            height: toRpx(36);
            color: #999ea8;
            text-align: left;
            line-height: toRpx(36);
            margin-right: toRpx(24);
          }

          .score-value {
            // width: toRpx(346.5);
            height: toRpx(36);
            text-align: left;
            line-height: toRpx(36);

            &.passed {
              color: #2cca74;
            }
          }
        }

        // 重新考试按钮
        .retake-btn {
          width: toRpx(466.5);
          height: toRpx(56);
          border-radius: toRpx(24);
          opacity: 1;
          background: #f1f7fe;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;

          .retake-text {
            // width: toRpx(104);
            height: toRpx(32);
            opacity: 1;
            color: #3b73ff;
            font-size: toRpx(26);
            font-weight: 400;
            text-align: center;
            line-height: toRpx(32);
          }
        }
      }
    }
  }

  .nothing {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    height: toRpx(1000);
    color: #999999;
    font-size: toRpx(24);

    .icon {
      width: toRpx(344);
      height: toRpx(208);
    }
  }

  .end-text {
    text-align: center;
    color: #999999;
    font-size: toRpx(24);
    padding: toRpx(40) 0;
  }
}
</style>