zs
litian
2024-09-20 fba71ba1ae5030cb1a20f54f29fb06ad97060297
pages/digitalCourses/index.js
@@ -1,5 +1,10 @@
// pages/digitalCourses/index.js
const app = getApp()
import {
  setSessionGuid,
  setNewView,
  storage
} from "../../assets/js/userAction";
Page({
  /**
@@ -12,7 +17,37 @@
    tabList: [],
    courseList: [],
    searchValue: '',
    activeItem: null
    active: 0,
    activeItem: null,
    loading: false,
    contentLoading: false,
    noData: false,
    shoppingCartGetId: [],
    sorter: {
      value: "*",
      options: [{
          value: "*",
          label: "默认排序",
        },
        {
          value: "nameAsc",
          label: "点击量正序",
        },
        {
          value: "nameDesc",
          label: "点击量倒序",
        },
        {
          value: "timeAsc",
          label: "创建时间正序",
        },
        {
          value: "timeDesc",
          label: "创建时间倒序",
        },
      ],
    },
    sortActive: '',
  },
  /**
@@ -27,7 +62,13 @@
      barHeight: systInfo.statusBarHeight,
      navBarHeight: navBarHeight,
    });
    if (options.courseTypeActive) {
      this.setData({
        active: JSON.parse(options.courseTypeActive),
      });
    }
    this.getCourseTypeListList()
    this.getShoppingCartList()
  },
  /**
@@ -79,8 +120,10 @@
  },
  onTabsChange(event) {
    const value = event.detail.value
    this.setData({
      contentLoading: true
    })
    this.getCourseList(this.data.tabList[value])
  },
@@ -89,12 +132,15 @@
    wx.navigateBack();
  },
  getCourseTypeListList() {
    this.setData({
      loading: true,
    })
    const data = {
      path: '*',
      filterList: [{
        value: 'Normal',
        field: 'state'
      }],
      // filterList: [{
      //   value: 'Normal',
      //   field: 'state'
      // }],
      queryType: '\\',
      searchList: [],
      size: '20',
@@ -106,52 +152,66 @@
      }
    }
    app.MG.store.getStoreChannelList(data).then((res) => {
      // 去除数组最后一个元素
      const newData = res.datas.slice(0, res.datas.length - 1)
      newData.unshift({
        name: "全部",
        pathList: newData[0].pathList[0],
        id: ""
      })
      this.setData({
        tabList: newData,
        activeItem: newData[0]
        loading: false
      })
      this.getCourseList(newData[0])
      if (this.data.active) {
        this.getCourseList(newData[this.data.active])
      } else {
        this.setData({
          activeItem: newData[0],
        })
        this.getCourseList(newData[0])
      }
    })
  },
  getCourseList(item) {
    this.setData({
      noData: false,
    })
    let searchObj = {}
    // let sort = {}
    let sort = {}
    // // 搜索框
    if (this.data.searchValue) {
      searchObj = {
        'Name*': this.data.searchValue.trim()
        // '||subtitle*': searchInputValue.value.trim()
        'Name*': this.data.searchValue.trim(),
        '||isbn*': this.data.searchValue.trim(),
        '||courseLeader*': this.data.searchValue.trim()
      }
    }
    // if (timeSort.value) {
    //     sort = {
    //         CreateDate: timeSort.value || 'Desc'
    //     }
    // } else {
    //     sort = {
    //         ViewCount: nameSort.value || 'Desc'
    //     }
    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'
      }
    }
    //     // 组合的写法 要查询的字段:升序和降序
    //     // Name: "Desc"
    //     //一般的排序传法
    //     // type: "Asc",
    //     // field: "CreateDate",
    // }
    // courseList.value = [];
    const obj = {
      storeInfo: app.config.goodsStore,
      path: item.pathList + '\\' + item.id,
      // path: item.pathList + '\\' + item.id,
      path: item.id ? item.pathList + '\\' + item.id : item.pathList,
      queryType: "*",
      coverSize: {
        width: 260
      },
@@ -163,7 +223,7 @@
        value: 'Normal',
        field: 'state'
      }],
      // sort: sort,
      sort: sort,
      fields: {
        courseLeader: [],
        affiliatedUnit: [],
@@ -174,31 +234,108 @@
      }
    }
    app.MG.store.getProductList(obj).then((res) => {
      console.log(res, '7895')
      if (!res.datas.length) {
        return this.setData({
          noData: true,
          contentLoading: false
        })
      }
      res.datas.forEach(item => {
        item.price = item.price.toFixed(2)
        item.productLinkInfo = JSON.parse(item.productLinkInfo)
      })
      // 排除其他分类里的数据
      this.setData({
        courseList: res.datas
        courseList: res.datas,
        contentLoading: false,
      })
      console.log(this.data.courseList);
    })
  },
  searchBook() {
    console.log(this.data.searchValue);
    this.getCourseList(this.data.activeItem)
  },
  onSort(e) {
    this.setData({
      "sortActive": e.detail.value,
    });
    this.getCourseList(this.data.activeItem);
  },
  courseDetail(e) {
    console.log(e);
    const item = e.currentTarget.dataset.item
    setNewView('productId', item.id)
    this.getCourseTypeListList()
    wx.navigateTo({
      url: '/pages/digitalCourses/digitalCoursesDetails/index?id=' + item.id,
      url: '/pages/digitalCourses/digitalCoursesDetails/index?id=' + item.id + '&path=' + item.idPath
    })
  }
  },
  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)
      }
    }
  },
})