// pages/digitalCourses/index.js const app = getApp() import { setSessionGuid, setNewView, storage } from "../../assets/js/userAction"; Page({ /** * 页面的初始数据 */ data: { barHeight: "", navBarHeight: "", tabPanelstyle: 'display:flex;justify-content:center;align-items:center;', tabList: [], courseList: [], searchValue: '', active: 0, activeItem: null, loading: false, shoppingCartGetId: [], sorter: { value: "*", options: [{ value: "*", label: "默认排序", }, { value: "nameAsc", label: "点击量正序", }, { value: "nameDesc", label: "点击量倒序", }, { value: "timeAsc", label: "创建时间正序", }, { value: "timeDesc", label: "创建时间倒序", }, ], }, sortActive: '' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const systInfo = wx.getSystemInfoSync(); const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息 const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度 this.setData({ barHeight: systInfo.statusBarHeight, navBarHeight: navBarHeight, }); if (options.courseTypeActive) { this.setData({ active: JSON.parse(options.courseTypeActive), }); } this.getCourseTypeListList() this.getShoppingCartList() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, onTabsChange(event) { const value = event.detail.value this.getCourseList(this.data.tabList[value]) }, goBack() { wx.navigateBack(); }, getCourseTypeListList() { this.setData({ loading: true }) const data = { path: '*', filterList: [{ value: 'Normal', field: 'state' }], queryType: '\\', searchList: [], size: '20', start: '0', storeRefCode: app.config.digitalCourses, sort: { type: 'Asc', field: 'LinkOrder' } } app.MG.store.getStoreChannelList(data).then((res) => { // 去除数组最后一个元素 const newData = res.datas.slice(0, res.datas.length - 1) this.setData({ tabList: newData, loading: false }) if (this.data.active) { this.getCourseList(newData[this.data.active]) } else { this.setData({ activeItem: newData[0], }) this.getCourseList(newData[0]) } }) }, getCourseList(item) { let searchObj = {} let sort = {} // // 搜索框 if (this.data.searchValue) { searchObj = { 'Name*': this.data.searchValue.trim(), '||isbn*': searchInputValue.value.trim(), '||courseLeader*': searchInputValue.value.trim() } } if (this.data.sortActive == 'nameAsc') { sort = { ViewCount: 'Asc' } } else if (this.data.sortActive == 'nameDesc') { sort = { ViewCount: 'Desc' } } else if (this.data.sortActive == 'timeAsc') { sort = { CreateDate: 'Asc' } } else if (this.data.sortActive == 'timeDesc') { sort = { CreateDate: 'Desc' } } // // 组合的写法 要查询的字段:升序和降序 const obj = { storeInfo: app.config.goodsStore, path: item.pathList + '\\' + item.id, coverSize: { width: 260 }, paging: { start: 0, size: 99 }, filterList: [{ value: 'Normal', field: 'state' }], sort: sort, fields: { courseLeader: [], affiliatedUnit: [], publishingUnit: [], classHours: [], ViewCount: [], ...searchObj } } app.MG.store.getProductList(obj).then((res) => { res.datas.forEach(item => { item.price = item.price.toFixed(2) }) this.setData({ courseList: res.datas }) }) }, searchBook() { this.getCourseList(this.data.activeItem) }, onSort(e) { this.setData({ "sortActive": e.detail.value, }); console.log(e) this.getCourseList(this.data.activeItem); }, courseDetail(e) { const item = e.currentTarget.dataset.item setNewView('productId', item.id) this.getCourseTypeListList() wx.navigateTo({ url: '/pages/digitalCourses/digitalCoursesDetails/index?id=' + item.id, }) }, getShoppingCartList() { let query = { start: 0, size: 999, filterList: [], searchList: [] } app.MG.store.getShoppingCartProductList(query).then((res) => { let idList = []; res.datas.forEach((item) => { idList.push(item.saleMethod.id) }) this.setData({ shoppingCartGetId: idList }) }) }, addCart(e) { const { item } = e.currentTarget.dataset; wx.showToast({ title: "建设中", icon: 'none', duration: 1000 }) if (wx.getStorageSync(app.config.tokenKey)) { let query = { start: 0, size: 999, filterList: [], searchList: [] } try { if ( this.data.shoppingCartGetId.includes( item.defaultSaleMethodId ) ) { wx.showToast({ title: "该书已在购物车,请勿重复添加空", icon: 'none', duration: 1000 }) } else { let query = { requests: [{ saleMethodId: item.defaultSaleMethodId, storeEventId: null, agentCode: '数字课程' }] } const addRes = app.MG.store.addShoppingCart(query) if (addRes) { wx.showToast({ title: "添加成功", icon: 'success', duration: 1000 }) } this.getShoppingCartList() } } catch (error) { console.error('出错了:', error) } } }, })