addressPopup.vue 6.52 KB
<template>
  <van-popup
    v-model="showPopup"
    round
    position="bottom"
    :close-on-click-overlay="false"
    :style="{ height: '60%' }"
    >
    <div class="head-wrap">
      <span @click="lastStep">{{curLevel==1 ? '取消' : '上一步'}}</span>
      <span class="title">请选择{{curLevel==1 ? '小区': curLevel==2 ? '楼栋': '房号'}}</span>
      <span class="confirm" :style="{opacity: isCompleted ? '1' : '0.5'}" @click="confirmFn" >确定</span>
    </div>
    <search placeholder="请输入关键字" :keyword="keyWord" @search="searchkeyWord"></search>
    <div class="list-wrap" ref="list" @scroll.passive ="scroll">
        <div class="item" v-for="(item,index) in searchObj[curLevel].areaList" @click="selectItem(item)" ref="item" :key="item.id">
            <span>{{ item.name }}</span>
            <van-icon v-if="selectInfo[curLevel].id==item.id" name="success icon" />
        </div>
        <div class="item"></div>
    </div>
  </van-popup>
</template>
<script>
import { detailApi } from '@/api'
import {throttle} from '@/utils/index'
export default {
  name: 'addressPopup',
  props: {
    show: {
        type: Boolean,
        default: false
    },
    info: {
        type: Object,
        default: ()=>{}
    }
  },
  data() {
    return {
        showPopup: false,
        keyWord: '',
        searchObj: {
            1: {
                pageSize : 10, 
                pageNum : 1,
                loaded: false,
                loadFinished: false,
                areaList: []
            },
            2: {
                pageSize : 10, 
                pageNum : 1,
                loaded: false,
                loadFinished: false,
                areaList: []
            },
            3: {
                pageSize : 10, 
                pageNum : 1,
                loaded: false,
                loadFinished: false,
                areaList: []
            }
        },
        curLevel: 1,
        selectInfo: {
            1: {
                id: '',
                name: ''
            },
            2: {
                id: '',
                name: ''
            },
            3: {
                id: '',
                name: ''
            }
        },
        areaList: []
    }   
  },
  created() {
    const {building, buildingId, community, communityId, room, roomId, level} = this.info
    this.curLevel = level || 1
    this.selectInfo = {
            1: {
                id: communityId,
                name: community
            },
            2: {
                id: buildingId,
                name: building
            },
            3: {
                id: roomId,
                name: room
            }
        },
    this.showPopup = this.show
    this.getRoomList()
  },
  computed: {
    isCompleted(){
        return !!(this.selectInfo[1].id &&  this.selectInfo[2].id &&  this.selectInfo[3].id)
    }
  },
  methods: {
    searchkeyWord(e){
      this.keyWord = e
      this.searchObj[this.curLevel].pageNum = 1
      this.searchObj[this.curLevel].loadFinished = false
      this.getRoomList()
    },
    lastStep() {
        if(this.curLevel==2 || this.curLevel==3){
            this.curLevel = this.curLevel - 1
            if(!this.searchObj?.[this.curLevel]?.loaded){
                this.getRoomList()
            }
        }else{
            this.$emit('cancelFn')
        }
    },
    selectItem(item){
        const { id, name } = item
        if(id!= this.selectInfo[this.curLevel]?.id && this.curLevel!=3) {
          if(this.curLevel==1){
            this.selectInfo['2'] = {
                id: '',
                name: ''
            }
            this.searchObj['2'] = {
              pageSize : 10, 
              pageNum : 1,
              loaded: false,
              loadFinished: false,
              areaList: []
            }
          }
          this.selectInfo['3'] = {
              id: '',
              name: ''
          }
          this.searchObj['3'] = {
              pageSize : 10, 
              pageNum : 1,
              loaded: false,
              loadFinished: false,
              areaList: []
          }
        }
        this.selectInfo[this.curLevel].id = id
        this.selectInfo[this.curLevel].name = name
        this.keyWord = ''
        if(this.curLevel==3) return;
        this.curLevel = this.curLevel + 1
        this.getRoomList()
    },
    getRoomList(isConcat) {
        const { pageSize , pageNum } = this.searchObj[this.curLevel]
        const params = {
            name: this.keyWord,
            level: this.curLevel,
            firstId: this.selectInfo?.[this.curLevel]?.id,
            pageSize,
            pageNum,
            pid: this.curLevel==1 ? '' : this.selectInfo?.[this.curLevel-1]?.id,
        }
        if(this.searchObj[this.curLevel].loadFinished) return;
        detailApi.getRoomListApi(params).then(res => {
            if(res?.list) {
                const list = res?.list || []
                this.searchObj[this.curLevel].areaList = isConcat ? [...this.searchObj[this.curLevel].areaList, ...list] : list
                const len = this.searchObj?.[this.curLevel]?.areaList?.length || 0
                this.searchObj[this.curLevel].loadFinished = res.total<=len
                this.searchObj[this.curLevel].loaded = true
            }
        })
    },
    scroll: throttle(function (e){
        let scrollBottom = e.target.scrollHeight - e.target.scrollTop - e.target.clientHeight;
        if(scrollBottom < 10){
            console.log("滚动条到底了")
            this.searchObj[this.curLevel].pageNum++
            this.getRoomList(1)
        }
    },500),
    confirmFn(){
        if(!this.isCompleted) return;
        this.$emit('confirm', this.selectInfo)
    }
  }
}
</script>
<style scoped lang="scss">
.van-popup{
    overflow: hidden;;
}
.head-wrap{
  height: 56px;
  padding: 0 32px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #EFEFEF;
  margin-bottom: 8px;
  overflow: hidden;
  span{
    line-height: 22px;
    font-family: PingFangSC-Regular;
    font-weight: 400;
    font-size: 16px;
    color: #666666;
    letter-spacing: 0;
  }
  .title{
    color: #333333;
  }
  .confirm{
    font-weight: 500;
    color: #3975C6;
  }
}
.list-wrap{
  padding: 8px 0 30px;
  max-height: 300px;
  overflow: auto;
  .item{
    padding: 0 20px;
    line-height: 20px;
    font-weight: 400;
    font-family: PingFangSC-Regular;
    font-size: 14px;
    color: #333333;
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
    .icon{
      color: #3A77C4;
      margin-right: 8px;
    }
  }
}
</style>