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 密码 mqttPassword: string // MQTT 密码
authType: string // 认证类型 authType: string // 认证类型
locationType: number // 定位类型 locationType: number // 定位类型
latitude: number // 设备位置的纬度 latitude?: number // 设备位置的纬度
longitude: number // 设备位置的经度 longitude?: number // 设备位置的经度
areaId: number // 地区编码 areaId: number // 地区编码
address: string // 设备详细地址 address: string // 设备详细地址
serialNumber: string // 设备序列号 serialNumber: string // 设备序列号

View File

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

View File

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

View File

@ -34,7 +34,7 @@
</el-select> </el-select>
</el-form-item> </el-form-item>
<el-form-item label="设备类型" prop="deviceType"> <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 <el-radio
v-for="dict in getIntDictOptions(DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE)" v-for="dict in getIntDictOptions(DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE)"
:key="dict.value" :key="dict.value"
@ -42,10 +42,10 @@
> >
{{ dict.label }} {{ dict.label }}
</el-radio> </el-radio>
</el-radioTO-group> </el-radio-group>
</el-form-item> </el-form-item>
<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="联网方式" label="联网方式"
prop="netType" prop="netType"
> >