CarBrand.js 5.54 KB
// packageMine/components/CarBrand/CarBrand.js
import mineApi from '~/api/mineApi/index'
import Util from '~/utils/util'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    popupShow: {
      type: Boolean,
      value: false,
      observer: function (n) {
        if (n) {
          this.getBrandList()
        }
      }
    },
    selectedList: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    selectingList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    dealSelectedList(brandList) {
      return new Promise(resolve => {
        let { selectedList } = this.data
        selectedList.forEach((item, idx) => {
          let brandIdx = brandList.findIndex(k => k.id == item.id)
          let brandChildList = brandList[brandIdx].children
          item.label = item.name
          item.children.forEach((subItem, subIdx) => {
            let cIdx = brandChildList.findIndex(k => k.id == subItem.id)
            brandList[brandIdx].children[cIdx].active = true
            subItem.active = true
            subItem.label = subItem.name
          })
        })
        this.setData({
          selectingList: Util.deepClone(selectedList)
        })
        resolve(brandList)
      })
    },
    getBrandList() {
      let data = wx.$storage.get('car_brand_list')
      if (data) {
        this.dealSelectedList(Util.deepClone(data)).then(brandlist => {
          this.formatData(brandlist)
        })
      } else {
        mineApi.getCarBrandList().then(data => {
          wx.$storage.set('car_brand_list', data)
          this.dealSelectedList(Util.deepClone(data)).then(brandlist => {
            this.formatData(brandlist)
          })
        })
      }
    },
    formatData(data) {
      let letterList = Array.from(new Set([...data.map(e => e.letter)]))
      let brandList = letterList.map(e => {
        let list = []
        data.map(i => {
          if (e === i.letter) {
            list.push(i)
          }
        })
        return {
          letter: e,
          list
        }
      })
      this.setCarData(brandList)
    },
    setCarData(brandList) {
      wx.$api.showLoading('加载中...')
      let count = 0
      let settime = setInterval(() => {
        if (null != brandList[count].list && brandList[count].list.length > 0) {
          this.setData({
            [`brandList[${count}]`]: brandList[count]
          })
        }
        count++
        if (count >= brandList.length) {
          clearInterval(settime)
          this.getLetter()
        }
      }, 10)
    },
    getLetter() {
      wx.hideLoading()
      let { brandList, letter } = this.data
      let arr = []
      if (brandList.length > 0) {
        brandList.forEach(e => {
          arr.push(e.letter)
        })
        this.setData({
          arr,
          letter,
          flag: true
        })
      }
      setTimeout(() => {
        this.getSideBarInfo()
      }, 500)
    },
    getSideBarInfo() {
      let _this = this
      wx.$api.getViewInfo('#letter-A', res => {
        wx.$api.getViewInfo('#side-bar', _res => {
          _this.setData({
            letterHeight: res.height,
            toTop: res.height + _res.top,
            height: _res.bottom
          })
        })
      })
    },
    dealSelectList(index, pidx, cidx) {
      let { selectingList } = this.data
      let pItem = Util.deepClone(this.data.brandList[index].list[pidx])
      // 已选列表中,当前选中的品牌是否存在
      let selectPItemId = selectingList.findIndex(item => item.id == pItem.id)
      // 不存在该品牌,插入;存在该品牌,替换
      if (selectPItemId == -1) {
        selectingList.push(pItem)
        this.setData({
          selectingList
        })
        return true
      }
      this.setData({
        [`selectingList[${selectPItemId}]`]: pItem
      })
    },
    // 选中/取消子品牌
    clickChild(e) {
      let { index, pidx, cidx } = e.currentTarget.dataset
      let childItem = this.data.brandList[index].list[pidx].children[cidx]
      this.setData({
        [`brandList[${index}].list[${pidx}].children[${cidx}].active`]: !childItem.active
      })
      this.dealSelectList(index, pidx, cidx)
    },
    handleConfirm() {
      let { selectingList } = this.data
      let _list = []
      let idx = 0
      selectingList.forEach(item => {
        if (item.children.some(k => k.active == true)) {
          _list.push({
            id: item.id,
            name: item.label,
            logoUrl: item.logoUrl,
            children: []
          })
          item.children.forEach(k => {
            if (k.active) {
              _list[idx].children.push({
                id: k.id,
                name: k.label
              })
            }
          })
          idx = idx + 1
        }
      })
      console.log(_list, '_list处理后---')
      return _list
    },
    start(e) {
      let { letter } = e.target.dataset
      this.setData({
        showTip: true,
        letter
      })
    },
    move(e) {
      let { letterHeight, showTip, height, toTop, arr } = this.data
      let { y, num, isNum } = this
      if (showTip) {
        y = e.touches[0].clientY
        num = Math.ceil((y - toTop) / letterHeight)
        if (num < 0) num = 0
        if (y < height && isNum != num) {
          clearTimeout(this.time)
          isNum = num
          this.time = setTimeout(() => {
            this.setData({
              letter: arr[num]
            })
          }, 1000 / 60)
        }
      }
    },
    end(e) {
      clearTimeout(this.time)
      this.setData({
        showTip: false
      })
    }
  }
})