brands.js 12.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

import api from '../../common/api.js';
import router from '../../router/index.js'
// import { getShopID } from '../../utils/util';

// import {
//   logEvent,
//   YB_BRAND_TAB_C,
//   YB_BRAND_FLR_C,
//   YB_SEARCH_C,
//   YB_PAGE_OPEN_L
// } from '../../libs/analytics.js'

let app = getApp();
let bannerW = 640;
let bannerH = 310;
let itemWidth = Math.ceil((app.globalData.systemInfo.windowWidth - 40) / 3);
let itemHeight = itemWidth - 40;
let PV_ID = new Date().getTime() + '';
let imageW = 368;
let imageH = 184
const windowHeight = app.globalData.systemInfo.windowHeight;
const scrollHeight = windowHeight;

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

    itemWidth,
    itemHeight,
    currentChannelId: "1",

    scrollToView: '',
    indicatorDots: true,
    autoplay: true,
    interval: 4000,
    duration: 1000,
    banner: [],
    custom_brands: '',
    switchData:{},

    all_list: [],
    hot_list: [],
    new_list: [],
    all_list_key: [],

    //数据分开放,防止一个数据大于1M,微信报错
    cacheBrandListDataForMen: '',
    cacheBrandListDataForWoMen: '',
    cacheBrandListDataForKid: '',
    //数据分开放,防止一个数据大于1M,微信报错
    cacheBrandResourceDataForMen: '',
    cacheBrandResourceDataForWoMen: '',
    cacheBrandResourceDataForKid: '',

    hotKeyword: {},
    searching: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let channelId = options.currentChannelId;
    if (!channelId) {
      channelId = app.globalData.cid;
    }
    this.setData({ currentChannelId: channelId });
    this.fetchBrandList();
    this.fetchBrandResourceB();
    this.fetchHotKeyword();

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

    // let params = {
    //   PAGE_NAME: 'brands',
    //   PAGE_PARAM: '',
    //   FROM_PAGE_NAME: 'category',
    //   FROM_PAGE_PARAM: '',
    //   PV_ID: PV_ID,
    //   PAGE_PATH: url
    // };
    // logEvent(YB_PAGE_OPEN_L, params);
  },

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

  },

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

  },

  onReachBottom: function () {

  },

  switchBrandFliter: function (event) {
    let currentBrandFliter = event.detail.currentTarget.dataset.brandFliter;
    let brandsTextFilter = this.data.switchData.brandsTextFilter;
    this.setData({
      switchData:{brandsTextFilter,currentBrandFliter}
    });
  },

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

    // let params = {
    //   TAB_ID: currentChannelId,
    // };
    // logEvent(YB_BRAND_TAB_C, params);

  },

  updateChannelData: function (currentChannelId) {
    let scrollToView = 'newsearchSpace';

    this.setData({
      scrollToView,
      currentChannelId,
    });

    this.getBrandsData();
  },

  getBrandsData: function () {

    let brandListData = this.data.cacheBrandListDataForMen;
    let brandResourceData = this.data.cacheBrandResourceDataForMen;

    //检查缓存是否存在数据,如果不存在则获取
    let key = this.data.currentChannelId;

    if(key == 1) {
      brandListData = this.data.cacheBrandListDataForMen ? this.data.cacheBrandListDataForMen : '';
      brandResourceData = this.data.cacheBrandResourceDataForMen ? this.data.cacheBrandResourceDataForMen : '';
    } else if(key == 2) {
      brandListData = this.data.cacheBrandListDataForWoMen ? this.data.cacheBrandListDataForWoMen : '';  
      brandResourceData = this.data.cacheBrandResourceDataForWoMen ? this.data.cacheBrandResourceDataForWoMen : '';

    }else if(key == 3) {
      brandListData = this.data.cacheBrandListDataForKid ? this.data.cacheBrandListDataForKid : ''; 
      brandResourceData = this.data.cacheBrandResourceDataForKid ? this.data.cacheBrandResourceDataForKid : ''; 
    }

    if (brandListData) {
      let all_list = brandListData.all_list;
      let hot_list = brandListData.hot_list;
      let new_list = brandListData.new_list;
      let all_list_key = brandListData.all_list_key;
      this.setData({
        all_list,
        hot_list,
        new_list,
        all_list_key,
      });
    } else {
      this.fetchBrandList();
    }

    if (brandResourceData) {
      let banner = brandResourceData.banner;
      let custom_brands = brandResourceData.custom_brands;
      let brandsTextFilter = brandResourceData.brandsText;
      let currentBrandFliter = 0;
      this.setData({
        banner,
        custom_brands,
        switchData:{brandsTextFilter,currentBrandFliter}
      });
    } else {
      this.fetchBrandResourceB();
    }
    this.fetchHotKeyword();
  },
  
  /**
   * 获取品牌列表
   */
  fetchBrandList: function () {
    let param = {
      method: 'app.brand.allBrandList',
      yh_channel: this.data.currentChannelId,
      fromPage: 'iFP_Brand'
    }
    let that = this;
    api.get({data:param})
      .then(data => {
        let json = parseListResources(data.data);
        let all_list = json.all_list;
        let hot_list = json.hot_list;
        let new_list = json.new_list;
        let all_list_key = json.all_list_key;
        //记录缓存数据
        let key = that.data.currentChannelId;
        if (key == 1) {
          that.setData({
            cacheBrandListDataForMen: json,
          });
        } else if (key == 2) {
          that.setData({
            cacheBrandListDataForWoMen: json,
          });
        } else if (key == 3) {
          that.setData({
            cacheBrandListDataForKid: json,
          });
        }

        that.setData({
          all_list,
          hot_list,
          new_list,
          all_list_key,
        });

        console.log('============2=======================');
        console.log(this.data.all_list);
        console.log(all_list);
        console.log(all_list_key);
        console.log('====================================');
      })
      .catch(error => {

      });
  },

  fetchBrandResourceB: function () {
    let channel = this.data.currentChannelId;
    let content_code = '';
    if (channel == 1) {
      content_code = 'ce6ac059493ec26241a8cbe0bfa1b17a';
    } else if (channel == 2) {
      content_code = 'dac99cdedc1f948e84c145aca561f7d2';
    } else if (channel == 3) {
      content_code = '713784f93f52bb1a7b93916b2bb15510';
    } else if (channel == 4) {
      content_code = 'f1aa914fd23cbcda71a87de6f5416c75';
    }
    let that = this;
    let param = {
      method: 'app.brand.fromBigData',
      channel_id: channel,
      content_code,
      fromPage: 'iFP_Brand'
    }

    api.get({data:param})
      .then(data => {
        let json = parseResourceResources(data.data);
        let banner = json.banner;
        let custom_brands = json.custom_brands;
        let brandsTextFilter = json.brandsText;
        let currentBrandFliter = 0;
        //记录缓存数据
        let key = that.data.currentChannelId;        
        if (key == 1) {
          that.setData({
            cacheBrandResourceDataForMen: json,
          });
        } else if (key == 2) {
          that.setData({
            cacheBrandResourceDataForWoMen: json,
          });
        } else if (key == 3) {
          that.setData({
            cacheBrandResourceDataForKid: json,
          });
        }
        that.setData({
          banner,
          custom_brands,
          switchData:{brandsTextFilter,currentBrandFliter}
        });
      })
      .catch(error => {

      });
  },

  fetchHotKeyword: function () {
    let app = getApp();
    let that = this;
    let param = {
      method: 'app.search.hotBrands',
      uid: app.getUid(),
      sourcePage: 'iFP_SearchProList',
    }

    api.get({data:param})
      .then(data => {
        let hotKeyword = data;
        that.setData({
          hotKeyword,
        });
      })
      .catch(error => {

      });
  },

  activityItemTapped: function (event) {
    let brandItem = event.currentTarget.dataset.brandItem;
    let jumpUrl = brandItem.url;
    router.goUrl(jumpUrl);

    // let F_ID = event.currentTarget.dataset.f_id;
    // let I_INDEX = event.currentTarget.dataset.i_index + 1;
    // let F_INDEX = event.currentTarget.dataset.f_index;
    // let params = {
    //   TAB_ID: this.data.currentChannelId,
    //   F_ID,
    //   F_INDEX,
    //   I_INDEX,
    //   F_URL: jumpUrl,
    //   SHOP_ID: getShopID(jumpUrl),
    // };
    // logEvent(YB_BRAND_FLR_C, params);    
  },
  
  brandItemTapped: function(event) {
    let brandItem = event.detail.currentTarget.dataset.brandItem;

    let shop_id = brandItem.shop_id;
    if (shop_id) {
      tt.navigateTo({
        url: '../goodsList/brandStore?shop_id=' + shop_id + '&shop_name' + brandItem.brand_name + '&page_name=' + 'brands' + '&page_param=' + ''
      });
    }else{
      shop_id = brandItem.id;
      tt.navigateTo({
        url: '../goodsList/brand?brandId=' + shop_id + '&brandName=' + brandItem.brand_name + '&page_name=' + 'brands' + '&page_param=' + ''
      });
    }

    // let F_ID = event.currentTarget.dataset.f_id;
    // let F_INDEX = event.currentTarget.dataset.f_index;
    // let I_INDEX = event.currentTarget.dataset.i_index + 1;
    // let params = {
    //   TAB_ID: this.data.currentChannelId,
    //   F_ID,
    //   F_INDEX,
    //   I_INDEX,
    //   BRD_CATE: this.data.currentBrandFliter,
    //   F_URL: '../goodsList/brandStore?shop_id=' + shop_id + '&shop_name' + brandItem.brand_name,
    //   SHOP_ID: shop_id,
    // };
    // logEvent(YB_BRAND_FLR_C, params);
  },


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

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

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

    // let params = {
    //   SEARCH_POS: 2,
    // };
    // logEvent(YB_SEARCH_C, params);

  },

})


