myCourse.js 7.2 KB
// packageStudyCenter/pages/myCourse/myCourse.js
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/dj-car-boutique-work-wechat/'
import api from "~/packageStudyCenter/api"
import commonApi from "~/api/api"
import constantData from "../../constantData"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMG_URL,
    tabList: [{
      label: '待学习',
      num: 0
    }, {
      label: '正在学习',
      num: 0
    }, {
      label: '已学完',
      num: 0
    }, {
	  label: '推荐学习',
	  num: 0
	}],
    tabIndex: 0,
    pageNum: 1,
    pageSize: 10,
    residueObj: {}, // 剩余开单时间
    courseList: [],
    formItems: [],
    tipsBoxHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options?.tabIndex) {
      this.setData({
        tabIndex: Number(options.tabIndex)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getStaticData()
	this.getResidueTime()
    this.getList()
  },

  // 获取我的课程剩余开单时间
  getResidueTime() {
    api.getResidueTimeApi({}).then(res => {
      let { code, data, message } = res
      if (code == 0) {
        this.setData({
          residueObj: data || {}
        }, () => {
          if (data?.remindType > 0) {
            const query = wx.createSelectorQuery()
            query.select('.tips-box').boundingClientRect((res) => {
              const height = res && res?.height
              this.setData({
                tipsBoxHeight: height || 0
              })
            }).exec()
          }
        })
      } else {
        wx.$util.showToast(message)
      }
      this.setData({

      })
    })
  },

  getStaticData() {
    api.getMyCourseCount({}).then(res => {
      let { code, data, message } = res
      if (code == 0) {
        this.setData({
          ['tabList[0].num']: data.unLearnCount,
          ['tabList[1].num']: data.learningCount,
		  ['tabList[2].num']: data.completedCount,
		  ['tabList[3].num']: data.recommendCount
        })
      } else {
        wx.$util.showToast(message)
      }
    })
  },

  changeTab(e) {
    let { index } = e.currentTarget.dataset
    this.setData({
      tabIndex: index,
      pageNum: 1,
      dataList: []
    }, () => {
	  this.getList(true)
	  this.getStaticData()
    })
  },

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

  async getList(scrollTopFlag = false) {
    try {
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      let { pageNum, pageSize, tabIndex, formItems } = this.data
      const params = {
        learnStatus: tabIndex,
        pageNum,
        pageSize
	  }
      this.setData({
        loading: true
      })
      if (pageNum > 1) {
        this.setData({
          loadingList: true
        })
	  }
	  let eventApi = tabIndex == 3 ? 'getRecommendListAPI' : 'getMyCourseList'
      const res = await api[eventApi](params)
      switch (tabIndex) {
        case 0:
          formItems = [{
            key: 'title',
            type: 'title'
          }, {
            label: '课程时长',
            key: 'totalDuration',
            unit: 'min'
          }, {
            type: 'score',
            key: '_totalStar',
            textKey: 'totalStar'
          }, {
            type: 'studied',
            key: 'learnTotal'
          }]
          break
        case 1:
          formItems = [{
            key: 'title',
            type: 'title'
          }, {
            label: '课程时长',
            key: 'totalDuration',
            unit: 'min'
          }, {
            label: '学习进度',
            type: 'progress',
            statusKey: 'completeStatus',
            statusEvent: '0',
            key: '_process'
          }]
          break
        case 2:
          formItems = [{
            key: 'title',
            type: 'title'
          }, {
            label: '课程时长',
            key: 'totalDuration',
            unit: 'min'
          }, {
            label: '学习进度',
            type: 'progress',
            statusKey: 'completeStatus',
            statusEvent: '0',
            key: '_process'
          }, {
            type: 'completeIcon',
            showEventKey: 'completeStatus',
            showEventValue: 1
          }]
          break
      }
      this.setData({
        loadingList: false,
        formItems
      })
      if (res.code != 0) {
        wx.$util.showToast(res.message)
      }
      let newList = res?.data?.list || [];
      newList?.forEach(val => {
        val._totalStar = val.totalStar / 2
        val._imageUrl = constantData.TYPEIMG[val.courseFileType]
        val._tagImage = constantData.IMGTAG[val.planCourseType]
        val._tagText = constantData.TAGTEXT[val.courseFileType]
        val.completeStatus = val.process >= 100 ? 1 : 0 // 学习进度是否已完成
        val._process = `${Math.floor(val.process)}%`
        val._showCompleteIcon = val.process >= 100
      })
      this.setData({
        loading: false
      })
      if (pageNum == 1) {
        this.setData({ courseList: newList })
      } else {
        this.setData({ 
          courseList: this.data.courseList.concat(newList) 
        });
      }
      wx.hideLoading()
      this.setData({ 
        total: res.data?.total,
        nothing: newList.length > 0 
      })
      if (scrollTopFlag) {
        this.setData({scrollTop: 0})
      }
    } catch (error) {
      wx.showToast({
        title: error.message || '请求失败',
        icon: "none",
      })
    }
  },

  // 跳转北森课程详情
  goBeisenDetail(e) {
    let { target } = e.currentTarget.dataset
    wx.showLoading()
    commonApi.findBeiSenSSOToken({}).then(res => {
      wx.hideLoading()
      const beisenToken = res.data;
      if (beisenToken) {
        const COURSE_ID = target?.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'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})