category.js 5.5 KB
import {
  request
} from '../../request/index.js'
const a = getApp()
Page({
  category: [], // 接口返回数据
  data: {
    imagesUrl: a.globalData.baseUrl,
    leftCates: [], // 左分类
    cateList2: [],
    currentIndex: 0, // 被点击的左侧菜单的效果
    scroll_top: 0,
    id: "",
    size: "",
    page: 5,
    sott_time: '',
    sort_price: '',
    key_word: '',
    cate_id: '',
    nominate_switch: '',
    click_num: '',
    childShow: false,
    send: 0,
    it_tue: false,
    newindex: '',
    top_true: true,
    scrollH: '', //滚动高度
    windowH: '', //屏幕高度
    //下一步弹框
    showNext: false
  },

  onShow: function () {

  },

  onLoad: function (options) {
    //  console.log(options);
    this.setData({
      id: options.id || '',
    })
    this.getLeftCates()
    wx.getSystemInfo({
      success: (result) => {
        console.log(result, '屏幕样式')
        this.setData({
          windowH: result.windowHeight
        })
      },
    })
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    console.log(e, '滚动条高度')
  },
  getLeftCates() { // 左分类数据
    let that = this;
    request({
        url: "api/shop/cate",
        data: {
          id: that.data.id
        }
      })
      .then(res => {
        that.setData({
          leftCates: res.data.data,
        })
        that.data.leftCates.forEach((value, indexone) => {
          if (value.id == that.data.id) {
            that.setData({
              cateList2: value.cate2,
              currentIndex: indexone,
              newindex: indexone + 1,
            })
          }
        })
        if (res.data.data.length != 0) {
          if (that.data.id == '') {
            that.setData({
              cateList2: res.data.data[0].cate2,
              id: res.data.data[0].id,
              newindex: 1,
            })
            //  console.log(that.data.id, "id22222");
          }
        }
        //  console.log("分类2 ", that.data.cateList2);
      })
  },
  itemTap(e) { // 点击事件
    //  console.log(e);
    let that = this
    that.setData({
      id: e.currentTarget.dataset.id,
      send: e.currentTarget.dataset.index
    })
    that.getLeftCates()
  },
  onReachBottom: function () {
    let newpage = this.page;
    newpage++;
    this.page = newpage;

  },
  getRecommendList(id) {
    let that = this;
    request({
        url: 'api/shop/listCategory',
        data: {
          id: id,
          page: that.data.page,
          size: 10,
          sott_time: '',
          sort_price: '',
          key_word: '',
          nominate_switch: '',
          click_num: ''
        }
      })
      .then(res => {
        that.setData({
          rightContent: res.data.data
        })
        //  console.log("右侧产品", that.data.rightContent);

      })
  },
  cateDetails(e) { // 去分类列表
    //  console.log(e);
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/cate3List/cate3List?id=' + id
    })
  },
  watchScr(e) {
    console.log(e.detail, '监听滚动')
    this.setData({
      scrollH: e.detail.scrollHeight,
    })
  },

  lower(e) {
    let that = this
    console.log(that.data.scrollH, that.data.windowH, '对比高度')
    // // console.log(e,'向下');
    // if (that.data.scrollH > that.data.windowH + 100 ) {
    //   let leftCates = that.data.leftCates
    //   let indexnum = Number(that.data.leftCates.length)
    //   let id = that.data.id
    //   let send = ''
    //   //  console.log(id,'id');
    //   //  console.log(indexnum,'左侧length')
    //   leftCates.forEach((item, index) => {
    //     if (item.id == id) {
    //       //  console.log(index);
    //       send = index + 1
    //     }
    //   })
    //   //  console.log(send);
    //   if (send != indexnum) {
    //     let idt = leftCates[send].id
    //     that.setData({
    //       id: idt,
    //       it_tue: false,
    //     })
    //     that.getLeftCates()
    //   } else {
    //     that.setData({
    //       id,
    //       it_tue: true,
    //     })

    //   }
    // }
    that.setData({
      showNext: true
    })
  },
  // 前往下一步
  goNext() {
    let that = this
    that.setData({
      showNext: false
    })
    // console.log(e,'向下');
    let leftCates = that.data.leftCates
    let indexnum = Number(that.data.leftCates.length)
    let id = that.data.id
    let send = ''
    //  console.log(id,'id');
    //  console.log(indexnum,'左侧length')
    leftCates.forEach((item, index) => {
      if (item.id == id) {
        //  console.log(index);
        send = index + 1
      }
    })
    //  console.log(send);
    if (send != indexnum) {
      let idt = leftCates[send].id
      that.setData({
        id: idt,
        it_tue: false,
      })
      that.getLeftCates()
    } else {
      that.setData({
        id,
        it_tue: true,
      })

    }
  },
  upper(e) {
    let that = this
    // console.log(e, '向上',);
    console.log(that.data.scrollH, that.data.windowH + 100, '比较')
    if (that.data.scrollH > that.data.windowH + 100) {
      let leftCates = that.data.leftCates
      let id = that.data.id
      let send = ''
      //  console.log(id,'id');
      leftCates.forEach((item, index) => {
        if (item.id == id) {
          if (index != 0) {
            send = index - 1
          } else {
            send = index
          }
        }
      })
      //  console.log(send);
      let idt = leftCates[send].id
      //  console.log(idt);
      that.setData({
        id: idt,
        showNext:false
      })
      that.getLeftCates()
    }
  },
})