IndexList.js 6.37 KB
// components/CarBrands/CarBrands.js
import {
  indexApi
} from '../../api/api'

import {componentsMixin } from '../../mixins/index'
Component({
  behaviors: [componentsMixin],
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function (n) {
        if (n) setTimeout(() => {
          this.getSideBarInfo()
        }, 0.5 * 1000)
      }
    },
    listType: {
      type: Number,
      value: 0,
      observer: function (n, o) {
        // if (n != o) this.init()
      }
    },
    defaultData: {
      type:String,
      observers(n){
        if(n){
          this.init()
        }
      }
    },
    viewHeight:{
      type:String,
      value:'90%'
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    letter: 'A',
    chooseItem: null
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getCityList() {
      indexApi.getCityList().then(({
        data
      }) => {
        wx.$util.setStorageSync('srcm_shopmall_city_list', data)
        this.setListData(data)
      })
    },
    getBrandList() {
      indexApi.getBrandList().then(({
        data
      }) => {
        wx.$util.setStorageSync('srcm_shopmall_brand_list', data)
        this.setListData(data)
      })
    },
    init() {
      let cityList = wx.$util.getStorageSync('srcm_shopmall_city_list')
      let brandList = wx.$util.getStorageSync('srcm_shopmall_brand_list')
      let newbrandList = wx.$util.getStorageSync('newCarBrandList')
      let {
        listType
      } = this.data
      if (listType == 0) {
        if (cityList) this.setListData(cityList)
        else this.getCityList()
      } else if (listType == 2) {
        let newobj = {}
        newbrandList.forEach(item => {
          newobj[item.letter] = item.list
        })
        this.data.newobj = newobj
        if (newbrandList) this.setListData(newobj)
      } else {
        if (brandList) this.setListData(brandList)
        else this.getBrandList()
      }
    },
    refactorList(data) {
      let {
        defaultData
      } = this.data
      let list = []
      let arr = []
      Object.keys(data).map(letter => {
        data[letter].map(s => {
          if (s.name == defaultData || s.brandName == defaultData) s.active = true
        })
        arr.push(letter)
        list.push({
          letter,
          list: data[letter]
        })
      })
      return {
        list,
        arr
      }
    },
    setListData(data) {
      let {
        list,
        arr
      } = this.refactorList(data)
      this.setData({
        list,
        arr,
        isSearch: false
      })
      setTimeout(() => {
        this.getSideBarInfo()
      }, 500)
    },
    choose(e) {
      let {
        index,
        item,
        ind,
      } = e.currentTarget.dataset
      let {
        list,
        listType
      } = this.data
      list.map(i => {
        i.list.map(c => c.active = false)
      })
      list[index].list[ind].active = true
      let chooseItem
      if (listType == 2) {
        chooseItem = {
          brandId: item.vehicleBrandId,
          brandName: item.vehicleBrandName,
          vehicleSubBrandName:item.vehicleSubBrandName,
          vehicleSubBrandId:item.vehicleSubBrandId
        }
      } else {
        chooseItem = listType == 0 ? {
          cityCode: item.name
        } : {
          brandId: item.brandId,
          brandName: item.brandName
        }
      }
      this.setData({
        list,
        chooseItem
      })
    },
    search(e) {
      let {
        value
      } = e.detail
      let {
        listType
      } = this.data
      let cityList, brandList, newbrandList
      if (listType != 2) {
        cityList = this.refactorList(wx.$util.getStorageSync('srcm_shopmall_city_list')).list
        brandList = this.refactorList(wx.$util.getStorageSync('srcm_shopmall_brand_list')).list
      } else {
        newbrandList = this.refactorList(this.data.newobj).list
      }
      if (!value) {
        this.setData({
          letter: 'A',
          chooseItem: null
        })
        this.init()
        return
      }
      let arr = []
      let data = listType == 0 ? cityList : brandList
      let target = listType == 0 ? 'name' : 'brandName'
      if (listType == 2) {
        data = newbrandList
        target = 'vehicleBrandName'
      }
      data.map(c => {
        let childs_list = []
        c.list.map(i => {
          if (i[target].indexOf(value) != -1) childs_list.push(i)
        })
        if (childs_list.length) {
          c.list = childs_list
          arr.push(c)
        }
      })
      let letterList = Array.from(new Set([...arr.map(e => e.letter)]))
      let searchList = letterList.map(l => {
        let list = []
        arr.map(i => {
          if (l === i.letter) list = i.list
        })
        return {
          letter: l,
          list
        }
      })
      this.setData({
        list: searchList,
        isSearch: true,
        chooseItem: null
      })
    },
    getSideBarInfo() {
      let _this = this
      let {
        arr
      } = this.data
      if (arr.length) {
        wx.$util.getViewInfo(`#letter-${arr[0]}`, res => {
          wx.$util.getViewInfo('#side-bar', _res => {
            _this.setData({
              letterHeight: res.height,
              toTop: res.height + _res.top,
              height: _res.bottom
            })
          }, _this)
        }, _this)
      }
    },
    confirm() {
      let {
        chooseItem
      } = this.data
      if (chooseItem) this.triggerEvent('click', chooseItem)
      this.setData({
        show: false
      })
    },
    start(e) {
      let {
        letter,
      } = e.target.dataset
      this.setData({
        showTip: true,
        letter
      })
    },
    move(e) {
      let {
        letterHeight,
        showTip,
        height,
        toTop,
        arr
      } = this.data
      if (showTip) {
        this.y = e.touches[0].clientY
        this.num = Math.ceil((this.y - toTop) / letterHeight)
        if (this.num < 0) this.num = 0
        if (this.y < height && this.isNum != this.num) {
          clearTimeout(this.time)
          this.isNum = this.num
          this.time = setTimeout(() => {
            this.setData({
              letter: arr[this.num]
            })
          }, 1000 / 60)
        }
      }
    },
    end(e) {
      clearTimeout(this.time)
      this.setData({
        showTip: false
      })
    },
  },
  lifetimes: {
    attached() {
      this.init()
    }
  }
})