Compare commits
1 Commits
2ec91cca26
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d574565fb3 |
@@ -2,7 +2,7 @@
|
|||||||
"name" : "虫子智家",
|
"name" : "虫子智家",
|
||||||
"appid" : "__UNI__925D0CC",
|
"appid" : "__UNI__925D0CC",
|
||||||
"description" : "",
|
"description" : "",
|
||||||
"versionName" : "1.2.3",
|
"versionName" : "1.2.4",
|
||||||
"versionCode" : "100",
|
"versionCode" : "100",
|
||||||
"transformPx" : false,
|
"transformPx" : false,
|
||||||
/* 5+App特有相关 */
|
/* 5+App特有相关 */
|
||||||
@@ -70,6 +70,14 @@
|
|||||||
"version" : "2",
|
"version" : "2",
|
||||||
"offline" : false
|
"offline" : false
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"geolocation" : {
|
||||||
|
"amap" : {
|
||||||
|
"name" : "",
|
||||||
|
"__platform__" : [ "ios", "android" ],
|
||||||
|
"appkey_ios" : "7d9a3ca1ef32775dd5cef9b11962ca49",
|
||||||
|
"appkey_android" : "6b0279ce51355d222b5023c1124f4581"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"icons" : {
|
"icons" : {
|
||||||
|
|||||||
@@ -10,6 +10,19 @@
|
|||||||
width: 100%;
|
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 {
|
.add-device-page__body {
|
||||||
padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
|
padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
<template>
|
<template>
|
||||||
<view class="add-device-page">
|
<view class="add-device-page">
|
||||||
<app-navbar title="添加设备" />
|
<app-navbar title="添加设备" />
|
||||||
<page-scroll-view class="add-device-page__scroll">
|
<view v-if="checkingBluetooth" class="add-device-page__checking">
|
||||||
|
<text class="add-device-page__checking-text">正在检测蓝牙…</text>
|
||||||
|
</view>
|
||||||
|
<page-scroll-view v-else class="add-device-page__scroll">
|
||||||
<view class="add-device-page__body">
|
<view class="add-device-page__body">
|
||||||
<bind-steps :steps="BIND_STEPS" :current="currentStep" />
|
<bind-steps :steps="BIND_STEPS" :current="currentStep" />
|
||||||
<bind-bluetooth-hero />
|
<bind-bluetooth-hero />
|
||||||
@@ -30,16 +33,35 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onLoad } from '@dcloudio/uni-app'
|
import { onLoad, onShow, onUnload } from '@dcloudio/uni-app'
|
||||||
import BindBluetoothHero from './components/bind-bluetooth-hero/index.vue'
|
import BindBluetoothHero from './components/bind-bluetooth-hero/index.vue'
|
||||||
import BindFeatureList from './components/bind-feature-list/index.vue'
|
import BindFeatureList from './components/bind-feature-list/index.vue'
|
||||||
import BindSteps from './components/bind-steps/index.vue'
|
import BindSteps from './components/bind-steps/index.vue'
|
||||||
import { BIND_FEATURES, BIND_STEPS, useAddDevice } from './use-add-device'
|
import { BIND_FEATURES, BIND_STEPS, useAddDevice } from './use-add-device'
|
||||||
|
|
||||||
const { currentStep, opening, applyBindHomeId, onOpenBluetooth, onBindLater } = useAddDevice()
|
const {
|
||||||
|
currentStep,
|
||||||
|
opening,
|
||||||
|
checkingBluetooth,
|
||||||
|
applyBindHomeId,
|
||||||
|
checkBluetoothAndRoute,
|
||||||
|
onPageShowAfterSettings,
|
||||||
|
onPageUnload,
|
||||||
|
onOpenBluetooth,
|
||||||
|
onBindLater,
|
||||||
|
} = useAddDevice()
|
||||||
|
|
||||||
onLoad((query) => {
|
onLoad((query) => {
|
||||||
applyBindHomeId(query?.homeId)
|
applyBindHomeId(query?.homeId)
|
||||||
|
void checkBluetoothAndRoute()
|
||||||
|
})
|
||||||
|
|
||||||
|
onShow(() => {
|
||||||
|
onPageShowAfterSettings()
|
||||||
|
})
|
||||||
|
|
||||||
|
onUnload(() => {
|
||||||
|
onPageUnload()
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import {
|
|||||||
getBluetoothErrorMessage,
|
getBluetoothErrorMessage,
|
||||||
isBluetoothApiSupported,
|
isBluetoothApiSupported,
|
||||||
isBluetoothPermissionError,
|
isBluetoothPermissionError,
|
||||||
|
probeBluetoothEnabledForBind,
|
||||||
promptEnableBluetooth,
|
promptEnableBluetooth,
|
||||||
|
subscribeBluetoothAdapterStateChange,
|
||||||
} from '@/utils/bluetooth'
|
} from '@/utils/bluetooth'
|
||||||
import { parseId } from '@/utils/device-id'
|
import { parseId } from '@/utils/device-id'
|
||||||
import { readStoredHomeId } from '@/utils/selected-home'
|
import { readStoredHomeId } from '@/utils/selected-home'
|
||||||
@@ -42,7 +44,13 @@ export const BIND_FEATURES: BindFeatureItem[] = [
|
|||||||
export function useAddDevice() {
|
export function useAddDevice() {
|
||||||
const currentStep = 1
|
const currentStep = 1
|
||||||
const opening = ref(false)
|
const opening = ref(false)
|
||||||
|
/** 进入页时检测蓝牙,未完成前不闪引导内容 */
|
||||||
|
const checkingBluetooth = ref(true)
|
||||||
const bindHomeId = ref('')
|
const bindHomeId = ref('')
|
||||||
|
let routingToScan = false
|
||||||
|
/** 仅用户去系统设置开启蓝牙后,回页才复检;避免系统弹窗触发 onShow 误跳扫描 */
|
||||||
|
let awaitingBluetoothSettings = false
|
||||||
|
let unsubscribeAdapterState: (() => void) | undefined
|
||||||
|
|
||||||
function applyBindHomeId(homeId?: string | number) {
|
function applyBindHomeId(homeId?: string | number) {
|
||||||
const parsed = parseId(homeId)
|
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() {
|
async function onOpenBluetooth() {
|
||||||
if (opening.value) return
|
if (opening.value || routingToScan) return
|
||||||
opening.value = true
|
opening.value = true
|
||||||
try {
|
try {
|
||||||
if (!isBluetoothApiSupported()) {
|
if (!isBluetoothApiSupported()) {
|
||||||
@@ -69,8 +149,7 @@ export function useAddDevice() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
await assertBluetoothEnabledForScan()
|
await assertBluetoothEnabledForScan()
|
||||||
const q = bindHomeId.value ? `?homeId=${bindHomeId.value}` : ''
|
goScanPage(false)
|
||||||
uni.navigateTo({ url: `/pages/add-device/scan/index${q}` })
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (isBluetoothPermissionError(err)) {
|
if (isBluetoothPermissionError(err)) {
|
||||||
uni.showModal({
|
uni.showModal({
|
||||||
@@ -81,6 +160,8 @@ export function useAddDevice() {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
awaitingBluetoothSettings = true
|
||||||
|
startWatchBluetoothOn()
|
||||||
promptEnableBluetooth()
|
promptEnableBluetooth()
|
||||||
} finally {
|
} finally {
|
||||||
opening.value = false
|
opening.value = false
|
||||||
@@ -88,13 +169,22 @@ export function useAddDevice() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onBindLater() {
|
function onBindLater() {
|
||||||
|
stopWatchBluetoothOn()
|
||||||
uni.navigateBack()
|
uni.navigateBack()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function onPageUnload() {
|
||||||
|
stopWatchBluetoothOn()
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
currentStep,
|
currentStep,
|
||||||
opening,
|
opening,
|
||||||
|
checkingBluetooth,
|
||||||
applyBindHomeId,
|
applyBindHomeId,
|
||||||
|
checkBluetoothAndRoute,
|
||||||
|
onPageShowAfterSettings,
|
||||||
|
onPageUnload,
|
||||||
onOpenBluetooth,
|
onOpenBluetooth,
|
||||||
onBindLater,
|
onBindLater,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,105 +0,0 @@
|
|||||||
<template>
|
|
||||||
<view class="hot-devices">
|
|
||||||
<view class="hot-devices__head">
|
|
||||||
<text class="hot-devices__title">热门设备</text>
|
|
||||||
<text class="hot-devices__link" @tap="$emit('mall')">去商城看看 ></text>
|
|
||||||
</view>
|
|
||||||
<scroll-view class="hot-devices__scroll" scroll-x :show-scrollbar="false">
|
|
||||||
<view class="hot-devices__list">
|
|
||||||
<view v-for="item in items" :key="item.id" class="hot-devices__card">
|
|
||||||
<view class="hot-devices__img" />
|
|
||||||
<text class="hot-devices__name">{{ item.name }}</text>
|
|
||||||
<text class="hot-devices__desc">{{ item.desc }}</text>
|
|
||||||
</view>
|
|
||||||
</view>
|
|
||||||
</scroll-view>
|
|
||||||
</view>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script setup lang="ts">
|
|
||||||
import type { HotDeviceItem } from '../../device.types'
|
|
||||||
|
|
||||||
defineProps<{
|
|
||||||
items: HotDeviceItem[]
|
|
||||||
}>()
|
|
||||||
|
|
||||||
defineEmits<{
|
|
||||||
mall: []
|
|
||||||
}>()
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
|
||||||
.hot-devices {
|
|
||||||
padding: 0 32rpx 32rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hot-devices__head {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: space-between;
|
|
||||||
margin-bottom: 24rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hot-devices__title {
|
|
||||||
font-size: 32rpx;
|
|
||||||
font-weight: 600;
|
|
||||||
color: $color-text;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hot-devices__link {
|
|
||||||
font-size: 26rpx;
|
|
||||||
color: $color-text-secondary;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hot-devices__scroll {
|
|
||||||
width: 100%;
|
|
||||||
white-space: nowrap;
|
|
||||||
|
|
||||||
/* #ifdef APP-PLUS */
|
|
||||||
::-webkit-scrollbar {
|
|
||||||
display: none;
|
|
||||||
width: 0;
|
|
||||||
height: 0;
|
|
||||||
}
|
|
||||||
/* #endif */
|
|
||||||
}
|
|
||||||
|
|
||||||
.hot-devices__list {
|
|
||||||
display: inline-flex;
|
|
||||||
gap: 24rpx;
|
|
||||||
padding-bottom: 8rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hot-devices__card {
|
|
||||||
display: inline-flex;
|
|
||||||
flex-direction: column;
|
|
||||||
width: 300rpx;
|
|
||||||
padding: 24rpx;
|
|
||||||
background: #fff;
|
|
||||||
border-radius: 24rpx;
|
|
||||||
box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, 0.04);
|
|
||||||
vertical-align: top;
|
|
||||||
white-space: normal;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hot-devices__img {
|
|
||||||
width: 100%;
|
|
||||||
height: 200rpx;
|
|
||||||
background: linear-gradient(145deg, #f0f2f5 0%, #e8eaed 100%);
|
|
||||||
border-radius: 16rpx;
|
|
||||||
margin-bottom: 20rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hot-devices__name {
|
|
||||||
font-size: 28rpx;
|
|
||||||
font-weight: 600;
|
|
||||||
color: $color-text;
|
|
||||||
margin-bottom: 8rpx;
|
|
||||||
}
|
|
||||||
|
|
||||||
.hot-devices__desc {
|
|
||||||
font-size: 22rpx;
|
|
||||||
color: $color-text-secondary;
|
|
||||||
line-height: 1.4;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
@@ -4,12 +4,6 @@ export interface RoomTab {
|
|||||||
locationId?: string | number
|
locationId?: string | number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HotDeviceItem {
|
|
||||||
id: string
|
|
||||||
name: string
|
|
||||||
desc: string
|
|
||||||
}
|
|
||||||
|
|
||||||
/** POST /app/device/list 查询参数(按家庭/房间筛选) */
|
/** POST /app/device/list 查询参数(按家庭/房间筛选) */
|
||||||
export interface DeviceQueryBody {
|
export interface DeviceQueryBody {
|
||||||
homeId?: string | number
|
homeId?: string | number
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
<template>
|
<template>
|
||||||
<view class="location-map">
|
<view class="location-map">
|
||||||
|
<!-- 固定屏幕中心定位标;拖动地图 = 选点 -->
|
||||||
<map
|
<map
|
||||||
:id="mapId"
|
id="home-location-map"
|
||||||
class="location-map__canvas"
|
class="location-map__canvas"
|
||||||
provider="amap"
|
provider="amap"
|
||||||
:latitude="mapLatitude"
|
:latitude="mapLatitude"
|
||||||
@@ -10,70 +11,302 @@
|
|||||||
:show-location="true"
|
:show-location="true"
|
||||||
:enable-3d="false"
|
:enable-3d="false"
|
||||||
:show-compass="false"
|
:show-compass="false"
|
||||||
|
:enable-scroll="true"
|
||||||
|
:enable-zoom="true"
|
||||||
@regionchange="onRegionChange"
|
@regionchange="onRegionChange"
|
||||||
/>
|
@updated="onMapUpdated"
|
||||||
|
>
|
||||||
|
<!-- #ifdef APP-PLUS || MP-WEIXIN -->
|
||||||
|
<cover-view class="location-map__pin">
|
||||||
|
<cover-image class="location-map__pin-img" src="/static/icon-map-pin.png" />
|
||||||
|
</cover-view>
|
||||||
|
<cover-view
|
||||||
|
class="location-map__locate"
|
||||||
|
:class="{ 'location-map__locate--loading': locating }"
|
||||||
|
@tap="emit('locate')"
|
||||||
|
>
|
||||||
|
<cover-view class="location-map__locate-icon">◎</cover-view>
|
||||||
|
</cover-view>
|
||||||
|
<!-- #endif -->
|
||||||
|
</map>
|
||||||
|
|
||||||
|
<!-- #ifdef H5 -->
|
||||||
<view class="location-map__pin" aria-hidden="true">
|
<view class="location-map__pin" aria-hidden="true">
|
||||||
<view class="location-map__pin-dot" />
|
<image class="location-map__pin-img" :src="pinIcon" mode="aspectFit" />
|
||||||
<view class="location-map__pin-stick" />
|
|
||||||
</view>
|
</view>
|
||||||
|
<view
|
||||||
|
class="location-map__locate location-map__locate--web"
|
||||||
|
:class="{ 'location-map__locate--loading': locating }"
|
||||||
|
@tap="emit('locate')"
|
||||||
|
>
|
||||||
|
<text class="location-map__locate-icon">◎</text>
|
||||||
|
</view>
|
||||||
|
<!-- #endif -->
|
||||||
</view>
|
</view>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { getCurrentInstance, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
|
import { getCurrentInstance, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
|
||||||
|
import { staticAssets } from '@/utils/static-assets'
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = withDefaults(
|
||||||
latitude: number
|
defineProps<{
|
||||||
longitude: number
|
latitude: number
|
||||||
}>()
|
longitude: number
|
||||||
|
locating?: boolean
|
||||||
|
}>(),
|
||||||
|
{ locating: false },
|
||||||
|
)
|
||||||
|
|
||||||
const emit = defineEmits<{
|
const emit = defineEmits<{
|
||||||
ready: []
|
ready: []
|
||||||
regionchange: [event: UniHelper.MapRegionChangeEvent]
|
locate: []
|
||||||
|
pick: [point: { latitude: number; longitude: number }]
|
||||||
}>()
|
}>()
|
||||||
|
|
||||||
|
const MAP_ID = 'home-location-map'
|
||||||
const instance = getCurrentInstance()
|
const instance = getCurrentInstance()
|
||||||
const mapId = `location-map-${Math.random().toString(36).slice(2, 10)}`
|
const pinIcon = staticAssets.iconMapPin
|
||||||
|
|
||||||
function getMapContext() {
|
|
||||||
return uni.createMapContext(mapId, instance)
|
|
||||||
}
|
|
||||||
const mapLatitude = ref(props.latitude)
|
const mapLatitude = ref(props.latitude)
|
||||||
const mapLongitude = ref(props.longitude)
|
const mapLongitude = ref(props.longitude)
|
||||||
let syncingFromParent = false
|
|
||||||
let destroyed = false
|
let destroyed = false
|
||||||
|
let mapReady = false
|
||||||
|
let dragging = false
|
||||||
|
/** 本次手势开始时的绑定中心,用于拦截「瞬移到北京」 */
|
||||||
|
let dragOrigin = { latitude: props.latitude, longitude: props.longitude }
|
||||||
|
let ignoreRegionUntil = 0
|
||||||
|
let lastEmitKey = ''
|
||||||
|
let mapCtx: UniApp.MapContext | null = null
|
||||||
|
|
||||||
|
type NativeMap = {
|
||||||
|
getCenter?: () => {
|
||||||
|
getLng?: () => number
|
||||||
|
getLat?: () => number
|
||||||
|
longitude?: number
|
||||||
|
latitude?: number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function pointKey(lat: number, lng: number) {
|
||||||
|
return `${lat.toFixed(6)},${lng.toFixed(6)}`
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 常见地图默认中心(天安门附近),不能只用 39.9042/116.4074 一个点 */
|
||||||
|
function isBeijingCore(lat: number, lng: number) {
|
||||||
|
return lat > 39.85 && lat < 40.05 && lng > 116.28 && lng < 116.55
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensureMapContext(): UniApp.MapContext | null {
|
||||||
|
if (mapCtx) return mapCtx
|
||||||
|
const proxy = instance?.proxy
|
||||||
|
if (!proxy) return null
|
||||||
|
mapCtx = uni.createMapContext(MAP_ID, proxy as never)
|
||||||
|
return mapCtx
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNativeMap(): NativeMap | null {
|
||||||
|
// #ifdef APP-PLUS
|
||||||
|
try {
|
||||||
|
const ctx = ensureMapContext() as UniApp.MapContext & { $getAppMap?: () => NativeMap }
|
||||||
|
if (typeof ctx?.$getAppMap === 'function') {
|
||||||
|
return ctx.$getAppMap() || null
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
// #endif
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 同步读取原生可视中心(App 唯一可靠来源) */
|
||||||
|
function readNativeCenterSync(): { latitude: number; longitude: number } | null {
|
||||||
|
const nativeMap = getNativeMap()
|
||||||
|
if (!nativeMap?.getCenter) return null
|
||||||
|
try {
|
||||||
|
const center = nativeMap.getCenter()
|
||||||
|
if (!center) return null
|
||||||
|
const lng = typeof center.getLng === 'function' ? center.getLng() : Number(center.longitude)
|
||||||
|
const lat = typeof center.getLat === 'function' ? center.getLat() : Number(center.latitude)
|
||||||
|
if (!Number.isFinite(lat) || !Number.isFinite(lng)) return null
|
||||||
|
return { latitude: lat, longitude: lng }
|
||||||
|
} catch {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function readEventCenter(detail: Record<string, unknown>): { latitude: number; longitude: number } | null {
|
||||||
|
const nested = detail.centerLocation as { latitude?: number; longitude?: number } | undefined
|
||||||
|
const lat = Number(detail.latitude ?? nested?.latitude)
|
||||||
|
const lng = Number(detail.longitude ?? nested?.longitude)
|
||||||
|
if (!Number.isFinite(lat) || !Number.isFinite(lng)) return null
|
||||||
|
return { latitude: lat, longitude: lng }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 丢弃错误中心:从非北京「瞬移」进北京城区核心(地图未就绪时的默认点)
|
||||||
|
* 小范围拖动(< ~5km)即使进北京也允许
|
||||||
|
*/
|
||||||
|
function isBogusBeijingTeleport(
|
||||||
|
from: { latitude: number; longitude: number },
|
||||||
|
to: { latitude: number; longitude: number },
|
||||||
|
) {
|
||||||
|
const jump = Math.hypot(to.latitude - from.latitude, to.longitude - from.longitude)
|
||||||
|
if (jump < 0.05) return false
|
||||||
|
return isBeijingCore(to.latitude, to.longitude) && !isBeijingCore(from.latitude, from.longitude)
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyCenter(lat: number, lng: number, emitPick: boolean) {
|
||||||
|
if (!Number.isFinite(lat) || !Number.isFinite(lng)) return false
|
||||||
|
if (isBogusBeijingTeleport(
|
||||||
|
{ latitude: mapLatitude.value, longitude: mapLongitude.value },
|
||||||
|
{ latitude: lat, longitude: lng },
|
||||||
|
)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
mapLatitude.value = lat
|
||||||
|
mapLongitude.value = lng
|
||||||
|
if (!emitPick) return true
|
||||||
|
const key = pointKey(lat, lng)
|
||||||
|
if (key === lastEmitKey) return true
|
||||||
|
lastEmitKey = key
|
||||||
|
emit('pick', { latitude: lat, longitude: lng })
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
function pickFromDetailOrNative(detail: Record<string, unknown>) {
|
||||||
|
const fromEvent = readEventCenter(detail)
|
||||||
|
if (fromEvent && !isBogusBeijingTeleport(dragOrigin, fromEvent)) {
|
||||||
|
applyCenter(fromEvent.latitude, fromEvent.longitude, true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// #ifdef APP-PLUS
|
||||||
|
// 必须在 end 回调里同步读原生中心,抢在框架用旧绑定重绘之前
|
||||||
|
const fromNative = readNativeCenterSync()
|
||||||
|
if (fromNative && !isBogusBeijingTeleport(dragOrigin, fromNative)) {
|
||||||
|
applyCenter(fromNative.latitude, fromNative.longitude, true)
|
||||||
|
}
|
||||||
|
// 禁止 getCenterLocation:App 常返回绑定值或默认北京
|
||||||
|
return
|
||||||
|
// #endif
|
||||||
|
|
||||||
|
// #ifndef APP-PLUS
|
||||||
|
const fromNative = readNativeCenterSync()
|
||||||
|
if (fromNative && !isBogusBeijingTeleport(dragOrigin, fromNative)) {
|
||||||
|
applyCenter(fromNative.latitude, fromNative.longitude, true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ensureMapContext()?.getCenterLocation({
|
||||||
|
success: (res) => {
|
||||||
|
if (destroyed) return
|
||||||
|
const lat = Number(res?.latitude)
|
||||||
|
const lng = Number(res?.longitude)
|
||||||
|
const point = { latitude: lat, longitude: lng }
|
||||||
|
if (!isBogusBeijingTeleport(dragOrigin, point)) {
|
||||||
|
applyCenter(lat, lng, true)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
// #endif
|
||||||
|
}
|
||||||
|
|
||||||
|
function onRegionChange(event: Record<string, unknown>) {
|
||||||
|
if (Date.now() < ignoreRegionUntil) return
|
||||||
|
const detail = (event.detail || {}) as Record<string, unknown>
|
||||||
|
const type = String(event.type || detail.type || '')
|
||||||
|
|
||||||
|
if (type === 'begin') {
|
||||||
|
dragging = true
|
||||||
|
dragOrigin = { latitude: mapLatitude.value, longitude: mapLongitude.value }
|
||||||
|
ensureMapContext()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// end,或 App 上偶发无 type
|
||||||
|
if (type === 'end' || type === '') {
|
||||||
|
dragging = false
|
||||||
|
pickFromDetailOrNative(detail)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onMapUpdated() {
|
||||||
|
if (!mapReady) {
|
||||||
|
mapReady = true
|
||||||
|
emit('ready')
|
||||||
|
}
|
||||||
|
ensureMapContext()
|
||||||
|
}
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => [props.latitude, props.longitude],
|
() => [props.latitude, props.longitude] as const,
|
||||||
([lat, lng]) => {
|
([lat, lng]) => {
|
||||||
if (syncingFromParent) return
|
if (dragging) return
|
||||||
|
if (!Number.isFinite(lat) || !Number.isFinite(lng)) return
|
||||||
|
if (
|
||||||
|
Math.abs(mapLatitude.value - lat) < 1e-7
|
||||||
|
&& Math.abs(mapLongitude.value - lng) < 1e-7
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 父级(搜索/定位)写入时也拦截异常北京瞬移
|
||||||
|
if (isBogusBeijingTeleport(
|
||||||
|
{ latitude: mapLatitude.value, longitude: mapLongitude.value },
|
||||||
|
{ latitude: lat, longitude: lng },
|
||||||
|
)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
mapLatitude.value = lat
|
mapLatitude.value = lat
|
||||||
mapLongitude.value = lng
|
mapLongitude.value = lng
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
function onRegionChange(event: UniHelper.MapRegionChangeEvent) {
|
|
||||||
emit('regionchange', event)
|
|
||||||
}
|
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
emit('ready')
|
mapLatitude.value = props.latitude
|
||||||
|
mapLongitude.value = props.longitude
|
||||||
|
dragOrigin = { latitude: props.latitude, longitude: props.longitude }
|
||||||
|
void nextTick(() => {
|
||||||
|
ensureMapContext()
|
||||||
|
setTimeout(() => {
|
||||||
|
if (!mapReady && !destroyed) {
|
||||||
|
mapReady = true
|
||||||
|
emit('ready')
|
||||||
|
}
|
||||||
|
ensureMapContext()
|
||||||
|
}, 400)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
destroyed = true
|
||||||
|
mapCtx = null
|
||||||
})
|
})
|
||||||
|
|
||||||
defineExpose({
|
defineExpose({
|
||||||
|
/** 页面 onReady 时再取一次 context,App 上更稳 */
|
||||||
|
onPageReady() {
|
||||||
|
ensureMapContext()
|
||||||
|
},
|
||||||
moveTo(lng: number, lat: number) {
|
moveTo(lng: number, lat: number) {
|
||||||
syncingFromParent = true
|
ignoreRegionUntil = Date.now() + 800
|
||||||
|
dragging = false
|
||||||
|
lastEmitKey = ''
|
||||||
mapLatitude.value = lat
|
mapLatitude.value = lat
|
||||||
mapLongitude.value = lng
|
mapLongitude.value = lng
|
||||||
void nextTick(() => {
|
dragOrigin = { latitude: lat, longitude: lng }
|
||||||
syncingFromParent = false
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
moveToUserLocation() {
|
moveToUserLocation() {
|
||||||
if (destroyed) return
|
if (destroyed) return
|
||||||
|
ignoreRegionUntil = Date.now() + 1000
|
||||||
|
dragging = false
|
||||||
|
lastEmitKey = ''
|
||||||
void nextTick(() => {
|
void nextTick(() => {
|
||||||
if (destroyed) return
|
if (destroyed) return
|
||||||
getMapContext().moveToLocation({})
|
ensureMapContext()?.moveToLocation({
|
||||||
|
latitude: mapLatitude.value,
|
||||||
|
longitude: mapLongitude.value,
|
||||||
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -84,6 +317,7 @@ defineExpose({
|
|||||||
position: relative;
|
position: relative;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
.location-map__canvas {
|
.location-map__canvas {
|
||||||
@@ -95,28 +329,45 @@ defineExpose({
|
|||||||
position: absolute;
|
position: absolute;
|
||||||
left: 50%;
|
left: 50%;
|
||||||
top: 50%;
|
top: 50%;
|
||||||
transform: translate(-50%, -100%);
|
width: 56rpx;
|
||||||
display: flex;
|
height: 76rpx;
|
||||||
flex-direction: column;
|
margin-left: -28rpx;
|
||||||
align-items: center;
|
margin-top: -76rpx;
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
|
z-index: 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.location-map__pin-img {
|
||||||
|
width: 56rpx;
|
||||||
|
height: 76rpx;
|
||||||
|
}
|
||||||
|
|
||||||
|
.location-map__locate {
|
||||||
|
position: absolute;
|
||||||
|
right: 24rpx;
|
||||||
|
bottom: 32rpx;
|
||||||
|
width: 80rpx;
|
||||||
|
height: 80rpx;
|
||||||
|
border-radius: 40rpx;
|
||||||
|
background-color: #ffffff;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
z-index: 2;
|
z-index: 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
.location-map__pin-dot {
|
.location-map__locate--web {
|
||||||
width: 28rpx;
|
box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.12);
|
||||||
height: 28rpx;
|
|
||||||
border-radius: 50%;
|
|
||||||
background: $color-primary;
|
|
||||||
border: 4rpx solid #fff;
|
|
||||||
box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.location-map__pin-stick {
|
.location-map__locate--loading {
|
||||||
width: 4rpx;
|
opacity: 0.6;
|
||||||
height: 16rpx;
|
}
|
||||||
margin-top: -2rpx;
|
|
||||||
background: $color-primary;
|
.location-map__locate-icon {
|
||||||
border-radius: 2rpx;
|
font-size: 40rpx;
|
||||||
|
line-height: 80rpx;
|
||||||
|
text-align: center;
|
||||||
|
color: #1ec671;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -73,23 +73,41 @@
|
|||||||
bottom: 32rpx;
|
bottom: 32rpx;
|
||||||
width: 80rpx;
|
width: 80rpx;
|
||||||
height: 80rpx;
|
height: 80rpx;
|
||||||
border-radius: 50%;
|
border-radius: 40rpx;
|
||||||
background: #fff;
|
background-color: #ffffff;
|
||||||
box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.12);
|
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
z-index: 2;
|
z-index: 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.home-location-page__locate--web {
|
||||||
|
box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
.home-location-page__locate--loading {
|
.home-location-page__locate--loading {
|
||||||
opacity: 0.6;
|
opacity: 0.6;
|
||||||
}
|
}
|
||||||
|
|
||||||
.home-location-page__locate-icon {
|
.home-location-page__locate-icon {
|
||||||
font-size: 40rpx;
|
font-size: 40rpx;
|
||||||
line-height: 1;
|
line-height: 80rpx;
|
||||||
color: $color-primary;
|
text-align: center;
|
||||||
|
color: #1ec671;
|
||||||
|
}
|
||||||
|
|
||||||
|
.home-location-page__map-loading {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
background: #eef1f4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.home-location-page__map-loading-text {
|
||||||
|
font-size: 28rpx;
|
||||||
|
color: #98a2b3;
|
||||||
}
|
}
|
||||||
|
|
||||||
.home-location-page__footer {
|
.home-location-page__footer {
|
||||||
@@ -114,3 +132,23 @@
|
|||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
word-break: break-all;
|
word-break: break-all;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.home-location-page__pick-btn {
|
||||||
|
margin-top: 24rpx;
|
||||||
|
height: 88rpx;
|
||||||
|
border-radius: 16rpx;
|
||||||
|
background: $color-primary;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.home-location-page__pick-btn--disabled {
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.home-location-page__pick-btn-text {
|
||||||
|
font-size: 30rpx;
|
||||||
|
color: #fff;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,29 +18,63 @@
|
|||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view v-show="!searchVisible" class="home-location-page__map-wrap">
|
<view v-show="!searchVisible" class="home-location-page__map-wrap">
|
||||||
<location-map
|
<view v-if="!mapCenterReady" class="home-location-page__map-loading">
|
||||||
ref="locationMapRef"
|
<text class="home-location-page__map-loading-text">正在定位…</text>
|
||||||
|
</view>
|
||||||
|
<map
|
||||||
|
v-else
|
||||||
|
:id="MAP_ID"
|
||||||
class="home-location-page__map"
|
class="home-location-page__map"
|
||||||
|
provider="amap"
|
||||||
:latitude="latitude"
|
:latitude="latitude"
|
||||||
:longitude="longitude"
|
:longitude="longitude"
|
||||||
@ready="onMapReady"
|
:scale="16"
|
||||||
@regionchange="onRegionChange"
|
:markers="markers"
|
||||||
/>
|
:show-location="true"
|
||||||
|
:enable-3d="false"
|
||||||
|
:show-compass="false"
|
||||||
|
:enable-scroll="true"
|
||||||
|
:enable-zoom="true"
|
||||||
|
@tap="onMapTap"
|
||||||
|
@updated="onMapReady"
|
||||||
|
>
|
||||||
|
<!-- #ifdef APP-PLUS || MP-WEIXIN -->
|
||||||
|
<cover-view
|
||||||
|
class="home-location-page__locate"
|
||||||
|
:class="{ 'home-location-page__locate--loading': locating }"
|
||||||
|
@tap.stop="onLocate"
|
||||||
|
>
|
||||||
|
<cover-view class="home-location-page__locate-icon">◎</cover-view>
|
||||||
|
</cover-view>
|
||||||
|
<!-- #endif -->
|
||||||
|
</map>
|
||||||
|
|
||||||
|
<!-- #ifdef H5 -->
|
||||||
<view
|
<view
|
||||||
class="home-location-page__locate"
|
v-if="mapCenterReady"
|
||||||
|
class="home-location-page__locate home-location-page__locate--web"
|
||||||
:class="{ 'home-location-page__locate--loading': locating }"
|
:class="{ 'home-location-page__locate--loading': locating }"
|
||||||
@tap="onLocate"
|
@tap.stop="onLocate"
|
||||||
>
|
>
|
||||||
<text class="home-location-page__locate-icon">◎</text>
|
<text class="home-location-page__locate-icon">◎</text>
|
||||||
</view>
|
</view>
|
||||||
|
<!-- #endif -->
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view v-show="!searchVisible" class="home-location-page__footer">
|
<view v-show="!searchVisible" class="home-location-page__footer">
|
||||||
<text class="home-location-page__address-label">当前位置</text>
|
<text class="home-location-page__address-label">当前位置</text>
|
||||||
<text class="home-location-page__address-text">
|
<text class="home-location-page__address-text">
|
||||||
{{ resolvingAddress ? '正在解析地址…' : (addressText || '拖动地图选择家庭位置') }}
|
{{ resolvingAddress ? '正在解析地址…' : (addressText || '请选择家庭位置') }}
|
||||||
</text>
|
</text>
|
||||||
|
<view
|
||||||
|
class="home-location-page__pick-btn"
|
||||||
|
:class="{ 'home-location-page__pick-btn--disabled': choosingLocation }"
|
||||||
|
@tap="openChooseLocation"
|
||||||
|
>
|
||||||
|
<text class="home-location-page__pick-btn-text">
|
||||||
|
{{ choosingLocation ? '打开选点…' : '选择位置' }}
|
||||||
|
</text>
|
||||||
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<location-search
|
<location-search
|
||||||
@@ -56,34 +90,42 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref } from 'vue'
|
import { onReady } from '@dcloudio/uni-app'
|
||||||
|
import { getCurrentInstance } from 'vue'
|
||||||
const sys = uni.getSystemInfoSync()
|
|
||||||
const searchPanelTop = (sys.statusBarHeight ?? 0) + uni.upx2px(88)
|
|
||||||
import LocationMap from './components/location-map/index.vue'
|
|
||||||
import LocationSearch from './components/location-search/index.vue'
|
import LocationSearch from './components/location-search/index.vue'
|
||||||
import { useHomeLocation } from './use-home-location'
|
import { useHomeLocation } from './use-home-location'
|
||||||
|
|
||||||
const locationMapRef = ref<{ moveTo: (lng: number, lat: number) => void } | null>(null)
|
const sys = uni.getSystemInfoSync()
|
||||||
|
const searchPanelTop = (sys.statusBarHeight ?? 0) + uni.upx2px(88)
|
||||||
|
const pageInstance = getCurrentInstance()
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
MAP_ID,
|
||||||
latitude,
|
latitude,
|
||||||
longitude,
|
longitude,
|
||||||
|
markers,
|
||||||
|
mapCenterReady,
|
||||||
searchKeyword,
|
searchKeyword,
|
||||||
addressText,
|
addressText,
|
||||||
locating,
|
locating,
|
||||||
resolvingAddress,
|
resolvingAddress,
|
||||||
onMapReady,
|
choosingLocation,
|
||||||
onRegionChange,
|
|
||||||
searchVisible,
|
searchVisible,
|
||||||
searchCity,
|
searchCity,
|
||||||
|
onMapReady,
|
||||||
|
onMapTap,
|
||||||
|
openChooseLocation,
|
||||||
onOpenSearch,
|
onOpenSearch,
|
||||||
onCloseSearch,
|
onCloseSearch,
|
||||||
onSelectSearchPlace,
|
onSelectSearchPlace,
|
||||||
onLocate,
|
onLocate,
|
||||||
onConfirm,
|
onConfirm,
|
||||||
} = useHomeLocation({
|
} = useHomeLocation({
|
||||||
locationMapRef,
|
pageInstance,
|
||||||
|
})
|
||||||
|
|
||||||
|
onReady(() => {
|
||||||
|
onMapReady()
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { onLoad, onUnload } from '@dcloudio/uni-app'
|
import { onLoad, onUnload } from '@dcloudio/uni-app'
|
||||||
import type { Ref } from 'vue'
|
import type { ComponentInternalInstance } from 'vue'
|
||||||
import { ref } from 'vue'
|
import { computed, ref } from 'vue'
|
||||||
import {
|
import {
|
||||||
buildUpdateHomeBody,
|
buildUpdateHomeBody,
|
||||||
isValidHomeName,
|
isValidHomeName,
|
||||||
@@ -15,19 +15,16 @@ import {
|
|||||||
} from '@/utils/amap'
|
} from '@/utils/amap'
|
||||||
import { ensureLocationPermission, getLocationFailHint } from '@/utils/location-permission'
|
import { ensureLocationPermission, getLocationFailHint } from '@/utils/location-permission'
|
||||||
import { formatCoordinateAddress } from '@/utils/map-geocode'
|
import { formatCoordinateAddress } from '@/utils/map-geocode'
|
||||||
|
import { staticAssets } from '@/utils/static-assets'
|
||||||
import { loadHomeGeo, saveHomeGeo } from '../use-home-manage'
|
import { loadHomeGeo, saveHomeGeo } from '../use-home-manage'
|
||||||
import type { HomeGeoInfo } from '../home-manage.types'
|
import type { HomeGeoInfo } from '../home-manage.types'
|
||||||
|
|
||||||
const DEFAULT_LATITUDE = 39.9042
|
const DEFAULT_LATITUDE = 39.9042
|
||||||
const DEFAULT_LONGITUDE = 116.4074
|
const DEFAULT_LONGITUDE = 116.4074
|
||||||
|
const MAP_ID = 'home-location-map'
|
||||||
interface LocationMapExpose {
|
|
||||||
moveTo: (lng: number, lat: number) => void
|
|
||||||
moveToUserLocation: () => void
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UseHomeLocationOptions {
|
interface UseHomeLocationOptions {
|
||||||
locationMapRef?: Ref<LocationMapExpose | null>
|
pageInstance?: ComponentInternalInstance | null
|
||||||
}
|
}
|
||||||
|
|
||||||
async function requestUserLocation(): Promise<UniApp.GetLocationSuccess> {
|
async function requestUserLocation(): Promise<UniApp.GetLocationSuccess> {
|
||||||
@@ -53,16 +50,41 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) {
|
|||||||
const saving = ref(false)
|
const saving = ref(false)
|
||||||
const latitude = ref(DEFAULT_LATITUDE)
|
const latitude = ref(DEFAULT_LATITUDE)
|
||||||
const longitude = ref(DEFAULT_LONGITUDE)
|
const longitude = ref(DEFAULT_LONGITUDE)
|
||||||
|
const mapCenterReady = ref(false)
|
||||||
const searchKeyword = ref('')
|
const searchKeyword = ref('')
|
||||||
const addressText = ref('')
|
const addressText = ref('')
|
||||||
const locating = ref(false)
|
const locating = ref(false)
|
||||||
const resolvingAddress = ref(false)
|
const resolvingAddress = ref(false)
|
||||||
const mapReady = ref(false)
|
const choosingLocation = ref(false)
|
||||||
const searchVisible = ref(false)
|
const searchVisible = ref(false)
|
||||||
const searchCity = ref('')
|
const searchCity = ref('')
|
||||||
|
|
||||||
let geocodeTimer: ReturnType<typeof setTimeout> | null = null
|
let geocodeTimer: ReturnType<typeof setTimeout> | null = null
|
||||||
let pendingMapCenter: { lng: number; lat: number } | null = null
|
|
||||||
let disposed = false
|
let disposed = false
|
||||||
|
let mapCtx: UniApp.MapContext | null = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根因:App-vue 的 <map @tap> 官方不返回经纬度(仅 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() {
|
function clearGeocodeTimer() {
|
||||||
if (geocodeTimer) {
|
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() {
|
function scheduleAddressResolve() {
|
||||||
clearGeocodeTimer()
|
clearGeocodeTimer()
|
||||||
geocodeTimer = setTimeout(() => {
|
geocodeTimer = setTimeout(() => {
|
||||||
|
geocodeTimer = null
|
||||||
void resolveAddressFromCoords()
|
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() {
|
async function resolveAddressFromCoords() {
|
||||||
@@ -116,50 +196,38 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function initMapCenter(saved?: HomeGeoInfo) {
|
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
|
locating.value = true
|
||||||
|
mapCenterReady.value = false
|
||||||
try {
|
try {
|
||||||
const location = await requestUserLocation()
|
if (saved?.latitude != null && saved.longitude != null) {
|
||||||
latitude.value = location.latitude
|
latitude.value = saved.latitude
|
||||||
longitude.value = location.longitude
|
longitude.value = saved.longitude
|
||||||
syncMapView(location.longitude, location.latitude)
|
addressText.value = saved.address || ''
|
||||||
if (location.address) {
|
if (!addressText.value) await resolveAddressFromCoords()
|
||||||
addressText.value = location.address
|
return
|
||||||
} else {
|
}
|
||||||
await resolveAddressFromCoords()
|
|
||||||
|
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 {
|
} finally {
|
||||||
locating.value = false
|
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() {
|
function onOpenSearch() {
|
||||||
searchVisible.value = true
|
searchVisible.value = true
|
||||||
}
|
}
|
||||||
@@ -177,15 +245,16 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) {
|
|||||||
locating.value = true
|
locating.value = true
|
||||||
try {
|
try {
|
||||||
const location = await requestUserLocation()
|
const location = await requestUserLocation()
|
||||||
latitude.value = location.latitude
|
setLocation(
|
||||||
longitude.value = location.longitude
|
location.latitude,
|
||||||
syncMapView(location.longitude, location.latitude)
|
location.longitude,
|
||||||
options.locationMapRef?.value?.moveToUserLocation()
|
location.address || undefined,
|
||||||
if (location.address) {
|
)
|
||||||
addressText.value = location.address
|
ensureMapContext()?.moveToLocation({
|
||||||
} else {
|
latitude: location.latitude,
|
||||||
await resolveAddressFromCoords()
|
longitude: location.longitude,
|
||||||
}
|
})
|
||||||
|
if (!location.address) await resolveAddressFromCoords()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
uni.showToast({ title: getLocationFailHint(err), icon: 'none' })
|
uni.showToast({ title: getLocationFailHint(err), icon: 'none' })
|
||||||
} finally {
|
} finally {
|
||||||
@@ -233,7 +302,7 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) {
|
|||||||
onUnload(() => {
|
onUnload(() => {
|
||||||
disposed = true
|
disposed = true
|
||||||
clearGeocodeTimer()
|
clearGeocodeTimer()
|
||||||
pendingMapCenter = null
|
mapCtx = null
|
||||||
})
|
})
|
||||||
|
|
||||||
onLoad((query?: Record<string, string | undefined>) => {
|
onLoad((query?: Record<string, string | undefined>) => {
|
||||||
@@ -251,16 +320,21 @@ export function useHomeLocation(options: UseHomeLocationOptions = {}) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
MAP_ID,
|
||||||
latitude,
|
latitude,
|
||||||
longitude,
|
longitude,
|
||||||
|
markers,
|
||||||
|
mapCenterReady,
|
||||||
searchKeyword,
|
searchKeyword,
|
||||||
addressText,
|
addressText,
|
||||||
locating,
|
locating,
|
||||||
resolvingAddress,
|
resolvingAddress,
|
||||||
onMapReady,
|
choosingLocation,
|
||||||
onRegionChange,
|
|
||||||
searchVisible,
|
searchVisible,
|
||||||
searchCity,
|
searchCity,
|
||||||
|
onMapReady,
|
||||||
|
onMapTap,
|
||||||
|
openChooseLocation,
|
||||||
onOpenSearch,
|
onOpenSearch,
|
||||||
onCloseSearch,
|
onCloseSearch,
|
||||||
onSelectSearchPlace,
|
onSelectSearchPlace,
|
||||||
|
|||||||
@@ -27,10 +27,11 @@
|
|||||||
<view v-else-if="loading" class="device-page__loading">
|
<view v-else-if="loading" class="device-page__loading">
|
||||||
<text class="device-page__loading-text">加载中...</text>
|
<text class="device-page__loading-text">加载中...</text>
|
||||||
</view>
|
</view>
|
||||||
<template v-else>
|
<device-empty
|
||||||
<device-empty :step-hint="bindStepHint" @bind="onStartBind" />
|
v-else
|
||||||
<hot-devices :items="hotDevices" @mall="onGoMall" />
|
:step-hint="bindStepHint"
|
||||||
</template>
|
@bind="onStartBind"
|
||||||
|
/>
|
||||||
|
|
||||||
<room-location-dialog
|
<room-location-dialog
|
||||||
v-model:visible="roomDialogVisible"
|
v-model:visible="roomDialogVisible"
|
||||||
@@ -48,7 +49,6 @@ import { useTabPage } from '@/hooks/use-tab-page'
|
|||||||
import DeviceEmpty from './components/device-empty/index.vue'
|
import DeviceEmpty from './components/device-empty/index.vue'
|
||||||
import DeviceHeader from './components/device-header/index.vue'
|
import DeviceHeader from './components/device-header/index.vue'
|
||||||
import DeviceList from './components/device-list/index.vue'
|
import DeviceList from './components/device-list/index.vue'
|
||||||
import HotDevices from './components/hot-devices/index.vue'
|
|
||||||
import OperationSuccessToast from './components/operation-success-toast/index.vue'
|
import OperationSuccessToast from './components/operation-success-toast/index.vue'
|
||||||
import RoomLocationDialog from './components/room-location-dialog/index.vue'
|
import RoomLocationDialog from './components/room-location-dialog/index.vue'
|
||||||
import RoomTabs from './components/room-tabs/index.vue'
|
import RoomTabs from './components/room-tabs/index.vue'
|
||||||
@@ -67,7 +67,6 @@ const {
|
|||||||
onlineCount,
|
onlineCount,
|
||||||
hasDevices,
|
hasDevices,
|
||||||
loading,
|
loading,
|
||||||
hotDevices,
|
|
||||||
bindStepHint,
|
bindStepHint,
|
||||||
roomDialogVisible,
|
roomDialogVisible,
|
||||||
successToastVisible,
|
successToastVisible,
|
||||||
@@ -76,7 +75,6 @@ const {
|
|||||||
onConfirmAddRoom,
|
onConfirmAddRoom,
|
||||||
onStartBind,
|
onStartBind,
|
||||||
onScan,
|
onScan,
|
||||||
onGoMall,
|
|
||||||
onNotify,
|
onNotify,
|
||||||
onHomeManage,
|
onHomeManage,
|
||||||
onSelectHome,
|
onSelectHome,
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import { createLocation } from '@/api/location'
|
|||||||
import { getErrorMessage } from '@/utils/api-error'
|
import { getErrorMessage } from '@/utils/api-error'
|
||||||
import { fetchRoomList } from '@/api/location'
|
import { fetchRoomList } from '@/api/location'
|
||||||
import { fetchDeviceList } from './device.api'
|
import { fetchDeviceList } from './device.api'
|
||||||
import type { DeviceListItem, HotDeviceItem, RoomTab } from './device.types'
|
import type { DeviceListItem, RoomTab } from './device.types'
|
||||||
import type { LocationMap } from './device.utils'
|
import type { LocationMap } from './device.utils'
|
||||||
import { readStoredHomeId, saveSelectedHomeId } from '@/utils/selected-home'
|
import { readStoredHomeId, saveSelectedHomeId } from '@/utils/selected-home'
|
||||||
import { refreshDevicePresence } from './device-presence'
|
import { refreshDevicePresence } from './device-presence'
|
||||||
@@ -33,19 +33,6 @@ import {
|
|||||||
/** 在线态轮询:缩短以便关机后尽快变为离线 */
|
/** 在线态轮询:缩短以便关机后尽快变为离线 */
|
||||||
const PRESENCE_INTERVAL_MS = 8000
|
const PRESENCE_INTERVAL_MS = 8000
|
||||||
|
|
||||||
const DEFAULT_HOT_DEVICES: HotDeviceItem[] = [
|
|
||||||
{
|
|
||||||
id: 'z2',
|
|
||||||
name: 'Z2 空气净化器',
|
|
||||||
desc: '高效除醛 · 智能检测',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'lamp',
|
|
||||||
name: '智能护眼台灯',
|
|
||||||
desc: '无频闪 · 多场景模式',
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
export function useDevice() {
|
export function useDevice() {
|
||||||
const homeName = ref('我的家')
|
const homeName = ref('我的家')
|
||||||
const homeId = ref('')
|
const homeId = ref('')
|
||||||
@@ -54,7 +41,6 @@ export function useDevice() {
|
|||||||
const activeRoomId = ref('all')
|
const activeRoomId = ref('all')
|
||||||
const devices = ref<DeviceListItem[]>([])
|
const devices = ref<DeviceListItem[]>([])
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
const hotDevices = ref<HotDeviceItem[]>(DEFAULT_HOT_DEVICES)
|
|
||||||
const bindStepHint = ref(3)
|
const bindStepHint = ref(3)
|
||||||
const roomDialogVisible = ref(false)
|
const roomDialogVisible = ref(false)
|
||||||
const successToastVisible = ref(false)
|
const successToastVisible = ref(false)
|
||||||
@@ -262,10 +248,6 @@ export function useDevice() {
|
|||||||
uni.showToast({ title: '扫一扫功能开发中', icon: 'none' })
|
uni.showToast({ title: '扫一扫功能开发中', icon: 'none' })
|
||||||
}
|
}
|
||||||
|
|
||||||
function onGoMall() {
|
|
||||||
uni.showToast({ title: '商城功能开发中', icon: 'none' })
|
|
||||||
}
|
|
||||||
|
|
||||||
function onNotify() {
|
function onNotify() {
|
||||||
uni.navigateTo({ url: '/pages/mine/notify/index' })
|
uni.navigateTo({ url: '/pages/mine/notify/index' })
|
||||||
}
|
}
|
||||||
@@ -304,7 +286,6 @@ export function useDevice() {
|
|||||||
onlineCount,
|
onlineCount,
|
||||||
hasDevices,
|
hasDevices,
|
||||||
loading,
|
loading,
|
||||||
hotDevices,
|
|
||||||
bindStepHint,
|
bindStepHint,
|
||||||
roomDialogVisible,
|
roomDialogVisible,
|
||||||
successToastVisible,
|
successToastVisible,
|
||||||
@@ -313,7 +294,6 @@ export function useDevice() {
|
|||||||
onConfirmAddRoom,
|
onConfirmAddRoom,
|
||||||
onStartBind,
|
onStartBind,
|
||||||
onScan,
|
onScan,
|
||||||
onGoMall,
|
|
||||||
onNotify,
|
onNotify,
|
||||||
onHomeManage,
|
onHomeManage,
|
||||||
onSelectHome,
|
onSelectHome,
|
||||||
|
|||||||
@@ -16,10 +16,16 @@ const GENDER_OPTIONS = [
|
|||||||
{ value: '0', label: '未知' },
|
{ value: '0', label: '未知' },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
/** 省 / 市 / 区均展示;直辖市省市区名重复时去重相邻项 */
|
||||||
function formatLocation(province: string, city: string, district: string): string {
|
function formatLocation(province: string, city: string, district: string): string {
|
||||||
const primary = province || city
|
const parts: string[] = []
|
||||||
if (primary && district) return `${primary} · ${district}`
|
for (const part of [province, city, district]) {
|
||||||
return primary || 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 {
|
function createEmptyForm(): ProfileFormState {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<template>
|
<template>
|
||||||
<view class="privacy-page">
|
<view class="privacy-page">
|
||||||
<app-navbar title="账号与安全" />
|
<app-navbar title="隐私与安全" />
|
||||||
|
|
||||||
<page-scroll-view class="privacy-page__scroll">
|
<page-scroll-view class="privacy-page__scroll">
|
||||||
<view class="privacy-page__body">
|
<view class="privacy-page__body">
|
||||||
|
|||||||
BIN
src/static/icon-map-pin.png
Normal file
BIN
src/static/icon-map-pin.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 369 B |
@@ -25,6 +25,26 @@ export function isBluetoothApiSupported(): boolean {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type BluetoothAdapterStateChangeHandler = (res: {
|
||||||
|
available: boolean
|
||||||
|
discovering: boolean
|
||||||
|
}) => void
|
||||||
|
|
||||||
|
/** 监听蓝牙开关变化(添加设备页:开启后立即进扫描) */
|
||||||
|
export function subscribeBluetoothAdapterStateChange(
|
||||||
|
handler: BluetoothAdapterStateChangeHandler,
|
||||||
|
): () => void {
|
||||||
|
if (typeof uni.onBluetoothAdapterStateChange !== 'function') {
|
||||||
|
return () => undefined
|
||||||
|
}
|
||||||
|
uni.onBluetoothAdapterStateChange(handler)
|
||||||
|
return () => {
|
||||||
|
if (typeof uni.offBluetoothAdapterStateChange === 'function') {
|
||||||
|
uni.offBluetoothAdapterStateChange(handler)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function openAdapter(): Promise<void> {
|
function openAdapter(): Promise<void> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.openBluetoothAdapter({
|
uni.openBluetoothAdapter({
|
||||||
@@ -34,6 +54,19 @@ function openAdapter(): Promise<void> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function closeAdapter(): Promise<void> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
if (typeof uni.closeBluetoothAdapter !== 'function') {
|
||||||
|
resolve()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uni.closeBluetoothAdapter({
|
||||||
|
success: () => resolve(),
|
||||||
|
fail: () => resolve(),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function getAdapterState(): Promise<UniApp.GetBluetoothAdapterStateSuccess> {
|
function getAdapterState(): Promise<UniApp.GetBluetoothAdapterStateSuccess> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.getBluetoothAdapterState({
|
uni.getBluetoothAdapterState({
|
||||||
@@ -43,6 +76,10 @@ function getAdapterState(): Promise<UniApp.GetBluetoothAdapterStateSuccess> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function wait(ms: number): Promise<void> {
|
||||||
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||||
|
}
|
||||||
|
|
||||||
/** 初始化蓝牙适配器并确认可用 */
|
/** 初始化蓝牙适配器并确认可用 */
|
||||||
export async function ensureBluetoothReady(): Promise<void> {
|
export async function ensureBluetoothReady(): Promise<void> {
|
||||||
if (!isBluetoothApiSupported()) {
|
if (!isBluetoothApiSupported()) {
|
||||||
@@ -70,6 +107,39 @@ export async function ensureBluetoothReady(): Promise<void> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加设备入口探测:先关闭再打开适配器,避免「曾开启后关掉」仍读到旧 available=true。
|
||||||
|
*/
|
||||||
|
export async function probeBluetoothEnabledForBind(): Promise<void> {
|
||||||
|
if (!isBluetoothApiSupported()) {
|
||||||
|
throw new BluetoothError('当前环境不支持蓝牙,请使用 App 或微信小程序')
|
||||||
|
}
|
||||||
|
|
||||||
|
await closeAdapter()
|
||||||
|
// 系统关掉蓝牙后状态刷新有短暂延迟,稍候再开适配器
|
||||||
|
await wait(300)
|
||||||
|
|
||||||
|
try {
|
||||||
|
await openAdapter()
|
||||||
|
} catch (err) {
|
||||||
|
if (isBluetoothDisabledError(err) || isBluetoothPermissionError(err)) throw err
|
||||||
|
throw new BluetoothError(BLUETOOTH_DISABLED_MESSAGE, 10001)
|
||||||
|
}
|
||||||
|
|
||||||
|
const state = await getAdapterState()
|
||||||
|
if (!state.available) {
|
||||||
|
throw new BluetoothError(BLUETOOTH_DISABLED_MESSAGE, 10001)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await startBluetoothDiscovery()
|
||||||
|
await stopBluetoothDiscovery()
|
||||||
|
} catch (err) {
|
||||||
|
if (isBluetoothDisabledError(err) || isBluetoothPermissionError(err)) throw err
|
||||||
|
throw new BluetoothError(BLUETOOTH_DISABLED_MESSAGE, 10001)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function isBluetoothDisabledError(err: unknown): boolean {
|
export function isBluetoothDisabledError(err: unknown): boolean {
|
||||||
if (err instanceof BluetoothError && err.errCode === 10001) return true
|
if (err instanceof BluetoothError && err.errCode === 10001) return true
|
||||||
const code = (err as UniFail)?.errCode
|
const code = (err as UniFail)?.errCode
|
||||||
@@ -121,9 +191,7 @@ export function isBluetoothPermissionError(err: unknown): boolean {
|
|||||||
/** 确认蓝牙已开启且可扫描(添加设备流程入口使用) */
|
/** 确认蓝牙已开启且可扫描(添加设备流程入口使用) */
|
||||||
export async function assertBluetoothEnabledForScan(): Promise<void> {
|
export async function assertBluetoothEnabledForScan(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await ensureBluetoothReady()
|
await probeBluetoothEnabledForBind()
|
||||||
await startBluetoothDiscovery()
|
|
||||||
await stopBluetoothDiscovery()
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (isBluetoothDisabledError(err) || isBluetoothPermissionError(err)) {
|
if (isBluetoothDisabledError(err) || isBluetoothPermissionError(err)) {
|
||||||
throw err
|
throw err
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import iconMineCoupon from '@/static/icon-mine-coupon@2x.png'
|
|||||||
import iconMineAddress from '@/static/icon-mine-address@2x.png'
|
import iconMineAddress from '@/static/icon-mine-address@2x.png'
|
||||||
import iconNotifyFilterArrow from '@/static/icon-notify-filter-arrow.png'
|
import iconNotifyFilterArrow from '@/static/icon-notify-filter-arrow.png'
|
||||||
import iconSupportTime from '@/static/icon-support-time.png'
|
import iconSupportTime from '@/static/icon-support-time.png'
|
||||||
|
import iconMapPin from '@/static/icon-map-pin.png'
|
||||||
|
|
||||||
export const staticAssets = {
|
export const staticAssets = {
|
||||||
logo: logoCenter,
|
logo: logoCenter,
|
||||||
@@ -75,4 +76,5 @@ export const staticAssets = {
|
|||||||
iconMineAddress,
|
iconMineAddress,
|
||||||
iconNotifyFilterArrow,
|
iconNotifyFilterArrow,
|
||||||
iconSupportTime,
|
iconSupportTime,
|
||||||
|
iconMapPin,
|
||||||
} as const
|
} as const
|
||||||
|
|||||||
Reference in New Issue
Block a user