From d574565fb3e9915f17855a10b5d177c53d22ae2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=8A=E5=B8=86?= <15818603+Osborn-lyf@user.noreply.gitee.com> Date: Thu, 5 Nov 2026 13:54:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=9B=B4=E6=96=B0=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/manifest.json | 10 +- src/pages/add-device/add-device.scss | 13 + src/pages/add-device/index.vue | 28 +- src/pages/add-device/use-add-device.ts | 96 ++++- .../device/components/hot-devices/index.vue | 105 ------ src/pages/device/device.types.ts | 6 - .../components/location-map/index.vue | 337 +++++++++++++++--- .../home-manage/location/home-location.scss | 48 ++- .../device/home-manage/location/index.vue | 76 +++- .../home-manage/location/use-home-location.ts | 240 ++++++++----- src/pages/device/index.vue | 12 +- src/pages/device/use-device.ts | 22 +- src/pages/mine/edit/use-edit-profile.ts | 12 +- src/pages/mine/privacy/index.vue | 2 +- src/static/icon-map-pin.png | Bin 0 -> 369 bytes src/utils/bluetooth.ts | 74 +++- src/utils/static-assets.ts | 2 + 17 files changed, 782 insertions(+), 301 deletions(-) delete mode 100644 src/pages/device/components/hot-devices/index.vue create mode 100644 src/static/icon-map-pin.png diff --git a/src/manifest.json b/src/manifest.json index f7027b4..25f4b54 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -2,7 +2,7 @@ "name" : "虫子智家", "appid" : "__UNI__925D0CC", "description" : "", - "versionName" : "1.2.3", + "versionName" : "1.2.4", "versionCode" : "100", "transformPx" : false, /* 5+App特有相关 */ @@ -70,6 +70,14 @@ "version" : "2", "offline" : false } + }, + "geolocation" : { + "amap" : { + "name" : "", + "__platform__" : [ "ios", "android" ], + "appkey_ios" : "7d9a3ca1ef32775dd5cef9b11962ca49", + "appkey_android" : "6b0279ce51355d222b5023c1124f4581" + } } }, "icons" : { diff --git a/src/pages/add-device/add-device.scss b/src/pages/add-device/add-device.scss index d54f8f7..ef6d468 100644 --- a/src/pages/add-device/add-device.scss +++ b/src/pages/add-device/add-device.scss @@ -10,6 +10,19 @@ width: 100%; } +.add-device-page__checking { + flex: 1; + display: flex; + align-items: center; + justify-content: center; + padding: 120rpx 48rpx; +} + +.add-device-page__checking-text { + font-size: 28rpx; + color: #98a2b3; +} + .add-device-page__body { padding-bottom: calc(32rpx + env(safe-area-inset-bottom)); } diff --git a/src/pages/add-device/index.vue b/src/pages/add-device/index.vue index 995d94e..545f217 100644 --- a/src/pages/add-device/index.vue +++ b/src/pages/add-device/index.vue @@ -1,7 +1,10 @@  diff --git a/src/pages/add-device/use-add-device.ts b/src/pages/add-device/use-add-device.ts index 0f384ea..881974d 100644 --- a/src/pages/add-device/use-add-device.ts +++ b/src/pages/add-device/use-add-device.ts @@ -4,7 +4,9 @@ import { getBluetoothErrorMessage, isBluetoothApiSupported, isBluetoothPermissionError, + probeBluetoothEnabledForBind, promptEnableBluetooth, + subscribeBluetoothAdapterStateChange, } from '@/utils/bluetooth' import { parseId } from '@/utils/device-id' import { readStoredHomeId } from '@/utils/selected-home' @@ -42,7 +44,13 @@ export const BIND_FEATURES: BindFeatureItem[] = [ export function useAddDevice() { const currentStep = 1 const opening = ref(false) + /** 进入页时检测蓝牙,未完成前不闪引导内容 */ + const checkingBluetooth = ref(true) const bindHomeId = ref('') + let routingToScan = false + /** 仅用户去系统设置开启蓝牙后,回页才复检;避免系统弹窗触发 onShow 误跳扫描 */ + let awaitingBluetoothSettings = false + let unsubscribeAdapterState: (() => void) | undefined function applyBindHomeId(homeId?: string | number) { const parsed = parseId(homeId) @@ -55,8 +63,80 @@ export function useAddDevice() { } } + function scanPageUrl() { + const q = bindHomeId.value ? `?homeId=${bindHomeId.value}` : '' + return `/pages/add-device/scan/index${q}` + } + + function goScanPage(replace = false) { + if (routingToScan) return + routingToScan = true + awaitingBluetoothSettings = false + stopWatchBluetoothOn() + const url = scanPageUrl() + if (replace) { + uni.redirectTo({ + url, + fail: () => { + routingToScan = false + uni.navigateTo({ url }) + }, + }) + return + } + uni.navigateTo({ + url, + complete: () => { + routingToScan = false + }, + }) + } + + function stopWatchBluetoothOn() { + unsubscribeAdapterState?.() + unsubscribeAdapterState = undefined + } + + /** 停留在引导页时监听:用户一开蓝牙立即进扫描 */ + function startWatchBluetoothOn() { + if (unsubscribeAdapterState || routingToScan || !isBluetoothApiSupported()) return + unsubscribeAdapterState = subscribeBluetoothAdapterStateChange((res) => { + if (!res.available || routingToScan) return + goScanPage(true) + }) + } + + /** + * 蓝牙已开则直进扫描页;未开则展示本页引导并开始监听开启事件。 + * @param silent 从设置返回时静默复检,不闪「正在检测」 + */ + async function checkBluetoothAndRoute(options?: { silent?: boolean }) { + if (routingToScan) return + const silent = Boolean(options?.silent) + if (!silent) checkingBluetooth.value = true + try { + if (!isBluetoothApiSupported()) { + return + } + await probeBluetoothEnabledForBind() + goScanPage(true) + } catch { + // 未开启 / 无权限:停留在引导页,并监听随后开启 + startWatchBluetoothOn() + } finally { + if (!silent) checkingBluetooth.value = false + } + } + + /** 从系统设置返回时调用:仅在主动去开启后复检;平时靠适配器状态监听即时跳转 */ + function onPageShowAfterSettings() { + if (!awaitingBluetoothSettings || checkingBluetooth.value || routingToScan) return + awaitingBluetoothSettings = false + void checkBluetoothAndRoute({ silent: true }) + } + async function onOpenBluetooth() { - if (opening.value) return + if (opening.value || routingToScan) return opening.value = true try { if (!isBluetoothApiSupported()) { @@ -69,8 +149,7 @@ export function useAddDevice() { return } await assertBluetoothEnabledForScan() - const q = bindHomeId.value ? `?homeId=${bindHomeId.value}` : '' - uni.navigateTo({ url: `/pages/add-device/scan/index${q}` }) + goScanPage(false) } catch (err) { if (isBluetoothPermissionError(err)) { uni.showModal({ @@ -81,6 +160,8 @@ export function useAddDevice() { }) return } + awaitingBluetoothSettings = true + startWatchBluetoothOn() promptEnableBluetooth() } finally { opening.value = false @@ -88,13 +169,22 @@ export function useAddDevice() { } function onBindLater() { + stopWatchBluetoothOn() uni.navigateBack() } + function onPageUnload() { + stopWatchBluetoothOn() + } + return { currentStep, opening, + checkingBluetooth, applyBindHomeId, + checkBluetoothAndRoute, + onPageShowAfterSettings, + onPageUnload, onOpenBluetooth, onBindLater, } diff --git a/src/pages/device/components/hot-devices/index.vue b/src/pages/device/components/hot-devices/index.vue deleted file mode 100644 index 5571222..0000000 --- a/src/pages/device/components/hot-devices/index.vue +++ /dev/null @@ -1,105 +0,0 @@ - - - - - diff --git a/src/pages/device/device.types.ts b/src/pages/device/device.types.ts index 9a3a733..96b9826 100644 --- a/src/pages/device/device.types.ts +++ b/src/pages/device/device.types.ts @@ -4,12 +4,6 @@ export interface RoomTab { locationId?: string | number } -export interface HotDeviceItem { - id: string - name: string - desc: string -} - /** POST /app/device/list 查询参数(按家庭/房间筛选) */ export interface DeviceQueryBody { homeId?: string | number diff --git a/src/pages/device/home-manage/location/components/location-map/index.vue b/src/pages/device/home-manage/location/components/location-map/index.vue index 994be62..2c8527b 100644 --- a/src/pages/device/home-manage/location/components/location-map/index.vue +++ b/src/pages/device/home-manage/location/components/location-map/index.vue @@ -1,7 +1,8 @@ diff --git a/src/pages/device/home-manage/location/use-home-location.ts b/src/pages/device/home-manage/location/use-home-location.ts index f196bdd..4671c25 100644 --- a/src/pages/device/home-manage/location/use-home-location.ts +++ b/src/pages/device/home-manage/location/use-home-location.ts @@ -1,6 +1,6 @@ import { onLoad, onUnload } from '@dcloudio/uni-app' -import type { Ref } from 'vue' -import { ref } from 'vue' +import type { ComponentInternalInstance } from 'vue' +import { computed, ref } from 'vue' import { buildUpdateHomeBody, isValidHomeName, @@ -15,19 +15,16 @@ import { } from '@/utils/amap' import { ensureLocationPermission, getLocationFailHint } from '@/utils/location-permission' import { formatCoordinateAddress } from '@/utils/map-geocode' +import { staticAssets } from '@/utils/static-assets' import { loadHomeGeo, saveHomeGeo } from '../use-home-manage' import type { HomeGeoInfo } from '../home-manage.types' const DEFAULT_LATITUDE = 39.9042 const DEFAULT_LONGITUDE = 116.4074 - -interface LocationMapExpose { - moveTo: (lng: number, lat: number) => void - moveToUserLocation: () => void -} +const MAP_ID = 'home-location-map' interface UseHomeLocationOptions { - locationMapRef?: Ref + pageInstance?: ComponentInternalInstance | null } async function requestUserLocation(): Promise { @@ -53,16 +50,41 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) { const saving = ref(false) const latitude = ref(DEFAULT_LATITUDE) const longitude = ref(DEFAULT_LONGITUDE) + const mapCenterReady = ref(false) const searchKeyword = ref('') const addressText = ref('') const locating = ref(false) const resolvingAddress = ref(false) - const mapReady = ref(false) + const choosingLocation = ref(false) const searchVisible = ref(false) const searchCity = ref('') + let geocodeTimer: ReturnType | null = null - let pendingMapCenter: { lng: number; lat: number } | null = null let disposed = false + let mapCtx: UniApp.MapContext | null = null + + /** + * 根因:App-vue 的 官方不返回经纬度(仅 App-nvue / 微信小程序支持), + * detail 常为 {},因此点击/拖动选点在 App-vue 上本身不可用。 + * 正确做法:用 uni.chooseLocation 系统选点。 + */ + const markers = computed(() => { + let iconPath = '/static/icon-map-pin.png' + // #ifdef H5 + iconPath = staticAssets.iconMapPin + // #endif + return [ + { + id: 1, + latitude: latitude.value, + longitude: longitude.value, + iconPath, + width: 28, + height: 38, + anchor: { x: 0.5, y: 1 }, + }, + ] + }) function clearGeocodeTimer() { if (geocodeTimer) { @@ -71,35 +93,93 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) { } } - function syncMapView(lng: number, lat: number) { - if (!mapReady.value) { - pendingMapCenter = { lng, lat } - return - } - options.locationMapRef?.value?.moveTo(lng, lat) - } - - function onMapReady() { - mapReady.value = true - if (pendingMapCenter) { - options.locationMapRef?.value?.moveTo(pendingMapCenter.lng, pendingMapCenter.lat) - pendingMapCenter = null - } - } - - function applyMapLocation(point: MapLocationPoint) { - latitude.value = point.latitude - longitude.value = point.longitude - addressText.value = formatMapLocation(point) || formatCoordinateAddress(point.latitude, point.longitude) - searchKeyword.value = point.name || point.address || '' - syncMapView(point.longitude, point.latitude) - } - function scheduleAddressResolve() { clearGeocodeTimer() geocodeTimer = setTimeout(() => { + geocodeTimer = null void resolveAddressFromCoords() - }, 400) + }, 350) + } + + function ensureMapContext(): UniApp.MapContext | null { + if (mapCtx) return mapCtx + const proxy = options.pageInstance?.proxy + mapCtx = uni.createMapContext(MAP_ID, proxy as never) + return mapCtx + } + + function onMapReady() { + ensureMapContext() + } + + function setLocation(lat: number, lng: number, address?: string) { + if (!Number.isFinite(lat) || !Number.isFinite(lng)) return + latitude.value = lat + longitude.value = lng + if (address) { + addressText.value = address + return + } + addressText.value = formatCoordinateAddress(lat, lng) + scheduleAddressResolve() + } + + /** 系统地图选点(App-vue 唯一可靠方案) */ + function openChooseLocation() { + if (disposed || choosingLocation.value) return + choosingLocation.value = true + uni.chooseLocation({ + latitude: latitude.value, + longitude: longitude.value, + success: (res) => { + if (disposed) return + const lat = Number(res.latitude) + const lng = Number(res.longitude) + if (!Number.isFinite(lat) || !Number.isFinite(lng)) { + uni.showToast({ title: '未获取到位置', icon: 'none' }) + return + } + const address = [res.name, res.address].filter(Boolean).join(' ') + || formatCoordinateAddress(lat, lng) + setLocation(lat, lng, address) + if (res.name) searchKeyword.value = res.name + if (!res.address && !res.name) scheduleAddressResolve() + }, + fail: (err) => { + const msg = String((err as { errMsg?: string })?.errMsg || '') + // 用户取消不提示 + if (/cancel|取消/i.test(msg)) return + uni.showToast({ title: '选点失败,请重试', icon: 'none' }) + }, + complete: () => { + choosingLocation.value = false + }, + }) + } + + /** + * 点击地图: + * - 小程序等能返回经纬度时直接落点 + * - App-vue detail 为空时改走 chooseLocation + */ + function onMapTap(event: { detail?: { latitude?: number; longitude?: number } }) { + if (disposed) return + const lat = Number(event.detail?.latitude) + const lng = Number(event.detail?.longitude) + if (Number.isFinite(lat) && Number.isFinite(lng)) { + setLocation(lat, lng) + return + } + openChooseLocation() + } + + function applyMapLocation(point: MapLocationPoint) { + setLocation( + point.latitude, + point.longitude, + formatMapLocation(point) || formatCoordinateAddress(point.latitude, point.longitude), + ) + searchKeyword.value = point.name || point.address || '' } async function resolveAddressFromCoords() { @@ -116,50 +196,38 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) { } async function initMapCenter(saved?: HomeGeoInfo) { - if (saved?.latitude != null && saved.longitude != null) { - latitude.value = saved.latitude - longitude.value = saved.longitude - addressText.value = saved.address - syncMapView(saved.longitude, saved.latitude) - return - } - locating.value = true + mapCenterReady.value = false try { - const location = await requestUserLocation() - latitude.value = location.latitude - longitude.value = location.longitude - syncMapView(location.longitude, location.latitude) - if (location.address) { - addressText.value = location.address - } else { - await resolveAddressFromCoords() + if (saved?.latitude != null && saved.longitude != null) { + latitude.value = saved.latitude + longitude.value = saved.longitude + addressText.value = saved.address || '' + if (!addressText.value) await resolveAddressFromCoords() + return + } + + try { + const location = await requestUserLocation() + latitude.value = location.latitude + longitude.value = location.longitude + if (location.address) { + addressText.value = location.address + } else { + await resolveAddressFromCoords() + } + } catch (err) { + latitude.value = DEFAULT_LATITUDE + longitude.value = DEFAULT_LONGITUDE + addressText.value = formatCoordinateAddress(latitude.value, longitude.value) + uni.showToast({ title: getLocationFailHint(err), icon: 'none' }) } - } catch (err) { - latitude.value = DEFAULT_LATITUDE - longitude.value = DEFAULT_LONGITUDE - syncMapView(longitude.value, latitude.value) - addressText.value = formatCoordinateAddress(latitude.value, longitude.value) - uni.showToast({ title: getLocationFailHint(err), icon: 'none' }) } finally { locating.value = false + mapCenterReady.value = true } } - function applyMapCenter(center: { latitude: number; longitude: number }) { - latitude.value = center.latitude - longitude.value = center.longitude - scheduleAddressResolve() - } - - function onRegionChange(e: UniHelper.MapRegionChangeEvent) { - if (disposed) return - if (e.type !== 'end') return - const center = e.detail?.centerLocation - if (!center) return - applyMapCenter(center) - } - function onOpenSearch() { searchVisible.value = true } @@ -177,15 +245,16 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) { locating.value = true try { const location = await requestUserLocation() - latitude.value = location.latitude - longitude.value = location.longitude - syncMapView(location.longitude, location.latitude) - options.locationMapRef?.value?.moveToUserLocation() - if (location.address) { - addressText.value = location.address - } else { - await resolveAddressFromCoords() - } + setLocation( + location.latitude, + location.longitude, + location.address || undefined, + ) + ensureMapContext()?.moveToLocation({ + latitude: location.latitude, + longitude: location.longitude, + }) + if (!location.address) await resolveAddressFromCoords() } catch (err) { uni.showToast({ title: getLocationFailHint(err), icon: 'none' }) } finally { @@ -233,7 +302,7 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) { onUnload(() => { disposed = true clearGeocodeTimer() - pendingMapCenter = null + mapCtx = null }) onLoad((query?: Record) => { @@ -251,16 +320,21 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) { }) return { + MAP_ID, latitude, longitude, + markers, + mapCenterReady, searchKeyword, addressText, locating, resolvingAddress, - onMapReady, - onRegionChange, + choosingLocation, searchVisible, searchCity, + onMapReady, + onMapTap, + openChooseLocation, onOpenSearch, onCloseSearch, onSelectSearchPlace, diff --git a/src/pages/device/index.vue b/src/pages/device/index.vue index d7bee00..e8efba8 100644 --- a/src/pages/device/index.vue +++ b/src/pages/device/index.vue @@ -27,10 +27,11 @@ 加载中... - + ([]) const loading = ref(false) - const hotDevices = ref(DEFAULT_HOT_DEVICES) const bindStepHint = ref(3) const roomDialogVisible = ref(false) const successToastVisible = ref(false) @@ -262,10 +248,6 @@ export function useDevice() { uni.showToast({ title: '扫一扫功能开发中', icon: 'none' }) } - function onGoMall() { - uni.showToast({ title: '商城功能开发中', icon: 'none' }) - } - function onNotify() { uni.navigateTo({ url: '/pages/mine/notify/index' }) } @@ -304,7 +286,6 @@ export function useDevice() { onlineCount, hasDevices, loading, - hotDevices, bindStepHint, roomDialogVisible, successToastVisible, @@ -313,7 +294,6 @@ export function useDevice() { onConfirmAddRoom, onStartBind, onScan, - onGoMall, onNotify, onHomeManage, onSelectHome, diff --git a/src/pages/mine/edit/use-edit-profile.ts b/src/pages/mine/edit/use-edit-profile.ts index a3b692e..8361c44 100644 --- a/src/pages/mine/edit/use-edit-profile.ts +++ b/src/pages/mine/edit/use-edit-profile.ts @@ -16,10 +16,16 @@ const GENDER_OPTIONS = [ { value: '0', label: '未知' }, ] +/** 省 / 市 / 区均展示;直辖市省市区名重复时去重相邻项 */ function formatLocation(province: string, city: string, district: string): string { - const primary = province || city - if (primary && district) return `${primary} · ${district}` - return primary || district || '未设置' + const parts: string[] = [] + for (const part of [province, city, district]) { + const text = part.trim() + if (!text) continue + if (parts[parts.length - 1] === text) continue + parts.push(text) + } + return parts.length ? parts.join(' ') : '未设置' } function createEmptyForm(): ProfileFormState { diff --git a/src/pages/mine/privacy/index.vue b/src/pages/mine/privacy/index.vue index d0fa6ce..a9c7d63 100644 --- a/src/pages/mine/privacy/index.vue +++ b/src/pages/mine/privacy/index.vue @@ -1,6 +1,6 @@