RecommendCourse.js 2.89 KB
// packageStudyCenter/pages/myTask/components/RecommendCourse/RecommendCourse.js
import api from "~/packageStudyCenter/api"
import commonApi from '~/api/api'
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
Component({
  properties: {
    // 可以添加需要的属性
  },

  data: {
    IMG_URL,
    recommendCourseList: [],
    nothing: false,
    total: 0,
    pageNum: 1,
    pageSize: 10,
    scrollTop: 0,
  },

  lifetimes: {
    attached() {
      this.getCourseTaskPage()
    }
  },

  methods: {

    async getCourseTaskPage() {
      try {
        const { pageNum, pageSize } = this.data;
        const params = {
          recommendStatus: 1, // 推荐状态 0=无推荐 1=推荐课程
          pageNum,
          pageSize
        };
        
        const res = await api.getCourseTaskPage(params);
        
        if (pageNum == 1) {
          this.setData({ 
            recommendCourseList: res.data?.list || []
          });
        } else {
          this.setData({ 
            recommendCourseList: this.data.recommendCourseList.concat(res.data?.list || [])
          });
        }
        this.setData({ 
          total: res.data?.total || 0,
          nothing: (res.data?.list || []).length > 0 ? false : true
        });
      
      } catch (error) {
        wx.showToast({
          title: error.message || '请求失败',
          icon: "none",
        });
      }
    },

    pageonReachBottom() {
      const { pageNum, total, pageSize } = this.data;
      if (Math.ceil(total / pageSize) > pageNum) {
        this.setData({ 
          pageNum: pageNum + 1
        });
        this.getCourseTaskPage();
      } else {
        this.setData({ nothing: true });
      }
    },

    startStudy(e) {
      const item = e.currentTarget.dataset.item
      let userInfo = wx.$util.getUserInfo()
      // 埋点 推荐课程
      wx.$track.setTrackData(0, 'recommend_course', {}, '', userInfo?.sceneType)
      commonApi.findBeiSenSSOToken({}).then(res => {
        const beisenToken = res.data;
        if (beisenToken) {
          const COURSE_ID = item?.courseId;
          const baseUrl = 'https://cloud.italent.cn/PageHome/Index';
          const params = {
            product: 'Learning',
            keyName: 'Nusion',
            pageCode: 'LearningCourseTraineeDetailPage',
            appCode: 'Learning',
            id: COURSE_ID
          };
          this.jumpUrl = ''
          const queryString = Object.entries(params)
            .map(([key, value]) => `${key}=${value}`)
            .join('&');
          // this.setWebviewStyle()
          const url = encodeURIComponent(`${baseUrl}?${queryString}#/viewDynamic?1=1`);
          const tokenUrl = beisenToken
          wx.$router.jumpH5(`${tokenUrl}&return_url=${url}`)
        } else {
          wx.showToast({
            title: '培训账号未创建',
            icon: 'none'
          })
        }
      })
    }
  }
});