brandSearch.js 3.82 KB

import { getYHStorageSync } from '../../utils/util';

Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  /**
   * 组件的属性列表
   * 用于组件自定义设置
   */
  properties: {
    all_list: {
      type: Object,
      value: {},
    },
    hotKeyword: {
      type: Object,
      value: {},
    }
  },

  /**
   * 私有数据,组件的初始数据
   * 可用于模版渲染
   */
  data: {
     result: [],
     showResult: false,
     hasResult: false,

     searchHistory: [],
  },

  ready: function () {
    
    let searchHistory = getYHStorageSync('searchHistory', 'brandSearch') ? getYHStorageSync('searchHistory','brandSearch') : []; 
    this.setData({
      searchHistory,
    });
  },

  /**
   * 组件的方法列表
   * 更新属性和数据的方法与更新页面数据的方法类似
   */
  methods: {
    //搜索框文字改变
    inputChanged: function (event) {
      let text = event.detail.value;
      text = text.toLowerCase();
      if (text) {
        let all_list = this.properties.all_list;
        let hasResult = false;
        let result = [];
        for (let item of all_list) {
          let list = item.list;
          list && list.map((value, key) => {
            let name = value.name.toLowerCase();
            if (name.includes(text)) { 
              result.push(value);
              hasResult = true;
            }
          })
        }
        this.setData({
          hasResult,
          showResult: true,
          result,
        }) 
      }else {
        this.setData({
          hasResult: false,
          showResult: false,
          result: {},
        }) 
      }

      
    },

    //搜索框获取焦点焦点
    focusChanged: function (event) {
      // console.log('focusChanged')
    },

    //搜索框键盘点击确定
    inputConfirmed: function (event) {
      // console.log('inputConfirmed')

    },

    //热门搜索点击
    hotKeywordItemTapped: function (event) {
      let brandItem = event.currentTarget.dataset.item;
      this.triggerEvent('dismissSearchView', {});
      // console.log(event.currentTarget.dataset)
      wx.navigateTo({
        url: '../goodsList/brand?brandId=' + brandItem.brandId + '&brandName=' + brandItem.brandName + '&page_name=' + 'brands' + '&page_param=' + ''
      });
    },

    
    historyKeywordItemTapped: function (event) {
      let brandItem = event.currentTarget.dataset.item;
      this.triggerEvent('dismissSearchView', {});
      // console.log(event.currentTarget.dataset)
      wx.navigateTo({
        url: '../goodsList/brandStore?shop_id=' + brandItem.shop_id + '&shop_name' + brandItem.brand_name + '&page_name=' + 'brands' + '&page_param=' + ''
      });
    },


    cancelSearch: function (event) {
      this.triggerEvent('dismissSearchView', {});
    },

    clearSearchHistory: function (e) {
      try {
        let searchHistory = [];
        this.setData({
          searchHistory,
        });
        wx.removeStorageSync('searchHistory');
      } catch (e) { }

    },

    search_brandItemTapped: function (e) {

      let brandItem = e.currentTarget.dataset.brandItem;
      let searchHistory = this.data.searchHistory;
      let isHistory = false;
      for (var i = 0; i < searchHistory.length; i++) {
        let item = searchHistory[i];
        if (item.brand_name == brandItem.brand_name) {
          isHistory = true;
        }
      }
      if (!isHistory) {
        searchHistory.push(brandItem);
        this.setData({
          searchHistory,
        });

        try {
          wx.setStorageSync('searchHistory', searchHistory);
        } catch (e) {
        };
      }
      
      this.triggerEvent('dismissSearchView', {});
      wx.navigateTo({
        url: '../goodsList/brandStore?shop_id=' + brandItem.shop_id + '&shop_name' + brandItem.brand_name
      });
    },
  }
})