Add bind-time initial location fallback
This commit is contained in:
@@ -5,6 +5,7 @@ import Taro, { useDidShow, useDidHide, useRouter } from '@tarojs/taro'
|
||||
import { getToken } from '@/services/auth'
|
||||
import {
|
||||
getNFCBindSession,
|
||||
reportBindInitialLocation,
|
||||
resolveActiveBinding,
|
||||
setBindingChild,
|
||||
setSelectedBindingDeviceId,
|
||||
@@ -210,6 +211,26 @@ export default function Bind() {
|
||||
Taro.switchTab({ url: '/pages/sleep/index' })
|
||||
}
|
||||
|
||||
const uploadBindInitialLocation = async (targetDeviceId?: string | null) => {
|
||||
const normalizedDeviceId = String(targetDeviceId || '').trim()
|
||||
if (!normalizedDeviceId) return
|
||||
|
||||
try {
|
||||
const location = await Taro.getLocation({
|
||||
type: 'gcj02',
|
||||
isHighAccuracy: true,
|
||||
})
|
||||
await reportBindInitialLocation(normalizedDeviceId, {
|
||||
latitude: location.latitude,
|
||||
longitude: location.longitude,
|
||||
coord_type: 'gcj02',
|
||||
accuracy_m: typeof location.accuracy === 'number' ? Math.round(location.accuracy) : undefined,
|
||||
})
|
||||
} catch (error) {
|
||||
console.log('[bind] initial location skipped:', error)
|
||||
}
|
||||
}
|
||||
|
||||
const handleBack = () => {
|
||||
if (getCurrentPages().length > 1) {
|
||||
Taro.navigateBack()
|
||||
@@ -335,6 +356,9 @@ export default function Bind() {
|
||||
|
||||
if (session.status === SESSION_STATUS_COMPLETED) {
|
||||
setSelectedBindingDeviceId(session.device_id)
|
||||
if (!isAddingCard && session.child_id) {
|
||||
void uploadBindInitialLocation(session.device_id)
|
||||
}
|
||||
setBindHint(
|
||||
isAddingCard
|
||||
? session.card_uuid ? `新卡添加完成,卡号 ${session.card_uuid}` : '新卡添加完成'
|
||||
@@ -480,6 +504,7 @@ export default function Bind() {
|
||||
}
|
||||
await setBindingChild(pendingDeviceId!, { child_id: childId })
|
||||
setSelectedBindingDeviceId(pendingDeviceId)
|
||||
void uploadBindInitialLocation(pendingDeviceId)
|
||||
Taro.showToast({ title: '关联完成', icon: 'success' })
|
||||
setTimeout(() => {
|
||||
goDevicePage()
|
||||
|
||||
@@ -87,6 +87,13 @@ export interface NFCSessionStatus {
|
||||
card_uuid?: string | null
|
||||
}
|
||||
|
||||
export interface BindInitialLocationPayload {
|
||||
latitude: number
|
||||
longitude: number
|
||||
coord_type: 'gcj02'
|
||||
accuracy_m?: number
|
||||
}
|
||||
|
||||
export function getSelectedBindingDeviceId(): string | null {
|
||||
const value = Taro.getStorageSync(SELECTED_BINDING_DEVICE_ID_KEY)
|
||||
const deviceId = String(value || '').trim()
|
||||
@@ -211,6 +218,16 @@ export async function setBindingChild(
|
||||
})
|
||||
}
|
||||
|
||||
export async function reportBindInitialLocation(
|
||||
deviceId: string,
|
||||
data: BindInitialLocationPayload
|
||||
): Promise<{ accepted: boolean; inserted: boolean }> {
|
||||
return request(`/banban/bindings/${deviceId}/initial-location`, {
|
||||
method: 'POST',
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
export async function unbindDevice(deviceId: string): Promise<void> {
|
||||
return request(`/banban/bindings/${deviceId}`, {
|
||||
method: 'DELETE',
|
||||
|
||||
Reference in New Issue
Block a user