index.js 6.64 KB
import api from '../../common/api';
import { parseProductListData } from '../../utils/productListUtil';

Page({
    data:{
      tabs:[
        {
          id:0,
          name: "默认",
          isShowIcon: false,
          data:{},
        },
        {
          id: 1,
          name: "新品",
          isShowIcon: false,
          data:{},
        },
        {
          id: 2,
          name: "人气",
          isShowIcon: false,
          data:{},
        },
        {
          id: 3,
          name: "价格",
          isShowIcon: true,
          iconUrl: "../../images/arrow_normal@2x.png",
          iconType: 1,
          data:{},
        },
        {
          id: 4,
          name: "筛选",
          isShowIcon: true,
          iconUrl: "../../images/sx-ic-h@2x.png",
          iconType: 1,
          data:{},
        }
      ],
      activeIndex: 0,
      contentWidth: 0,
      contentHeight: 0,
      isRefresh: false,
      filterParams: {},
        
    },
    onLoad: function (option) {
      try{
        if(option){
          let categoryId = option.categoryId || '';
          let firstProductSkn = option.firstProductSkn || '';
          let sort = option.sort ? option.sort : '';
          let subCategoryId = option.subCategoryId || '';
          let title = option.title || '';
          let gender = option.gender || '';
          console.log("categoryId:"+categoryId+"==subCategoryId:"+subCategoryId+"==sort:"+sort+"==firstProductSkn:"+firstProductSkn);

          this.data.categoryId = categoryId;
          this.data.subCategoryId = subCategoryId;
          this.data.sort = sort;
          this.data.firstProductSkn = firstProductSkn;
          this.data.title = title;
          this.data.gender = gender;
          
          tt.setNavigationBarTitle({
            title: title
          });
        }
        let systemInfo = tt.getSystemInfoSync();
        let contentWidth = systemInfo.screenWidth;
        // let contentWidth = screenWidth;
        let contentHeight = systemInfo.windowHeight - 44;
        
        this.setData({
          contentWidth,
          contentHeight,
        })
      }catch(e){
        console.log(e.message)
      }
      let tab = this.data.tabs[0];
      // let filterParams = this.data.filterParams;
      this.fechProductList(tab,1);
      // this.fechFilter();
    },
    onShow: function () { 
      if(this.data.isRefresh){
        let filter = this.data.filter;
        if(filter){
          for(var i = 0; i < filter.length; i++){
            let filterItem = filter[i];
            let itemId = filterItem.filterId;
            let selectedIdParam = filterItem.selectedIdParam;
            if(selectedIdParam){
              this.data.filterParams[itemId] = selectedIdParam;
            }
          }
          let current = this.data.activeIndex
          let tab = this.data.tabs[current]; 
          this.fechProductList(tab,1);
        }
      }
    },
    onTabClick: function (e) {

      //获取到点击当前Tab的信息
      let tab = e.detail;
      let currentIndex = tab.id
      if(currentIndex === 4){
        tt.navigateTo({
          url: 'pages/productListFilter/filter?sort=116&subCategoryId=48&categoryId=5&firstProductSkn=51987592',
            success (res) {
                console.log(`${res}`);
            },
            fail (res) {
                console.log("navigateTo调用失败");
            }
        });
      }else {
        let tabData = this.data.tabs[currentIndex].data;
        let page = 1;
        if(tabData && tabData.page){
          page = tabData.page;
        }
        this.fechProductList(tab, page);
      }
      
    },
    fechProductList: function (tab, currentPage) {
      let filterParams = this.data.filterParams;
      let id = tab.id;
      let orderType = tab.iconType;
      let iconUrl = tab.iconUrl;
      let order = "";

      //设置是否显示加载中
      let thatTabs = this.data.tabs;
      thatTabs[id].showLoadMoreView = true;
      this.setData({
        tabs: thatTabs,
      });
        
      if(id === 1){
        order = "s_t_desc";
      }else if(id === 2){
        order = "h_v_desc";
      }else if(id === 3){
        if(orderType && orderType === 1){
          order= "s_p_desc";
        }else{
          order= "s_p_asc";
        }
      }else {
        order = "";
      }
      
      let that = this;
      // that.data.tabs.activeIndex = id;
      let sort = that.data.sort;
      let categoryId = that.data.categoryId;
      let subCategoryId = that.data.subCategoryId;
      let firstProductSkn = that.data.firstProductSkn;
      let title = that.data.title;
      let gender = that.data.gender;
      let param = {
        method: "app.search.category",
        firstProductSkn: firstProductSkn,
        order,
        gender,
        limit: 60,
        page: currentPage,
        sort: sort,
        categoryId,
        subCategoryId,
        title
      }
      let params = Object.assign(filterParams, param);
      api.get({data: params})
          .then(data => {
            let listData = data.data;
            let productList = listData.product_list;
            // let tab = that.tabs[0]
            // tab.data = data;
            let tabs = that.data.tabs;
            for (var index in productList) {
              let skn = productList[index].product_skn;
              let name = productList[index].product_name;
              // console.log("name:"+name+"==skn:"+skn)
            }
            productList = parseProductListData(productList);
            listData.product_list = productList;
            tabs[id].showLoadMoreView = false;
            let isRefresh = this.data.isRefresh;
            if(isRefresh){
              tabs[id].data = listData;
            }else {
              if(tabs[id].data && tabs[id].data.product_list){
                tabs[id].data.page = listData.page;
                tabs[id].data.product_list = tabs[id].data.product_list.concat(productList);
              }else {
                tabs[id].data = listData;
              }
            }
            
            // tablist[id].iconType = orderType;
            // tablist[id].iconUrl = iconUrl;
            // console.log("id:"+id)
          
            console.log('====================================');
            console.log(tabs);
            console.log('====================================');
            that.setData({
              tabs: tabs,
              activeIndex: id,
            });

      });
    },
    onLoadMore: function () {
      let current = this.data.activeIndex
      let tab = this.data.tabs[current];
      let page = tab.data.page + 1;
      let totalPage = tab.data.page_total;
      if(page < totalPage){
        this.fechProductList(tab,page);
      }
      
    },
})