brands.js 3.44 KB
import { ACTIVITY_HOST, API_HOST } from '../../libs/config';
import { GET, POST } from '../../libs/request';
import { jumpByUrl } from '../../libs/urlRoute';


let app = getApp();
let bannerW = 640;
let bannerH = 310;
let itemWidth = Math.ceil((app.globalData.systemInfo.windowWidth - 40) / 3);
let itemHeight = itemWidth - 40;
const windowHeight = app.globalData.systemInfo.windowHeight;
const scrollHeight = windowHeight;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight,
    itemWidth,
    itemHeight,

    scrollToView: '',

    brandsList: [],
    all_list: [],
    all_list_key: [],

    hotKeyword: {},
    searching: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let res = wx.getSystemInfoSync();
    let scrollHeight = res.screenHeight;
    if (!scrollHeight) {
      scrollHeight = res.windowHeight;
    }

    this.setData({ 
      scrollHeight,
    });
    this.fetchBrandList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  /**
   * 获取品牌列表
   */
  fetchBrandList: function () {
    let that = this;
    GET(ACTIVITY_HOST + '/api/yohood/getBrandList', {})
      .then(data => {

        let brandsList = data_letter_sort(data.data);
        that.setData({
          brandsList,
        });
      })
      .catch(error => {

      });
  },

  activityItemTapped: function (event) {
    let brandItem = event.currentTarget.dataset.brandItem;
    let jumpUrl = brandItem.url;
    jumpByUrl(jumpUrl, 'brands');
  },
  
  brandItemTapped: function(event) {
    let brandItem = event.currentTarget.dataset.brandItem;
    wx.navigateTo({
      url: './brandDetail?brandId=' + brandItem.id + '&brandName=' + brandItem.name
    });
  },


  handlerAlphaTap(e) {
    let { ap } = e.target.dataset;
    this.setData({ scrollToView: ap });
  },

  dismissSearchView(e) {
    this.setData({ searching: false });
  },

  showSearchView(e) {
    this.setData({ searching: true });
  },

  handlerMove(){

  },

})

function data_letter_sort(data) {
  var all_list_old = data;
  var letter_reg = /^[A-Z]$/;
  var list = new Array();
  for (var i = 0; i < all_list_old.length; i++) {
      // 添加 # 分组,用来 存放 首字母不能 转为 大写英文的 数据
      // list['#'] = new Array();
      // 首字母
      let barndName = all_list_old[i].name || '#';
      let letter = barndName.substr(0, 1);
      // 是否 大写 英文 字母
      if (!letter_reg.test(letter)) {
          letter = '#';
      }
      // 创建 字母 分组
      if (!(letter in list)) {
          list[letter] = new Array();
      }
      // 字母 分组 添加 数据
      list[letter].push(all_list_old[i]);
  }
  // 转换 格式 进行 排序;
  var result = new Array();
  for (var key in list) {
    result.push({
          letter: key,
          list: list[key]
      });
  }
  result.sort(function (x, y) {
      return x.letter.charCodeAt(0) - y.letter.charCodeAt(0);
  });
  // # 号分组 放最后
  var last_arr = result[0];
  result.splice(0, 1);
  result.push(last_arr);

  // 转换 数据 {} 格式
  // var json_sort = {}
  // for (var i = 0; i < result.length; i++) {
  //     json_sort[result[i].letter] = result[i].list;
  // }

  return result;
}

function isEmptyObject(obj) {
  for (var key in obj) {
    return false;
  }
  return true;
}