// packageCourse/pages/course/index.js import { getPublicImage } from '../../../assets/js/middleGround/tool' const app = getApp() Page({ /** * 页面的初始数据 */ data: { visible: false, visibleCart: false, searchKey: '', courseList: [], courseName: '', courseDesc: '', cartList: [], radioVal: 0, selectName: '', selectedBook: null, pageCourse: { page: 1, limit: 10, total: 0, loading: false }, pageBook: { page: 1, limit: 10, total: 999, loading: false }, // 加载参数 bottomLoading: false, isMoreData: false, // 返回顶部 isBackTop: false, setScrollValue: 0, skeletonLoding: true }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.getData() }, // 课程详情 todetail(e) { const { courseId, item } = e.currentTarget.dataset const refCode = item.linkProduct.refCode ? item.linkProduct.refCode : '' if (item.applyState == 'WaitAudit' || item.applyState == 'Reject') { wx.showToast({ title: item.applyState == 'WaitAudit' ? '审核中' : '未通过', duration: 1000, icon: 'none', }) return false } wx.navigateTo({ url: '/packageCourse/pages/course/detail/index?courseId=' + courseId + "&bookRefCode=" + refCode, }) }, // 搜索课程 changeHandle(e) { const { value } = e.detail; this.setData({ searchKey: value, 'pageCourse.page': 1 }); this.getData() }, // 搜索教材 changeHandleBook(e) { const { value } = e.detail; this.setData({ selectName: value, 'pageBook.page': 1 }); this.getTextBook() }, // 获取课程 getData(isReachBottom) { app.MG.edu .getAppCourseList({ start: (this.data.pageCourse.page - 1) * this.data.pageCourse.limit, size: this.data.pageCourse.limit, sort: { type: 'Desc', field: 'CreateDate' }, filterList: [], searchList: this.data.searchKey ? [{ keywords: this.data.searchKey, field: 'Name', compareType: 'Contains' }] : [] }) .then((res) => { let list = res.datas.map((item) => { return { ...item, name: item.name, id: item.id, icon: item.icon && item.icon != 'default' ? getPublicImage(item.icon, 200) : '', introduction: item.description, reason: item.applyReturnMsg ? JSON.parse(item.applyReturnMsg).reason : '-' } }) //触底加载新数据并保留老数据 if (isReachBottom) { list = [...this.data.courseList, ...list] //将新数据加入老数据中 } this.setData({ courseList: list, 'pageCourse.total': res.totalSize, skeletonLoding: false, bottomLoading: false }) }) }, // 获取已购买的教材列表 getTextBook() { const searchData = [{ keywords: 'jsek_digitalTextbooks', field: 'ProductType' }, { keywords: 'jsek_mediaBook', field: 'ProductType' }, { keywords: this.data.selectName, field: 'ProductName' } ] const data = { Size: this.data.pageBook.limit, Start: (this.data.pageBook.page - 1) * this.data.pageBook.limit, sort: { type: 'Desc', field: 'CreateDate' }, searchList: searchData } app.MG.store .getPurchasedProductList(data) .then((res) => { const list = res.datas.map((item) => { return { ...item, icon: item.product.icon ? getPublicImage(item.product.icon, 200) : '' } }) this.setData({ cartList: list, }) }) .catch((err) => { this.setData({ cartList: [], 'pageBook.loading': false }) console.log(err) }) }, // 新建课程 newCourse() { this.setData({ visible: true, 'pageBook.loading': true }) this.getTextBook() }, // 选择课程 selectedBook() { this.setData({ visibleCart: true }) }, // 关闭 cancle() { this.setData({ visible: false }) }, radioCancle() { this.setData({ visibleCart: false }) }, // 单选选择课本 onChange(e) { const { index } = e.currentTarget.dataset this.setData({ radioVal: index }); }, // 单选课本提交 radioSubmit() { const data = this.data.cartList[this.data.radioVal] if (!data.icon) { data.product.icon = 'default' } this.setData({ selectedBook: data, visibleCart: false }); }, onCourseNameInput(e) { this.setData({ courseName: e.detail.value }) }, onCourseDescInput(e) { this.setData({ courseDesc: e.detail.value }) }, // 新建课程 submitCourse() { if (!this.data.courseName) { wx.showToast({ title: '请填写课程名称', duration: 1000, icon: 'none', }) return false } if (!this.data.selectedBook) { wx.showToast({ title: '请选择关联教材', duration: 1000, icon: 'none', }) return false } app.MG.edu .applyNewCourse({ name: this.data.courseName, description: this.data.courseDesc, content: '', icon: this.data.selectedBook.product.icon, type: 'course', config: '', applyData: JSON.stringify({ textBookId: this.data.selectedBook.product.id, textBookName: this.data.selectedBook.product.name }), linkProductId: this.data.selectedBook.product.id, maxClassCount: 999, payPrice: 0 }) .then((res) => { if (res) { this.setData({ visible: false, selectedBook: null, courseDesc: '', courseName: '' }) this.getData() } }) }, // 返回顶部 onToTop() { this.setData({ setScrollValue: 0 }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { if (this._freshing) return this.setData({ 'pageCourse.page': 1, bottomLoading: false, isMoreData: false }) setTimeout(() => { this._freshing = true; this.setData({ triggered: false, }) this.getData(); this._freshing = false }, 500); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { if (this.data.pageCourse.total > this.data.courseList.length) { this.setData({ 'pageCourse.page': this.data.pageCourse.page + 1, bottomLoading: true, isMoreData: false }) } else { this.setData({ bottomLoading: false, isMoreData: true }) return false; } this.getData(true); }, // 监听滚动距离 onPageScroll(e) { if (e && e.scrollTop >= 1000) { this.setData({ isBackTop: true }) } else { this.setData({ isBackTop: false }) } }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })