review:【IoT 物联网】设备 location 逻辑

pull/790/MERGE
YunaiV 2025-07-05 11:09:51 +08:00
parent d5a28caa8e
commit cbbb8e54bc
4 changed files with 48 additions and 60 deletions

View File

@ -22,8 +22,8 @@ export interface DeviceVO {
mqttPassword: string // MQTT 密码
authType: string // 认证类型
locationType: number // 定位类型
latitude: number // 设备位置的纬度
longitude: number // 设备位置的经度
latitude?: number // 设备位置的纬度
longitude?: number // 设备位置的经度
areaId: number // 地区编码
address: string // 设备详细地址
serialNumber: string // 设备序列号

View File

@ -1,7 +1,10 @@
<!-- 地图组件基于高德地图实现 -->
<!-- TODO @宗超设备定位使用百度地图 ercharts 更好集成 -->
<template>
<div v-if="props.isWrite">
<el-form ref="form" label-width="120px">
<el-form-item label="定位位置:">
<!-- TODO @TODO @AIstyle 改成 unocss -->
<el-select
style="width: 100%"
v-model="state.address"
@ -41,21 +44,17 @@ import { propTypes } from '@/utils/propTypes'
const emits = defineEmits(['locateChange', 'update:center'])
const state = reactive({
lonLat: '',
lonLat: '', // ,
address: '',
loading: false,
//
latitude: '',
longitude: '',
//
map: null as any,
latitude: '', //
longitude: '', //
map: null as any, //
mapAddrOptions: [] as any[],
//
mapMarker: null as any,
mapMarker: null as any, //
geocoder: null as any,
autoComplete: null as any,
//
tips: []
tips: [] //
})
const props = defineProps({
clickMap: propTypes.bool.def(false),
@ -69,11 +68,12 @@ const loadMap = () => {
state.address = ''
state.latitude = ''
state.longitude = ''
// TODO @AI使 await
AMapLoader.load({
key: import.meta.env.VITE_AMAP_KEY, // WebKey load
key: import.meta.env.VITE_AMAP_KEY, // Web Key load
version: '2.0', // JSAPI 1.4.15
plugins: [
//
//
'AMap.Geocoder',
'AMap.AutoComplete'
]
@ -97,15 +97,15 @@ const initMap = () => {
let mapId = props.isWrite ? 'rwMap' : 'rMap'
state.map = new (window as any).AMap.Map(mapId, {
resizeEnable: true,
zoom: 11, //
zoom: 11, //
keyboardEnable: false
})
}
const initGeocoder = () => {
state.geocoder = new (window as any).AMap.Geocoder({
city: '010', //
radius: 500, //500
city: '010', //
radius: 500, // 500
extensions: 'all'
})
}
@ -118,7 +118,8 @@ const initAutoComplete = () => {
}
const autoSearch = (queryValue: string) => {
state.autoComplete.search(queryValue, (status, result) => {
state.autoComplete.search(queryValue, (_status, result) => {
// TODO @AI
var res = result.tips || [] // result
const temp = ref<any[]>([])
res.forEach((p) => {
@ -133,16 +134,18 @@ const autoSearch = (queryValue: string) => {
})
}
//
// TODO @AI使 /** */
//
const setMarker = (lnglat) => {
// TODO @AI: if return
if (lnglat) {
//
if (state.mapMarker !== null) {
//
state.map.remove(state.mapMarker)
state.lonLat = ''
}
//
state.mapMarker = new (window as any).AMap.Marker({
//
position: new (window as any).AMap.LngLat(lnglat[0], lnglat[1])
})
state.map.add(state.mapMarker) //
@ -152,9 +155,11 @@ const setMarker = (lnglat) => {
}
}
//
// TODO @AI
//
const regeoCode = (lonLat) => {
if (lonLat) {
// TODO @AI
let lnglat = lonLat.split(',')
state.latitude = lnglat[0]
state.longitude = lnglat[1]
@ -162,10 +167,10 @@ const regeoCode = (lonLat) => {
emits('update:center', lonLat)
setMarker(lnglat)
getAddress(lnglat)
console.log('经纬度', lnglat)
}
}
// TODO @AI
//
const getAddress = (lnglat) => {
state.geocoder.getAddress(lnglat, (status, result) => {
@ -176,14 +181,14 @@ const getAddress = (lnglat) => {
}
})
}
// 使访
defineExpose({
regeoCode
})
defineExpose({ regeoCode })
onMounted(() => {
loadMap()
})
// TODO @AIstyle unocss
</script>
<style scoped>

View File

@ -77,8 +77,8 @@
</el-radio>
</el-radio-group>
</el-form-item>
<!-- 只在定位类型为GPS时显示坐标和地图 -->
<template v-if="showCoordinates">
<!-- LocationTypeEnum.MANUAL手动定位 -->
<template v-if="LocationTypeEnum.MANUAL === formData.locationType">
<el-form-item label="设备经度" prop="longitude" type="number">
<el-input
v-model="formData.longitude"
@ -135,14 +135,6 @@ const formType = ref('') // 表单的类型create - 新增update - 修改
const showMap = ref(false) //
const mapRef = ref(null)
//
const showCoordinates = computed(() => {
return (
formData.value.locationType !== LocationTypeEnum.IP &&
formData.value.locationType !== LocationTypeEnum.MODULE
)
})
const formData = ref({
id: undefined,
productId: undefined,
@ -181,7 +173,7 @@ const formRules = reactive({
],
nickname: [
{
validator: (rule, value, callback) => {
validator: (_rule, value: any, callback) => {
if (value === undefined || value === null) {
callback()
return
@ -227,7 +219,7 @@ const open = async (type: string, id?: number) => {
try {
formData.value = await DeviceApi.getDevice(id)
// location
// location
if (formData.value.longitude && formData.value.latitude) {
formData.value.location = `${formData.value.longitude},${formData.value.latitude}`
}
@ -239,20 +231,11 @@ const open = async (type: string, id?: number) => {
showMap.value = true
//
try {
gatewayDevices.value = await DeviceApi.getSimpleDeviceList(DeviceTypeEnum.GATEWAY)
} catch (error) {
console.error('加载网关设备列表失败:', error)
}
gatewayDevices.value = await DeviceApi.getSimpleDeviceList(DeviceTypeEnum.GATEWAY)
//
products.value = await ProductApi.getSimpleProductList()
//
try {
deviceGroups.value = await DeviceGroupApi.getSimpleDeviceGroupList()
} catch (error) {
console.error('加载设备分组列表失败:', error)
}
deviceGroups.value = await DeviceGroupApi.getSimpleDeviceGroupList()
}
defineExpose({ open }) // open
@ -265,15 +248,15 @@ const submitForm = async () => {
formLoading.value = true
try {
const data = formData.value as unknown as DeviceVO
// IPMODULE
if (
data.locationType === LocationTypeEnum.IP ||
data.locationType === LocationTypeEnum.MODULE
) {
//
if (data.locationType !== LocationTypeEnum.MANUAL) {
data.longitude = undefined
data.latitude = undefined
}
// TODO @address areaId
// 1. longitude + latitude + areaId + addressaddress
// 2. IP IotDeviceMessage buildStateUpdateOnline ip areaId https://lbsyun.baidu.com/faq/api?title=webapi/ip-api-base location ok
// 3. haohao
if (formType.value === 'create') {
await DeviceApi.createDevice(data)
@ -304,6 +287,7 @@ const resetForm = () => {
locationType: undefined,
longitude: undefined,
latitude: undefined,
// TODO @location formData
location: '',
groupIds: []
}
@ -333,9 +317,8 @@ const handleLocationChange = (lnglat) => {
const updateLocationFromCoordinates = () => {
//
if (formData.value.longitude && formData.value.latitude) {
// location
// location
formData.value.location = `${formData.value.longitude},${formData.value.latitude}`
console.log('更新location字段:', formData.value.location)
mapRef.value.regeoCode(formData.value.location)
}
}

View File

@ -34,7 +34,7 @@
</el-select>
</el-form-item>
<el-form-item label="设备类型" prop="deviceType">
<el-radioTO-group v-model="formData.deviceType" :disabled="formType === 'update'">
<el-radio-group v-model="formData.deviceType" :disabled="formType === 'update'">
<el-radio
v-for="dict in getIntDictOptions(DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE)"
:key="dict.value"
@ -42,10 +42,10 @@
>
{{ dict.label }}
</el-radio>
</el-radioTO-group>
</el-radio-group>
</el-form-item>
<el-form-item
v-if="[DeviceTypeEnum.DEVICE, DeviceTypeEnum.GATEWAY].includes(formData.deviceType)"
v-if="[DeviceTypeEnum.DEVICE, DeviceTypeEnum.GATEWAY].includes(formData.deviceType!)"
label="联网方式"
prop="netType"
>