CompletedCourse.vue 7.43 KB
 <template>
  <view class="completed-course">
    <scroll-view scroll-y ref="scrollViewRef" :scroll-top="scrollTop" @scrolltolower="pageonReachBottom"
      :class="getScrollHeight">
      <template v-if="completedCourseList.length">
        <view class="completed-course-list" :class="{ 'fixed-width': cardWidth > 0 }">
          <view class="completed-course-item" v-for="(item, index) in completedCourseList" :key="index"
            @click="startStudy(item, index)" :style="cardWidth > 0 ? `width: ${cardWidth}rpx` : ''">
            <!-- 左上角:必修/选修标签 -->
            <image v-if="item.required !== undefined" class="course-tag-left"
              :src="item.required ? require('@/assets/images/studyCenter/Required.png') : require('@/assets/images/studyCenter/Elective.png')"
              mode="aspectFit" />

            <!-- 右上角:文档/视频标签 -->
            <view v-if="item.courseType" class="course-tag-right">
              <text class="tag-text">{{ item.courseType === 'video' ? '视频' : '文档' }}</text>
            </view>

            <image class="completed-course-item-img"
              src="../../../../assets/images/common/recommend-course-default.webp" mode="scaleToFill" />
            <view class="completed-course-content">
              <view class="completed-course-item-title">
                {{ item.taskTitle || item.courseName }}
              </view>

              <!-- 成功图片 -->
              <image class="success-icon" :src="require('@/assets/images/studyCenter/success.png')" mode="aspectFit" />
            </view>
          </view>
        </view>
      </template>
      <template v-else>
        <view class="nothing flex">
          <image class="icon" src="../../../../assets/images/boutique/nothing.png" />
          暂无数据
        </view>
      </template>
      <template v-if="nothing">
        <view class="end-text">到底了!</view>
      </template>
    </scroll-view>
  </view>
</template>
<script>
export default {
  name: 'CompletedCourse',
  props: {
    // 卡片宽度,不传则使用默认的响应式布局
    cardWidth: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      completedCourseList: [],
      nothing: false,
      total: 0,
      pageNum: 1,
      pageSize: 12,
      scrollTop: 0,
    }
  },
  computed: {
    getScrollHeight() {
      const list = this.completedCourseList;
      if (!list || list.length === 0) {
        return '';
      }
      return 'scroll-view'
    }
  },
  mounted() {
    this.getCourseTaskPage()
  },
  methods: {
    // 获取已完成课程接口
    async getCourseTaskPage() {
      try {
        uni.showLoading({
          mask: true
        })
        const { pageNum, pageSize } = this
        const params = {
          completeStatus: 1, // 完成状态 1=已完成
          pageNum,
          pageSize
        }
        const res = await uni.$api.commonApi.getCourseTaskPage(params)

        // 临时添加测试数据字段
        const listWithTestData = res.list.map((item, index) => ({
          ...item,
          required: index % 3 === 0, // 每3个课程第1个是必修
          courseType: index % 2 === 0 ? 'video' : 'document', // 交替显示视频/文档
        }))

        if (pageNum == 1) {
          this.completedCourseList = listWithTestData
        } else {
          this.completedCourseList = this.completedCourseList.concat(listWithTestData)
        }
        uni.hideLoading()
        this.total = res.total
        this.nothing = res.length ? true : false
      } catch (error) {
        uni.showToast({
          title: error,
          icon: "none",
        });
      }
    },
    pageonReachBottom() {
      console.log('到底了');
      let { pageNum, total, nothing = 'nothing', pageSize } = this;
      if (Math.ceil(total / pageSize) > pageNum) {
        this.pageNum += 1;
        this.getCourseTaskPage()
      } else {
        this.nothing = true;
      }
    },
    startStudy(item, index) {
      let { pageNum, total, nothing = 'nothing', pageSize } = this;
      uni.$track.add({ eventCode: 'app_completed_course', businessId: item.id, pagePath: '/pages/boutique/myCourse' })
      uni.navigateTo({
        url: `/pages/common/studyDetail/detail?type=completed&index=${index}&completedCourseList=${encodeURIComponent(JSON.stringify(this.completedCourseList))}&pageNum=${pageNum}&pageSize=${pageSize}&total=${total}`
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.completed-course {
  height: calc(100% - #{toRpx(100)});

  .scroll-view {
    margin-top: toRpx(40);
    height: calc(100% - #{toRpx(70)});

    .completed-course-list {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: toRpx(32);
      padding: 0 toRpx(40) toRpx(32);

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

      .completed-course-item {
        width: 100%;
        min-height: toRpx(376);
        box-shadow: 0 toRpx(8) toRpx(24) 0 #2a61eb1a;
        border-radius: toRpx(24);
        position: relative;

        // 左上角标签
        .course-tag-left {
          position: absolute;
          left: 0;
          top: 0;
          width: toRpx(94);
          height: toRpx(48);
          border-radius: toRpx(24) 0 toRpx(20) 0;
          z-index: 2;
        }

        // 右上角标签
        .course-tag-right {
          position: absolute;
          right: toRpx(16);
          top: toRpx(16);
          width: toRpx(72);
          height: toRpx(40);
          border-radius: toRpx(4);
          background: rgba(0, 0, 0, 0.35);
          display: flex;
          align-items: center;
          justify-content: center;
          z-index: 2;

          .tag-text {
            width: toRpx(48);
            height: toRpx(36);
            color: #ffffff;
            font-size: toRpx(24);
            font-weight: 400;
            font-family: "HarmonyOSSansSC";
            text-align: center;
            line-height: toRpx(36);
          }
        }

        .completed-course-item-img {
          width: 100%;
          height: toRpx(180);
          border-radius: toRpx(16) toRpx(16) 0 0;
        }

        .completed-course-content {
          padding: toRpx(32);
          position: relative;
          min-height: toRpx(196);

          .completed-course-item-title {
            font-size: toRpx(30);
            color: #292D33;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            text-overflow: ellipsis;
            word-break: break-all;
            line-height: 1.2;
            max-height: calc(2 * 1.2em);
          }

          // 成功图片 - 右下角定位
          .success-icon {
            position: absolute;
            bottom: toRpx(32);
            right: toRpx(33);
            width: toRpx(144);
            height: toRpx(144);
          }
        }
      }
    }
  }
}

.nothing {
  width: 100%;
  flex-direction: column;
  margin-top: toRpx(380);
  font-size: toRpx(28);
  color: #999999;

  .icon {
    width: toRpx(678);
    height: toRpx(284);
    margin-bottom: toRpx(24);
  }
}

.end-text {
  color: #999;
  text-align: center;
  padding: toRpx(30) 0;
  font-size: toRpx(24);
}

/deep/ {
  .uni-scroll-view-content {
    height: calc(100% - #{toRpx(20)});
  }
}
</style>