feat(address): 添加用户地址管理页面的定位功能

- 集成 Taro 地图选点功能,支持用户手动选择精确位置
- 新增地理位置权限处理逻辑,包括授权失败和用户取消的情况
- 实现经纬度信息在地址表单中的存储和回显功能
- 添加定位选择界面,展示已选择的位置信息
- 移除原有的实时定位功能,改为手动选择模式
- 更新地址表单提交逻辑以支持新的定位数据结构
```
This commit is contained in:
2026-02-09 17:15:18 +08:00
parent a1e1487d42
commit 8d2188b928
4 changed files with 256 additions and 132 deletions

View File

@@ -14,10 +14,3 @@
position: absolute;
z-index: 0;
}
/* 吸顶状态下的样式 */
.nutui-sticky--fixed {
.header-bg {
height: 100%;
}
}

View File

@@ -1,6 +1,6 @@
import {useEffect, useState} from "react";
import Taro from '@tarojs/taro';
import {Button, Sticky, Popup, Cell, CellGroup} from '@nutui/nutui-react-taro'
import {Button, Popup, Cell, CellGroup} from '@nutui/nutui-react-taro'
// import {TriangleDown} from '@nutui/icons-react-taro'
import { NavBar} from '@nutui/nutui-react-taro'
import {getUserInfo, getWxOpenId} from "@/api/layout";
@@ -25,7 +25,6 @@ const Header = (_: any) => {
const [IsLogin, setIsLogin] = useState<boolean>(true)
const [statusBarHeight, setStatusBarHeight] = useState<number>()
const [stickyStatus, setStickyStatus] = useState<boolean>(false)
const [userInfo] = useState<User>()
// 门店选择:用于首页展示“最近门店”,并在下单时写入订单 storeId
@@ -291,12 +290,6 @@ const Header = (_: any) => {
})
}
// 处理粘性布局状态变化
const onStickyChange = (isSticky: boolean) => {
setStickyStatus(isSticky)
console.log('Header 粘性状态:', isSticky ? '已固定' : '取消固定')
}
// 获取小程序系统信息
// const getSystemInfo = () => {
// const systemInfo = Taro.getSystemInfoSync()
@@ -311,121 +304,114 @@ const Header = (_: any) => {
return (
<>
<Sticky
threshold={0}
onChange={onStickyChange}
<View
className={'header-bg'}
style={{
zIndex: 1000,
backgroundColor: stickyStatus ? '#03605c' : 'transparent',
transition: 'background-color 0.3s ease',
height: '180px',
paddingBottom: '12px',
}}
>
<View className={'header-bg'} style={{
height: !stickyStatus ? '180px' : `${(statusBarHeight || 0) + 44}px`,
paddingBottom: !stickyStatus ? '12px' : '0px'
}}>
{/* 只在非吸顶状态下显示搜索框 */}
{!stickyStatus && <MySearch statusBarHeight={statusBarHeight} />}
</View>
<NavBar
style={{
marginTop: `${statusBarHeight}px`,
marginBottom: '0px',
backgroundColor: 'transparent'
}}
onBackClick={() => {
}}
// left={
// <View
// style={{display: 'flex', alignItems: 'center', gap: '8px'}}
// onClick={() => setStorePopupVisible(true)}
// >
// <Avatar
// size="22"
// src={getWebsiteLogo()}
// />
// <Text className={'text-white'}>
// {selectedStore?.name || '请选择门店'}
// </Text>
// <TriangleDown className={'text-white'} size={9}/>
// </View>
// }
right={
!IsLogin ? (
<Button
size="small"
fill="none"
style={{color: '#ffffff'}}
open-type="getPhoneNumber"
onGetPhoneNumber={handleGetPhoneNumber}
>
</Button>
) : null
}
>
<Text className={'text-white'}>{getTenantName()}</Text>
</NavBar>
<MySearch statusBarHeight={statusBarHeight} />
</View>
<Popup
visible={storePopupVisible}
position="bottom"
style={{height: '70vh'}}
onClose={() => setStorePopupVisible(false)}
>
<View className="p-4">
<View className="flex justify-between items-center mb-3">
<Text className="text-base font-medium"></Text>
<Text
className="text-sm text-gray-500"
onClick={() => setStorePopupVisible(false)}
>
</Text>
</View>
<NavBar
style={{
marginTop: `${statusBarHeight}px`,
marginBottom: '0px',
backgroundColor: 'transparent'
}}
onBackClick={() => {
}}
// left={
// <View
// style={{display: 'flex', alignItems: 'center', gap: '8px'}}
// onClick={() => setStorePopupVisible(true)}
// >
// <Avatar
// size="22"
// src={getWebsiteLogo()}
// />
// <Text className={'text-white'}>
// {selectedStore?.name || '请选择门店'}
// </Text>
// <TriangleDown className={'text-white'} size={9}/>
// </View>
// }
right={
!IsLogin ? (
<Button
size="small"
fill="none"
style={{color: '#ffffff'}}
open-type="getPhoneNumber"
onGetPhoneNumber={handleGetPhoneNumber}
>
</Button>
) : null
}
>
<Text className={'text-white'}>{getTenantName()}</Text>
</NavBar>
<View className="text-xs text-gray-500 mb-2">
{userLocation ? '已获取定位,按距离排序' : '未获取定位,可手动选择门店'}
</View>
<CellGroup>
{[...stores]
.sort((a, b) => (getStoreDistance(a) ?? Number.POSITIVE_INFINITY) - (getStoreDistance(b) ?? Number.POSITIVE_INFINITY))
.map((s) => {
const d = getStoreDistance(s)
const isActive = !!selectedStore?.id && selectedStore.id === s.id
return (
<Cell
key={s.id}
title={
<View className="flex items-center justify-between">
<Text className={isActive ? 'text-green-600' : ''}>{s.name || `门店${s.id}`}</Text>
{d !== undefined && <Text className="text-xs text-gray-500">{formatDistance(d)}</Text>}
</View>
}
description={s.address || ''}
onClick={async () => {
let storeToSave = s
if (s?.id) {
try {
const full = await getShopStore(s.id)
if (full) storeToSave = full
} catch (_e) {
// keep base item
}
}
setSelectedStore(storeToSave)
saveSelectedStoreToStorage(storeToSave)
setStorePopupVisible(false)
Taro.showToast({title: '门店已切换', icon: 'success'})
}}
/>
)
})}
</CellGroup>
<Popup
visible={storePopupVisible}
position="bottom"
style={{height: '70vh'}}
onClose={() => setStorePopupVisible(false)}
>
<View className="p-4">
<View className="flex justify-between items-center mb-3">
<Text className="text-base font-medium"></Text>
<Text
className="text-sm text-gray-500"
onClick={() => setStorePopupVisible(false)}
>
</Text>
</View>
</Popup>
</Sticky>
<View className="text-xs text-gray-500 mb-2">
{userLocation ? '已获取定位,按距离排序' : '未获取定位,可手动选择门店'}
</View>
<CellGroup>
{[...stores]
.sort((a, b) => (getStoreDistance(a) ?? Number.POSITIVE_INFINITY) - (getStoreDistance(b) ?? Number.POSITIVE_INFINITY))
.map((s) => {
const d = getStoreDistance(s)
const isActive = !!selectedStore?.id && selectedStore.id === s.id
return (
<Cell
key={s.id}
title={
<View className="flex items-center justify-between">
<Text className={isActive ? 'text-green-600' : ''}>{s.name || `门店${s.id}`}</Text>
{d !== undefined && <Text className="text-xs text-gray-500">{formatDistance(d)}</Text>}
</View>
}
description={s.address || ''}
onClick={async () => {
let storeToSave = s
if (s?.id) {
try {
const full = await getShopStore(s.id)
if (full) storeToSave = full
} catch (_e) {
// keep base item
}
}
setSelectedStore(storeToSave)
saveSelectedStoreToStorage(storeToSave)
setStorePopupVisible(false)
Taro.showToast({title: '门店已切换', icon: 'success'})
}}
/>
)
})}
</CellGroup>
</View>
</Popup>
</>
)
}

View File

@@ -234,7 +234,7 @@ function Home() {
return (
<>
{/* Header区域 - 现在由Header组件内部处理吸顶逻辑 */}
{/* Header区域 */}
<Header />
<View className="home-page">

View File

@@ -1,6 +1,6 @@
import {useEffect, useState, useRef} from "react";
import {useRouter} from '@tarojs/taro'
import {Button, Loading, CellGroup, Input, TextArea, Form} from '@nutui/nutui-react-taro'
import {Button, Loading, CellGroup, Cell, Input, TextArea, Form} from '@nutui/nutui-react-taro'
import {Scan, ArrowRight} from '@nutui/icons-react-taro'
import Taro from '@tarojs/taro'
import {View} from '@tarojs/components'
@@ -9,7 +9,24 @@ import {ShopUserAddress} from "@/api/shop/shopUserAddress/model";
import {getShopUserAddress, listShopUserAddress, updateShopUserAddress, addShopUserAddress} from "@/api/shop/shopUserAddress";
import RegionData from '@/api/json/regions-data.json';
import FixedButton from "@/components/FixedButton";
import { getCurrentLngLat } from "@/utils/location";
type SelectedLocation = { lng: string; lat: string; name?: string; address?: string }
const isLocationDenied = (e: any) => {
const msg = String(e?.errMsg || e?.message || e || '')
return (
msg.includes('auth deny') ||
msg.includes('authorize') ||
msg.includes('permission') ||
msg.includes('denied') ||
msg.includes('scope.userLocation')
)
}
const isUserCancel = (e: any) => {
const msg = String(e?.errMsg || e?.message || e || '')
return msg.includes('cancel')
}
const AddUserAddress = () => {
const {params} = useRouter();
@@ -19,6 +36,7 @@ const AddUserAddress = () => {
const [visible, setVisible] = useState(false)
const [FormData, setFormData] = useState<ShopUserAddress>({})
const [inputText, setInputText] = useState<string>('')
const [selectedLocation, setSelectedLocation] = useState<SelectedLocation | null>(null)
const formRef = useRef<any>(null)
// 判断是编辑还是新增模式
@@ -36,6 +54,10 @@ const AddUserAddress = () => {
setFormData(address)
// 设置所在地区
setText(`${address.province} ${address.city} ${address.region}`)
// 回显已保存的经纬度(编辑模式)
if (address?.lng && address?.lat) {
setSelectedLocation({ lng: String(address.lng), lat: String(address.lat) })
}
} catch (error) {
console.error('加载地址失败:', error)
Taro.showToast({
@@ -211,10 +233,111 @@ const AddUserAddress = () => {
return null;
};
// 选择定位:打开地图让用户选点,保存经纬度到表单数据
const chooseGeoLocation = async () => {
const applyChosenLocation = (res: any) => {
if (!res) return
if (res.latitude === undefined || res.longitude === undefined) {
Taro.showToast({ title: '定位信息获取失败', icon: 'none' })
return
}
const next: SelectedLocation = {
lng: String(res.longitude),
lat: String(res.latitude),
name: res.name,
address: res.address
}
setSelectedLocation(next)
// 将地图选点的地址同步到“收货地址”,减少用户重复输入
const nextDetailAddress = (() => {
const addr = String(res.address || '').trim()
const name = String(res.name || '').trim()
if (!addr && !name) return ''
if (!addr) return name
if (!name) return addr
return addr.includes(name) ? addr : `${addr} ${name}`
})()
// 尝试从地图返回的 address 文本解析省市区best-effort
const regionResult = res?.provinceName || res?.cityName || res?.adName
? {
province: String(res.provinceName || ''),
city: String(res.cityName || ''),
region: String(res.adName || '')
}
: parseRegion(String(res.address || ''))
setFormData(prev => ({
...prev,
lng: next.lng,
lat: next.lat,
address: nextDetailAddress || prev.address,
province: regionResult?.province || prev.province,
city: regionResult?.city || prev.city,
region: regionResult?.region || prev.region
}))
if (regionResult?.province && regionResult?.city && regionResult?.region) {
setText(`${regionResult.province} ${regionResult.city} ${regionResult.region}`)
}
// 更新表单展示值Form initialValues 不会跟随 FormData 变化)
if (formRef.current) {
const patch: any = {}
if (nextDetailAddress) patch.address = nextDetailAddress
if (regionResult?.region) patch.region = regionResult.region
formRef.current.setFieldsValue(patch)
}
}
try {
const initLat = selectedLocation?.lat ? Number(selectedLocation.lat) : undefined
const initLng = selectedLocation?.lng ? Number(selectedLocation.lng) : undefined
const res = await Taro.chooseLocation({
latitude: Number.isFinite(initLat as number) ? (initLat as number) : undefined,
longitude: Number.isFinite(initLng as number) ? (initLng as number) : undefined
})
applyChosenLocation(res)
} catch (e: any) {
console.warn('选择定位失败:', e)
if (isUserCancel(e)) return
if (isLocationDenied(e)) {
try {
const modal = await Taro.showModal({
title: '需要定位权限',
content: '选择定位需要开启定位权限,请在设置中开启后重试。',
confirmText: '去设置'
})
if (modal.confirm) {
await Taro.openSetting()
// 权限可能刚被开启:重试一次
const res = await Taro.chooseLocation({})
applyChosenLocation(res)
}
} catch (_e) {
// ignore
}
return
}
try {
await Taro.showToast({ title: '打开地图失败,请重试', icon: 'none' })
} catch (_e) {
// ignore
}
}
}
// 提交表单
const submitSucceed = async (values: any) => {
const loc = await getCurrentLngLat()
if (!loc) return
const loc =
selectedLocation ||
(FormData?.lng && FormData?.lat ? { lng: String(FormData.lng), lat: String(FormData.lat) } : null)
if (!loc) {
Taro.showToast({ title: '请选择定位', icon: 'none' })
return
}
try {
// 准备提交的数据
@@ -329,6 +452,28 @@ const AddUserAddress = () => {
</div>
</CellGroup>
<View className={'bg-gray-100 h-3'}></View>
<CellGroup className={'px-3'}>
<Cell
title="选择定位"
description={
selectedLocation?.address ||
(selectedLocation ? `经纬度:${selectedLocation.lng}, ${selectedLocation.lat}` : '')
}
extra={(
<div className={'flex items-center gap-2'}>
<div
className={'text-gray-900 text-sm'}
style={{maxWidth: '200px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap'}}
>
{selectedLocation?.name || (selectedLocation ? '已选择' : '请选择')}
</div>
<ArrowRight className={'text-gray-400'}/>
</div>
)}
onClick={chooseGeoLocation}
/>
</CellGroup>
<View className={'bg-gray-100 h-3'}></View>
<CellGroup style={{padding: '4px 0'}}>
<Form.Item name="name" label="收货人" initialValue={FormData.name} required>
<Input placeholder="请输入收货人姓名" maxLength={10}/>
@@ -371,14 +516,14 @@ const AddUserAddress = () => {
/>
{/* 底部浮动按钮 */}
<FixedButton
text={isEditMode ? '更新地址' : '保存并使用'}
<FixedButton
text={isEditMode ? '更新地址' : '保存并使用'}
onClick={() => {
// 触发表单提交
if (formRef.current) {
formRef.current.submit();
}
}}
}}
/>
</>
);