function parseListResources(json) {
  let all_list_old = json.all_list;
  let hot_list = json.hot_list;
  let new_list = json.new_list;

  if (isEmptyObject(all_list_old)) {
    return json;
  }
  let reg = /^[a-zA-Z]*$/;
  let all_list = {};
  let otherList = [];
  let all_list_key = [];

  for (let k in all_list_old) {
    all_list_key.push(k);

    if (reg.test(k)) {
      all_list[k] = all_list_old[k];
    } else {
      Array.prototype.push.apply(otherList, all_list_old[k]);
    }
  }
  all_list['0-9'] = otherList;

  for (var i = 0; i < hot_list.length; i++) {
    let item = hot_list[i];
    item.brand_ico = item.brand_ico.replace(/{width}/g, 368).replace(/{height}/g, 184).replace('{mode}', 2);
  }

  for (var i = 0; i < new_list.length; i++) {
    let item = new_list[i];
    item.brand_ico = item.brand_ico.replace(/{width}/g, 368).replace(/{height}/g, 184).replace('{mode}', 2);
  }
  return {
    all_list,
    hot_list,
    new_list,
    all_list_key,
  };
}

function parseResourceResources(json) {

  let banner = [];
  let custom_brands;
  let brandsText = [];

  brandsText.push({ key: 'default', name: '全部品牌' });

  for (var i = 0; i < json.length; i++) {
    if (json[i].template_name == 'focus') {
      banner = json[i].data;
      for (var j = 0; j < banner.length; j++){
        let item = banner[j];
        item.src = item.src.replace(/{width}/g, bannerW).replace(/{height}/g, bannerH).replace('{mode}', 2);
      }
    }
    if (json[i].template_name == 'custom_brands') {
      custom_brands = json[i].data;
      let list = custom_brands.list;
      for (var j = 0; j < list.length; j++) {
        let item = list[j];
        item.src = item.src.replace(/{width}/g, bannerW).replace(/{height}/g, bannerH).replace('{mode}', 2);
      }
    }
    if (json[i].template_name == 'text') {
      let data = json[i].data;
      let template_id = json[i].template_id;
      let text = data.text;
      brandsText.push({ key: template_id, name: text });
    }
  }

  return {
    banner,
    custom_brands,
    brandsText,
  };
}

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