闫增涛
2025-03-28 38cd76c5f05fd55855038e2d393074e27909c63d
pages/digitalCourses/index.js
@@ -1,4 +1,7 @@
// pages/digitalCourses/index.js
import {
  loginInfo
} from '../../assets/js/login';
const app = getApp()
import {
  setSessionGuid,
@@ -17,8 +20,37 @@
    tabList: [],
    courseList: [],
    searchValue: '',
    active: 0,
    activeItem: null,
    loading: false
    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: '',
  },
  /**
@@ -33,7 +65,14 @@
      barHeight: systInfo.statusBarHeight,
      navBarHeight: navBarHeight,
    });
    if (options.courseTypeActive) {
      this.setData({
        active: JSON.parse(options.courseTypeActive),
      });
    }
    this.getCourseTypeListList()
    const token = wx.getStorageSync(app.config.tokenKey)
    if (token) this.getShoppingCartList()
  },
  /**
@@ -86,6 +125,9 @@
  },
  onTabsChange(event) {
    const value = event.detail.value
    this.setData({
      contentLoading: true
    })
    this.getCourseList(this.data.tabList[value])
  },
@@ -94,16 +136,15 @@
    wx.navigateBack();
  },
  getCourseTypeListList() {
    this.setData({
      loading: true
      loading: true,
    })
    const data = {
      path: '*',
      filterList: [{
        value: 'Normal',
        field: 'state'
      }],
      // filterList: [{
      //   value: 'Normal',
      //   field: 'state'
      // }],
      queryType: '\\',
      searchList: [],
      size: '20',
@@ -115,53 +156,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
      },
@@ -173,7 +227,7 @@
        value: 'Normal',
        field: 'state'
      }],
      // sort: sort,
      sort: sort,
      fields: {
        courseLeader: [],
        affiliatedUnit: [],
@@ -184,34 +238,120 @@
      }
    }
    app.MG.store.getProductList(obj).then((res) => {
      console.log(res, '7895')
      if (!res.datas.length) {
        return this.setData({
          noData: true,
          courseList: [],
          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 addFun = () => {
      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)
        }
      }
    }
    const token = wx.getStorageSync(app.config.tokenKey)
    if (token) {
      addFun()
    } else {
      loginInfo(app, (data) => {
        if (data) {
          this.getShoppingCartList()
        }
      })
    }
  },
})