fix(user): 修复地址和购物车相关的多个问题

- 解决鸿蒙手机首次打开地图选择不显示 POI 列表,先获取当前位置传入地图
- 修正收货地址编辑接口路径,改为无 id 的 PUT /shop/shop-user-address
- 防御性合并旧地址数据,避免 null 字段覆盖初始值
- 新用户注册和登录后同步更新 UserContext 状态,确保页面刷新购物车数据
- 各页面 useDidShow 内增加从 storage 同步用户状态,解决状态不同步问题
- 优化地图选择初始化逻辑,提升用户体验
- 保证购物车、商品详情和首页登录状态及时同步更新
This commit is contained in:
2026-07-17 01:07:54 +08:00
parent 22fed3b163
commit 1077b16f58
9 changed files with 99 additions and 18 deletions

View File

@@ -313,11 +313,35 @@ const AddressEditPage: React.FC = () => {
return
}
// 3. 仅在有有效经纬度时传入初始化参数,避免 undefined 触发部分机型失败
const initLat = selectedLocation?.lat ? Number(selectedLocation.lat) : undefined
const initLng = selectedLocation?.lng ? Number(selectedLocation.lng) : undefined
const latitude = typeof initLat === 'number' && Number.isFinite(initLat) ? initLat : undefined
const longitude = typeof initLng === 'number' && Number.isFinite(initLng) ? initLng : undefined
// 3. 优先使用已有经纬度;没有则先调 getLocation 获取当前位置
// 解决鸿蒙等机型首次打开地图时 POI 列表不显示的问题
let latitude: number | undefined
let longitude: number | undefined
if (selectedLocation?.lat && selectedLocation?.lng) {
const initLat = Number(selectedLocation.lat)
const initLng = Number(selectedLocation.lng)
if (Number.isFinite(initLat) && Number.isFinite(initLng)) {
latitude = initLat
longitude = initLng
}
}
// 没有已有定位时,主动获取当前位置传给地图,确保地图打开就在用户位置附近
// 这样地图的 POI 列表能立即加载,不需要用户手动拖动
if (latitude === undefined || longitude === undefined) {
try {
const loc: any = await Taro.getLocation({ type: 'gcj02' })
if (loc && Number.isFinite(loc.latitude) && Number.isFinite(loc.longitude)) {
latitude = loc.latitude
longitude = loc.longitude
}
} catch (locErr) {
// getLocation 失败不阻断流程chooseLocation 仍可打开(只是默认定位到北京)
console.warn('getLocation 失败,地图将以默认位置打开:', locErr)
}
}
const params: any = {}
if (typeof latitude === 'number') params.latitude = latitude
if (typeof longitude === 'number') params.longitude = longitude
@@ -470,14 +494,28 @@ const AddressEditPage: React.FC = () => {
if (isEditMode && addressId) {
try {
const addr = (await getShopUserAddress(addressId)) as ShopUserAddress
setFormData(addr)
const p = String((addr as any)?.province || '').trim()
const c = String((addr as any)?.city || '').trim()
const r = String((addr as any)?.region || '').trim()
setRegionText([p, c, r].filter(Boolean).join(' '))
if (hasValidLngLat(addr)) {
setSelectedLocation({lng: String((addr as any).lng), lat: String((addr as any).lat)})
setRegionLocked(true)
if (addr) {
// 合并到 formData保证初始字段不被 null 覆盖为 undefined
setFormData(prev => ({
...prev,
...addr,
name: addr.name || prev.name || '',
phone: addr.phone || prev.phone || '',
address: addr.address || prev.address || '',
province: addr.province || prev.province || '',
city: addr.city || prev.city || '',
region: addr.region || prev.region || '',
country: addr.country || prev.country || '中国',
isDefault: addr.isDefault ?? false,
}))
const p = String(addr?.province || '').trim()
const c = String(addr?.city || '').trim()
const r = String(addr?.region || '').trim()
setRegionText([p, c, r].filter(Boolean).join(' '))
if (hasValidLngLat(addr)) {
setSelectedLocation({lng: String((addr as any).lng), lat: String((addr as any).lat)})
setRegionLocked(true)
}
}
} catch (error) {
console.error('加载地址失败:', error)