index.vue 4.14 KB
<template>
  <div class="mapList">
    <div class="list-search">
      <el-input v-model="listVal" suffix-icon="el-icon-search" class="list-input" :placeholder="$t('pholder.shopSearch')" @keyup.enter.native="mallSearch"></el-input>
    </div>
    <el-scrollbar class="citylist-scrollbar" wrap-class="list-scrollbar" :native="false" style="height:100%; max-height: 414px;">
      <div class="mall-list-box">
        <div class="list-wrapper" v-show="JSON.stringify(cityMallList1) !== '{}'">
          <div class="mallBox" v-for="(items,key,index) in cityMallList1" :key="index">
            <p v-if="key ? true : false">
              <span>{{ key }}</span>
            </p>
            <div class="mallListBox">
              <p v-for="(item, index) in items" :key="item.id" @click="mallRouteHandle(item)" :title="item.name">{{ item.name }}</p>
            </div>
          </div>
        </div>
        <div class="list-wrapper" v-show="JSON.stringify(cityMallList2) !== '{}'">
          <div class="mallBox" v-for="(items,key,index) in cityMallList2" :key="index">
            <p v-if="key ? true : false">
              <span>{{ key }}</span>
            </p>
            <div class="mallListBox">
              <p v-for="(item, index) in items" :key="item.id" @click="mallRouteHandle(item)" :title="item.name">{{ item.name }}</p>
            </div>
          </div>
        </div>
        <span v-show="JSON.stringify(cityMallList1)=== '{}' && JSON.stringify(cityMallList2) === '{}'" style="color: #666;">{{ i18n(noListText) }}</span>
      </div>
    </el-scrollbar>
  </div>
</template>

<script>
export default {
  name: 'mallList',
  props: {
    cityMallList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      listVal: '',
      cityMallList1: {},
      cityMallList2: {},
      noListText: ''
    }
  },
  watch: {
    cityMallList: {
      handler(newVal, oldVal) {
        // console.log(newVal, oldVal)
        if (newVal.length) {
          this.buildData(newVal)
        } else {
          this.cityMallList1 = {}
          this.cityMallList2 = {}
          this.noListText = 'loading'
        }
      },
      deep: true
    }
  },
  created() {
    this.noListText = 'loading'
  },
  methods: {
    buildData(sourceData) {
      this.cityMallList1 = {}
      this.cityMallList2 = {}
      let cityList = {}
        , cityMap = JSON.parse(window.sessionStorage.getItem('cityMap'))
        , list1 = {}
        , list2 = {}
      sourceData.forEach(item => {
        item.cityName = cityMap[item.cityId] || ''
        if (!cityList[item.cityName]) {
          cityList[item.cityName] = []
        }
        cityList[item.cityName].push(item)
      })
      Object.keys(cityList).forEach(key => {
        let length1 = 0, length2 = 0
        Object.keys(list1).forEach(key1 => {
          length1 += list1[key1].length
          length1 += 1
        })
        Object.keys(list2).forEach(key2 => {
          length2 += list2[key2].length
          length2 += 1
        })
        if (length1 > length2) {
          list2[key] = cityList[key]
        } else {
          list1[key] = cityList[key]
        }
      })
      setTimeout(() => {
        this.cityMallList1 = list1
        this.cityMallList2 = list2
        if (JSON.stringify(this.cityMallList1) === '{}' && JSON.stringify(this.cityMallList2) === '{}') {
          this.noListText = 'noData'
        }
      }, 500)
    },
    mallSearch() {
      this.noListText = 'loading'
      this.$api.base.mall({
        accountId: this.$cookie.get('accountId'),
        name_like: '%' + (this.listVal).replace(/\s+/g, '') + '%',
        status: 1,
        // // _t: Date.parse(new Date()) / 1000
      })
      .then(res => {
        let result = res.data
        if(result.code == 200) {
          this.buildData(result.data)
        }
      })
      .catch(err => {
        this.cityMallList1 = {}
        this.cityMallList2 = {}
      })
    },
    i18n(title) {
      if (title) {
        let languageTitle = 'echartsTitle.' + title;
        return this.$t(languageTitle)
      }
    },
    mallRouteHandle(item) {
      this.setSessionLocal('path', '/mall')
      this.$emit('setMall', item)
    }
  }
}
</script>

<style>

</style>