category.js 10.7 KB
import api from '../../common/api.js';
import { shouldDiscardTap } from '../../utils/util';

const app = getApp()

Page({
  data: {
    lastTapTimeStamp: 0,

    isFetching: false,
    categoryError: null,

    currentChannelId: "1",
    categoryList: null,

    currentCateId: 0,
    currentCateValue: '',

    currentSubCateData: null,
    cacheSubCateData: {},
  },

  onLoad: function (options) {
    let channelId = options.channelId;
    if (!channelId) {
      channelId = app.globalData.cid;
    }

    this.setData({ 
      currentChannelId: channelId
    });
    this.fetchCategoryList();
  },

  /**
   * 获取品类列表
   */
  fetchCategoryList: function () {
    let param = {
      method: 'app.sort.get',
      yh_channel: this.data.currentChannelId,
      fromPage: 'aFP_Category'
    }
    this.setData({
      isFetching: true,
    });
    api.get({data:param})
      .then(data => {
        if (!data || !data.code || data.code != 200) {
          this.setData({
            isFetching: false,
            categoryError: { code: data.code, message: data.message },
          });
          return;
        }

        //处理返回结果,1: boy  2: girl 3: kids 解析一级分类
        let category = data.data;
        let categoryList = {
          "1": category.boy,
          "2": category.girl,
          "3": category.kids,
        };

        this.setData({
          isFetching: false,
          categoryList
        }, () => {
          this.updateChannelData(this.data.currentChannelId);
        });
      })
      .catch(error => {
        this.setData({
          isFetching: false,
          categoryError: error,
        });
      });
  },
  /**
   *	获取子分类的KEY值
   **/
  getSubDetailCacheKey: function (channelId, categoryId) {
    let key = "CHA_" + channelId + "_CAT_" + categoryId;
    return key;
  },

  /**
   * 获取品类一级分类的数据信息
   */
  getSubCategoryInfo: function (categoryId) {

    let cacheSubCateData = this.data.cacheSubCateData;

    //检查缓存是否存在数据,如果不存在则获取
    let key = this.getSubDetailCacheKey(this.data.currentChannelId, categoryId);
    let categoryData = cacheSubCateData ? cacheSubCateData[key] : "";

    //命中,则将缓存数据赋给当前分类数据
    if (categoryData) {
      this.setData({
        currentSubCateData: categoryData,
      });
    }
    //未命中,访问网络数据
    else {
      this.fetchSubCategoryInfoFromServer(categoryId);
    }
  },

  /**
   * 从服务器获取一级分类数据信息
   */
  fetchSubCategoryInfoFromServer: function (categoryId) {
    let param = {
      method: 'app.sort.fromBigData',
      channel_id: this.data.currentChannelId,
      msort: categoryId,
      contentCode: 'daaa8b1a5103a30419ebd79c06e6feac',
      fromPage: 'aFP_Category'
    }
    this.setData({
      isFetching: true,
    });
    api.get({data:param})
      .then(data => {
        if (!data || !data.code || data.code != 200) {
          this.setData({
            isFetching: false,
            contentError: { code: data.code, message: data.message },
          });
          return;
        }

        //处理返回的数据,主要是替换图片的宽高信息
        let subCategoryData = data.data;
        //处理分类数据
        if (subCategoryData.sortInfo){
          subCategoryData.sortInfo.map((item, index) => {
            let default_images = item.default_images ? item.default_images.replace(/{width}/g, 144).replace(/{height}/g, 192).replace('{mode}', 2) :'';
            item.default_images = default_images;
          });
        }
        //处理热门推荐数据
        if (subCategoryData.salesInfo) {
          subCategoryData.salesInfo.map((item, index) => {
            let default_images = item.default_images ? item.default_images.replace(/{width}/g, 144).replace(/{height}/g, 192).replace('{mode}', 2) :'';
            item.default_images = default_images;
          });
        }

        //处理热门推荐数据
        if (subCategoryData.brandInfo) {
          subCategoryData.brandInfo.map((item, index) => {
            let brand_ico = item.brand_ico ? item.brand_ico.replace(/{width}/g, 210).replace(/{height}/g, 87).replace('{mode}', 2) : '';
            item.brand_ico = brand_ico;
          });
        }

        //记录缓存数据
        let key = this.getSubDetailCacheKey(this.data.currentChannelId, categoryId);
        let cacheSubCateData = this.data.cacheSubCateData;
        cacheSubCateData[key] = subCategoryData;

        //将分类列表赋值给data
        this.setData({
          isFetching: false,
          currentSubCateData: data.data,
          cacheSubCateData: cacheSubCateData,
        });

      })
      .catch(error => {
        this.setData({
          isFetching: false,
          contentError: error,
        });
      });
  },


  /**
   *  用户点击左边分类
   **/ 
  onCategoryItemSelected: function (event) {
    let currentCateData = event.currentTarget.dataset;
    let currentCateId = currentCateData.id

    // let param = {
    //   L1_CATE_ID: currentCateId,
    // }
    // logEvent(YB_CATEGORY_FL,param);

    let currentChannel = this.data.categoryList[this.data.currentChannelId];
    let cateData = currentChannel && currentChannel[currentCateData.index];

    if (cateData && cateData.sub && cateData.sub.length > 0) {
      this.setData({ currentCateId: currentCateId, currentCateValue: currentCateData.name });
      this.getSubCategoryInfo(currentCateId);
    } else {
      let categoryName = '全部' + cateData.category_name;
      let categoryId = cateData.category_id;
      let parentCategoryId = cateData.parent_id;
      let gender = this.data.currentChannelId == '1' ? '1,3' : '2,3';
      let sort = cateData.relation_parameter && cateData.relation_parameter.sort ? cateData.relation_parameter.sort : ''
      //拼接url
      let queryString = "?gender=" + gender + "&sort=" + sort + "&title=" + categoryName +
        "&categoryId=" + parentCategoryId + "&subCategoryId=" + categoryId;
      tt.navigateTo({
        url: '../productList/index' + queryString
      });
    }
  },

  /**
   *  点击热门推荐
   **/
  onHotRecommendItemTapped: function(event){
    if (shouldDiscardTap(event.timeStamp, this.data.lastTapTimeStamp)) {
      return;
    }
    this.setData({ lastTapTimeStamp: event.timeStamp });

    let url = event.currentTarget.dataset.url;
    if (!url) {
      return;
    }
    let start = url.indexOf('?');
    if (start == -1) {
      return;
    }
    let queryString = url.substring(start);
    if (!queryString) {
      return;
    }
    // let c2id = event.currentTarget.dataset.cate_id;
    // let c1id = this.data.currentCateId;
    // let F_ID = event.currentTarget.dataset.f_id;
    // let param={
    //   F_ID,
    //   L1_CATE_ID: c1id,
    //   L2_CATE_ID:c2id
    // }
    // logEvent(YB_CATEGORY_FL,param);
    tt.navigateTo({
      url: '../productList/index' + queryString
    });

  },

  /**
   *  点击全部xxx
   **/
  onCategoryItemTapped: function (event) {
    if (shouldDiscardTap(event.timeStamp, this.data.lastTapTimeStamp)) {
      return;
    }
    this.setData({ lastTapTimeStamp: event.timeStamp });

    let data = event.currentTarget.dataset.data;
    let categoryName = data.category_name;
    let categoryId = data.category_id;
    let parentCategoryId = data.parent_id;
    let firstProductSkn = data.product_skn;
    let gender = this.data.currentChannelId == '1' ? '1,3' : '2,3';
    let sort = data.relation_parameter && data.relation_parameter.sort ? data.relation_parameter.sort : ''

    // let c1id = this.data.currentCateId;
    // let param = {
    //   F_ID:'1002',
    //   L1_CATE_ID: c1id,
    //   L2_CATE_ID: categoryId
    // }
    // logEvent(YB_CATEGORY_FL, param);

    //拼接url
    let queryString = "?gender=" + gender + "&sort=" + sort + "&title=" + categoryName + 
      "&categoryId=" + parentCategoryId + "&subCategoryId=" + categoryId + "&firstProductSkn=" + firstProductSkn;

    tt.navigateTo({
      url: '../productList/index' + queryString
    });
  },

  /**
   *  点击二级分类MORE
   **/
  onCategoryMoreTapped: function (event) {
    let currentCateId = event.currentTarget.dataset.id;
    let currentCategory = null;

    this.data.categoryList[this.data.currentChannelId].map((item, index) => {
      let category_id = item.category_id;
      if (category_id == currentCateId){
        currentCategory = item;
      }
    });

    if (!currentCategory){
      return;
    }

    let categoryName = currentCategory.category_name;
    let categoryId = currentCategory.category_id;
    let gender = this.data.currentChannelId == '1' ? '1,3' : '2,3';
    let sort = currentCategory.relation_parameter && currentCategory.relation_parameter.sort ? currentCategory.relation_parameter.sort : ''

    // let c1id = this.data.currentCateId;
    // let param = {
    //   F_ID: '1002',
    //   L1_CATE_ID: c1id,
    //   L2_CATE_ID: "0"
    // }
    // logEvent(YB_CATEGORY_FL, param);

    //拼接url
    let queryString = "?gender=" + gender + "&sort=" + sort + "&title=" + categoryName + "&categoryId=" + categoryId + "&subCategoryId=";
    tt.navigateTo({
      url: '../productList/index' + queryString
    });

  },

  /**
   *  点击品牌列表页
   **/
  onBrandItemSelected: function (event) {
    tt.navigateTo({
      url: './brands' + '?currentChannelId=' + this.data.currentChannelId
    });
  },
  /**
   *  点击热门品牌
   **/
  onHotBrandItemTapped: function(event) {
    if (shouldDiscardTap(event.timeStamp, this.data.lastTapTimeStamp)) {
      return;
    }
    this.setData({ lastTapTimeStamp: event.timeStamp });

    let data = event.currentTarget.dataset.data;
    // let c1id = this.data.currentCateId;
    // let param = {
    //   F_ID: '1003',
    //   L1_CATE_ID: c1id,
    //   L2_CATE_ID: data.id
    // }
    // logEvent(YB_CATEGORY_FL, param);
    if (data && data.shop_id) {
      tt.navigateTo({
        url: '../goodsList/brandStore?shopId=' + data.shop_id + '&shopName=' + data.brand_name
      });
    } else {
      tt.navigateTo({
        url: '../goodsList/brand?brandId=' + data.id + '&brandName=' + data.brand_name
      });
    }
    
  },

  switchChannel: function(event) {
    let currentChannelId = event.detail.currentTarget.dataset.channel;
    this.updateChannelData(currentChannelId);
  },

  updateChannelData: function (currentChannelId) {
    if (!this.data.categoryList || typeof(currentChannelId) == "undefined") {
      return;
    }
    let currentChannel = this.data.categoryList[currentChannelId];

    let currentCateId = currentChannel[0].category_id;
    let currentCateValue = currentChannel[0].category_name;
    // 获取品类一级分类的数据信息
    this.getSubCategoryInfo(currentCateId);

    this.setData({
      currentChannelId,
      currentCateId,
      currentCateValue,
    });
  }
})