forked from drone/command-center-dashboard

罗广辉
2025-04-18 75cdd3cdcec1fc22bddebd6352e5e87e0a8d5d33
src/views/Home/SearchBox.vue
@@ -2,15 +2,15 @@
  <div class="searchBox" ref="searchBoxRef">
    <div class="searchInput">
      <el-select v-model="optionsValue" @change="optionChange" placeholder="请选择查询">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
      <el-input v-model="searchKey"  @focus="handleFocus" placeholder="请输入搜索关键字"></el-input>
         <el-input
            v-model="searchKey"
            @input="handlerInput"
            @focus="handleFocus"
            placeholder="请输入搜索关键字"
         ></el-input>
    </div>
    <div class="searchBtn" @click="searchClick"></div>
    <div class="region">
@@ -30,49 +30,50 @@
  </div>
</template>
<script setup>
import { getRegion } from '@/api/home';
import { searchByKeyword, selectDeviceList } from '@/api/home/common';
import { useStore } from 'vuex';
import _ from 'lodash'
import { getRegion } from '@/api/home'
import { searchByKeyword, selectDeviceList } from '@/api/home/common'
import { useStore } from 'vuex'
import cesiumOperation from '@/utils/cesium-tsa'
const { flyTo } = cesiumOperation()
const store = useStore();
const searchKey = ref('');
const userAreaCode = computed(() => store.state.user.userInfo.detail.areaCode);
const selectedAreaCode = computed(() => store.state.user.selectedAreaCode);
const areaValue = ref('江西省');
const treeValue = ref(userAreaCode.value);
let first = true;
const searchBoxRef = ref(null);
const selectDownRef = ref(null);
const store = useStore()
const searchKey = ref('')
const userAreaCode = computed(() => store.state.user.userInfo.detail.areaCode)
const selectedAreaCode = computed(() => store.state.user.selectedAreaCode)
const areaValue = ref('江西省')
const treeValue = ref(userAreaCode.value)
let first = true
const searchBoxRef = ref(null)
const selectDownRef = ref(null)
function handleNodeClick(data) {
  areaValue.value = data.name;
  store.commit('setSelectedAreaCode', data.code);
   areaValue.value = data.name
   store.commit('setSelectedAreaCode', data.code)
}
const props = {
  label: 'name',
  value: 'code',
  children: 'children',
};
}
const load = async (node, resolve) => {
  if (first) {
    first = false;
    const res = await getRegion(userAreaCode.value);
    const { provinceCode, provinceName } = res?.data?.data?.[0] || {};
    resolve([{ code: provinceCode, name: provinceName }]);
    return;
      first = false
      const res = await getRegion(userAreaCode.value)
      const { provinceCode, provinceName } = res?.data?.data?.[0] || {}
      resolve([{ code: provinceCode, name: provinceName }])
      return
  }
  const code = node?.data?.code || userAreaCode.value;
  if ((node?.data?.regionLevel || 0) > 2) return resolve([]);
   const code = node?.data?.code || userAreaCode.value
   if ((node?.data?.regionLevel || 0) > 2) return resolve([])
  getRegion(code).then(res => {
    resolve(res?.data?.data || []);
  });
};
      resolve(res?.data?.data || [])
   })
}
const optionsValue = ref('1');
const optionsValue = ref('1')
const options = [
  {
    value: '1',
@@ -82,84 +83,87 @@
    value: '2',
    label: '地址',
  },
];
]
const isSelectDown = ref(false);
// 下拉数据列表
const machineNestList = ref([]);
const isSelectDown = ref(false)
// 地址搜索结果
const downList = ref([]);
const position = ref({});
const downList = ref([])
const position = ref({})
// 获取机巢搜索结果
const getDeviceList = async () => {
  const res = await selectDeviceList({nickname: searchKey.value});
  if (res.data.code !== 0) return;
  machineNestList.value = res?.data?.data || [];
  downList.value = res?.data?.data || [];
};
   const res = await selectDeviceList({ nickname: searchKey.value })
   if (res.data.code !== 0) return
   downList.value = res?.data?.data || []
}
// 获取地址搜索结果
const getAddressList = async () => {
  const res = await searchByKeyword(encodeURIComponent(`${areaValue.value}+${searchKey.value}`));
  if (res.data.code !== 0) return;
  downList.value = res?.data?.data.tips || [];
   const res = await searchByKeyword(encodeURIComponent(`${areaValue.value}+${searchKey.value}`))
   if (res.data.code !== 0) return
   downList.value = res?.data?.data.tips || []
  if (downList.value.length > 0) {
    isSelectDown.value = true;
      isSelectDown.value = true
  }
};
}
// 搜索结果
const searchClick = () => {
   const longitude = Number(position.value.longitude)
   const latitude = Number(position.value.latitude)
   flyTo({ longitude, latitude }, 1, 1000)
};
}
// 地址和机巢的切换
const optionChange = () => {
  searchKey.value = '';
  downList.value = [];
};
   searchKey.value = ''
   downList.value = []
   inputSelect()
}
// input对应下拉数据初始化
const inputSelect = () => {
   if (optionsValue.value === '2') {
      getAddressList()
   } else {
      getDeviceList()
   }
}
inputSelect()
// 输入框input事件
const handlerInput = _.debounce(inputSelect, 1000)
// 输入框获取焦点
const handleFocus = () => {
  if (optionsValue.value === '1') {
    isSelectDown.value = true;
    downList.value = machineNestList.value;
   isSelectDown.value = true
  }
};
// 机巢下拉获取值
const selectedValue = item => {
  searchKey.value = item.nickname || item.name;
   searchKey.value = item.nickname || item.name
  if (optionsValue.value === '1') {
    position.value = item;
      position.value = item
  } else {
    position.value = { longitude: item.location.split(',')[0], latitude: item.location.split(',')[1] };
      position.value = { longitude: item.location.split(',')[0], latitude: item.location.split(',')[1] }
  }
  
  isSelectDown.value = false;
};
// 监听搜索关键字变化
watch(searchKey, async (newVal) => {
  if (optionsValue.value === '2') {
    await getAddressList();
  } else {
    await getDeviceList();
   isSelectDown.value = false
  }
}, { immediate: false });
onMounted(() => {
  document.addEventListener('click', handleClickOutside);
});
   document.addEventListener('click', handleClickOutside)
})
onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside);
});
   document.removeEventListener('click', handleClickOutside)
})
const handleClickOutside = (event) => {
const handleClickOutside = event => {
  if (!searchBoxRef.value?.contains(event.target) && !selectDownRef.value?.contains(event.target)) {
    isSelectDown.value = false;
      isSelectDown.value = false
  }
};
}
</script>
<style scoped lang="scss">
.select-down-list {
@@ -170,7 +174,7 @@
  width: 220px;
  height: 256px;
  overflow-y: auto;
  background: linear-gradient( 180deg, #0D3556 0%, #012350 100%);
   background: linear-gradient(180deg, #0d3556 0%, #012350 100%);
  border-radius: 0px 0px 8px 8px;
  border: 1px solid;
  border-image: linear-gradient(180deg, rgba(255, 255, 255, 0), rgba(115, 192, 255, 1)) 1 1;
@@ -182,7 +186,7 @@
  -ms-overflow-style: none;  /* IE and Edge */
  scrollbar-width: none;  /* Firefox */
  .item {
    color: #FFFFFF;
      color: #ffffff;
    height: 32px;
    line-height: 32px;
    text-align: center;