productPool.js 9.89 KB
import { API_HOST, SERVICE_HOST } from '../../libs/config';
import { GET, POST } from '../../libs/request';
import { parseBrandListData, cancelFilterSelectState } from '../../utils/productList';
import objectAssign from '../../vendors/object-assign';
import { shouldDiscardTap } from '../../utils/util';
import {
  logEvent,
  YB_PAGE_OPEN_L,
} from '../../libs/analytics.js'

//获取应用实例
let app = getApp();

const screenHeight = app.globalData.systemInfo.screenHeight;
const windowWidth = app.globalData.systemInfo.windowWidth;
const windowHeight = app.globalData.systemInfo.windowHeight;

let PV_ID = new Date().getTime() + '';

Page({
  data: {
    lastTapTimeStamp: 0,

    screenHeight,
    windowHeight,

    from_page_name: '',
    from_page_param: '',
    current_page_name: 'productPool',
    current_page_param: '',

    title: '',
    order: '',
    gender: '',

    defautParam: {},

    filter: {
      item1: {
        key: 'item1',
        name: '默认',
        asc: '',
        isRadio: true,
        selected: true,
      },
      item2: {
        key: 'item2',
        name: '新品',
        asc: 's_t_desc',
        isRadio: true,
        selected: false,
      },
      item3: {
        key: 'item3',
        name: '价格',
        defaults: 'asc',
        asc: 's_p_asc',
        desc: 's_p_desc',
        isRadio: false,
        isAsc: true,
        selected: false,
      },
      item4: {
        key: 'item4',
        name: '折扣',
        defaults: 'desc',
        asc: 'p_d_desc',
        desc: 'p_d_asc',
        isRadio: false,
        isAsc: false,
        selected: false,
      }
    },
    filterGenderItem: {
      key: 'filterGenderItem',
      name: '男生',
      isRadio: true,
      selected: true,
      hiddenGenderSelectView: true,
    },
    genderFilter: {
      item1: {
        key: 'item1',
        name: '男生/BOYS',
        selected: true,
        value: '1,3',
        title: '男生',
      },
      item2: {
        key: 'item2',
        name: '女生/GIRLS',
        selected: false,
        value: '2,3',
        title: '女生',
      }
    },
    list: {
      isLoading: false,
      error: null,
      data: null,
      pageSize: 60,
      currentPage: 0,
      pageCount: 0,
      total: 0,
      endReached: false,
    },
    isShowIndicator: false,
    scrollTop: 0
  },
  resetChannelFilterInfo: function (gender) {
    let filterGenderItem = this.data.filterGenderItem;
    let genderFilter = this.data.genderFilter;
    filterGenderItem.name = gender;
    for (let itemKey in genderFilter) {
      if (genderFilter.hasOwnProperty(itemKey)) {
        if (genderFilter[itemKey].title == gender) {
          genderFilter[itemKey].selected = true;
        } else {
          genderFilter[itemKey].selected = false;
        }
      }
    }
    this.setData({ filterGenderItem, genderFilter });
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    let defautParam = options;

    let title = options.title;
    let productPoolId = options.productPool;

    let from_page_name = options.page_name ? options.page_name : '';
    let from_page_param = options.page_param ? options.page_param : '';
    let current_page_param = productPoolId;
    this.setData({
      title, defautParam, from_page_name, from_page_param, current_page_param
    });
    // this.resetChannelFilterInfo(app.globalData.selectedChannel == 'boy' ? '男生' : '女生');
    this.resetChannelFilterInfo('筛选');

    var pages = getCurrentPages()
    var currentPage = pages[pages.length - 1]
    var url = currentPage.route

    let params = {
      PAGE_NAME: this.data.current_page_name,
      PAGE_PARAM: this.data.current_page_param,
      FROM_PAGE_NAME: this.data.from_page_name,
      FROM_PAGE_PARAM: this.data.from_page_param,
      PV_ID: PV_ID,
      PAGE_PATH: url
    };
    logEvent(YB_PAGE_OPEN_L, params);

    this.fetchListData();

  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
    
    wx.setNavigationBarTitle({
      title: this.data.title,
    });
  },
  onShow: function () {
    // 生命周期函数--监听页面显示

  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    let params = {
      PAGE_NAME: this.data.current_page_name,
      PAGE_PARAM: this.data.current_page_param,
      FROM_PAGE_NAME: this.data.from_page_name,
      FROM_PAGE_PARAM: this.data.from_page_param,
      PV_ID: PV_ID,
    };
    logEvent(YB_PAGE_OPEN_L, params);
  },
  loadMore: function () {
    // 页面上拉触底事件的处理函数

    this.fetchListData();
  },

  //品牌商品列表
  fetchListData: function () {
    if (this.data.list.isLoading || this.data.list.endReached) {
      return;
    }

    let page = this.data.list.currentPage + 1;
    let param = {
      method: 'app.search.pool.productList',
      page,
      limit: this.data.list.pageSize,
      new_filter: '1',
      ...this.data.defautParam,
      order: this.data.order,
    } 

    if (this.data.gender != ''){
      Object.assign(param, { gender: this.data.gender });
    }

    // console.log(param);

    let list = objectAssign(this.data.list, { isLoading: true, });
    this.setData({ list });

    GET(API_HOST, param)
      .then(json => {
        if (!json || !json.code || json.code != 200) {
          let list = objectAssign(this.data.list, { isLoading: false, error: { code: json.code, message: json.message } });
          this.setData({ list });
          return;
        }

        json = json.data;

        let currentPage = json && json.page ? json.page : 1;
        let pageCount = json && json.page_total ? json.page_total : 0;
        let total = json && json.total ? json.total : 0;
        let endReached = currentPage == pageCount;

        let data = json.product_list;
        data = parseBrandListData(data);

        if (currentPage > 1) {
          let oldList = this.data.list.data;
          data = [...oldList, ...data];
        }

        let newList = {
          isLoading: false,
          data,
          currentPage,
          pageCount,
          total,
          endReached,
        };
        let list = objectAssign(this.data.list, newList);
        this.setData({ list });
      })
      .catch(error => {
        let list = objectAssign(this.data.list, { isLoading: false, error, });
        this.setData({ list });
      });
  },

  //点击筛选
  filterTapped: function (event) {
    let data = event.currentTarget.dataset;
    let key = data.key;

    let item = this.data.filter[key];
    // 单选
    if (item.isRadio) {
      // 已选中 结束
      if (item.selected) {
        return;
      }

      // 选中当前filter
      item.selected = true;

      // 遍历所有filter,设置非当前filter为不选中
      let newFilters = cancelFilterSelectState(this.data.filter, key);
      newFilters[key] = item;

      let newList = objectAssign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
      this.setData({ filter: newFilters, order: item.asc, list: newList });

      this.fetchListData();
      // 多选
    } else {

      // 已选中
      if (item.selected) {

        // 修改当前filter排序
        item.isAsc = !item.isAsc;
        let order = item.isAsc ? item.asc : item.desc;
        let newFilters = this.data.filter;
        newFilters[key] = item;

        let newList = objectAssign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
        this.setData({ order, filter: newFilters, list: newList });

        this.fetchListData();
        // 未选中
      } else {

        // 选中当前filter
        item.selected = true;
        // 读取当前filter默认排序
        let defaultKey = item.defaults;
        let order = item[defaultKey];

        item.isAsc = defaultKey == 'asc';

        // 遍历所有filter,设置非当前filter为不选中
        let newFilters = cancelFilterSelectState(this.data.filter, key);
        newFilters[key] = item;

        let newList = objectAssign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
        this.setData({ order, filter: newFilters, list: newList });

        this.fetchListData();
      }
    }
  },

  hiddenGenderFilter: function (event) {
    let filterGenderItem = this.data.filterGenderItem;
    filterGenderItem.hiddenGenderSelectView = true;
    this.setData({ filterGenderItem });
  },
  filterGenderItemTapped: function (event) {
    let filterGenderItem = this.data.filterGenderItem;
    filterGenderItem.hiddenGenderSelectView = !filterGenderItem.hiddenGenderSelectView;
    this.setData({ filterGenderItem });
  },

  maskTouchStart: function (event) {
    this.hiddenGenderFilter();
  },
  genderFilterTapped: function (event) {
    let data = event.currentTarget.dataset;
    let key = data.key;
    let item = this.data.genderFilter[key];

    if (item.selected) {
      this.hiddenGenderFilter();
      return;
    }

    let filterGenderItem = this.data.filterGenderItem;
    filterGenderItem.name = item.title;
    // 选中当前filter
    item.selected = true;
    // 遍历所有filter,设置非当前filter为不选中
    let newFilters = cancelFilterSelectState(this.data.genderFilter, key);
    newFilters[key] = item;

    let newList = objectAssign(this.data.list, { currentPage: 0, pageCount: 0, total: 0, endReached: false, });
    this.setData({ filterGenderItem, genderFilter: newFilters, list: newList, gender: item.value });
    this.hiddenGenderFilter();
    this.fetchListData();
  },

  onScroll: function (e) {
    var { scrollTop } = e.detail;
    var isShow = scrollTop > windowHeight * 2 ? true : false;
    if (isShow != this.data.isShowIndicator) {
      this.setData({
        isShowIndicator: isShow
      });
    }
  },

  backToTop: function () {
    this.setData({
      scrollTop: 0
    })
  }
})