feat(ticket): 添加送水地址修改限制功能

- 引入 ADDRESS_CHANGE_COOLDOWN_DAYS 常量设置30天修改间隔
- 新增 ticketAddressModifyLimit 状态管理地址修改权限
- 实现 loadTicketAddressModifyLimit 函数查询订单历史判断修改限制
- 添加 openAddressPage 函数控制地址页面跳转逻辑
- 在提交订单时验证地址修改限制并显示提示信息
- 初始化时加载地址修改限制并强制使用锁定地址
- 更新地址单元格点击事件为 openAddressPage 函数
- 添加地址修改限制状态显示到界面
This commit is contained in:
2026-02-28 00:38:04 +08:00
parent 8b5609255a
commit 6f1e0a6a2b

View File

@@ -16,7 +16,7 @@ import { ArrowRight, Location, Ticket } from '@nutui/icons-react-taro'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import type { ShopGoods } from '@/api/shop/shopGoods/model' import type { ShopGoods } from '@/api/shop/shopGoods/model'
import { getShopGoods } from '@/api/shop/shopGoods' import { getShopGoods } from '@/api/shop/shopGoods'
import { listShopUserAddress } from '@/api/shop/shopUserAddress' import { getShopUserAddress, listShopUserAddress } from '@/api/shop/shopUserAddress'
import type { ShopUserAddress } from '@/api/shop/shopUserAddress/model' import type { ShopUserAddress } from '@/api/shop/shopUserAddress/model'
import './use.scss' import './use.scss'
import Gap from "@/components/Gap"; import Gap from "@/components/Gap";
@@ -27,6 +27,8 @@ import {getSelectedStoreFromStorage, saveSelectedStoreToStorage} from "@/utils/s
import type { GltUserTicket } from '@/api/glt/gltUserTicket/model' import type { GltUserTicket } from '@/api/glt/gltUserTicket/model'
import { listGltUserTicket } from '@/api/glt/gltUserTicket' import { listGltUserTicket } from '@/api/glt/gltUserTicket'
import { addGltTicketOrder } from '@/api/glt/gltTicketOrder' import { addGltTicketOrder } from '@/api/glt/gltTicketOrder'
import { pageGltTicketOrder } from '@/api/glt/gltTicketOrder'
import type { GltTicketOrder } from '@/api/glt/gltTicketOrder/model'
import type { ShopStoreRider } from '@/api/shop/shopStoreRider/model' import type { ShopStoreRider } from '@/api/shop/shopStoreRider/model'
import { listShopStoreRider } from '@/api/shop/shopStoreRider' import { listShopStoreRider } from '@/api/shop/shopStoreRider'
import type { ShopStoreFence } from '@/api/shop/shopStoreFence/model' import type { ShopStoreFence } from '@/api/shop/shopStoreFence/model'
@@ -34,6 +36,7 @@ import { listShopStoreFence } from '@/api/shop/shopStoreFence'
import { parseFencePoints, parseLngLatFromText, pointInAnyPolygon, pointInPolygon } from '@/utils/geofence' import { parseFencePoints, parseLngLatFromText, pointInAnyPolygon, pointInPolygon } from '@/utils/geofence'
const MIN_START_QTY = 10 const MIN_START_QTY = 10
const ADDRESS_CHANGE_COOLDOWN_DAYS = 30
const OrderConfirm = () => { const OrderConfirm = () => {
const [goods, setGoods] = useState<ShopGoods | null>(null); const [goods, setGoods] = useState<ShopGoods | null>(null);
@@ -97,6 +100,137 @@ const OrderConfirm = () => {
return Number.isFinite(id) && id > 0 ? id : undefined return Number.isFinite(id) && id > 0 ? id : undefined
}, []) }, [])
type TicketAddressModifyLimit = {
loaded: boolean
canModify: boolean
nextAllowedText?: string
lockedAddressId?: number
}
const [ticketAddressModifyLimit, setTicketAddressModifyLimit] = useState<TicketAddressModifyLimit>({
loaded: false,
canModify: true,
})
const ticketAddressModifyLimitPromiseRef = useRef<Promise<TicketAddressModifyLimit> | null>(null)
const parseTime = (raw?: unknown) => {
if (raw === undefined || raw === null || raw === '') return null
// Compatible with seconds/milliseconds timestamps.
if (typeof raw === 'number' || (typeof raw === 'string' && /^\d+$/.test(raw))) {
const n = Number(raw)
if (!Number.isFinite(n)) return null
return dayjs(n < 1e12 ? n * 1000 : n)
}
const d = dayjs(raw as any)
return d.isValid() ? d : null
}
const getOrderTime = (o?: Partial<GltTicketOrder> | null) => {
return parseTime(o?.createTime) || parseTime(o?.updateTime)
}
const getOrderAddressKey = (o?: Partial<GltTicketOrder> | null) => {
const id = Number(o?.addressId)
if (Number.isFinite(id) && id > 0) return `id:${id}`
const txt = String(o?.address || '').trim()
if (txt) return `txt:${txt}`
return ''
}
const loadTicketAddressModifyLimit = async (): Promise<TicketAddressModifyLimit> => {
if (ticketAddressModifyLimitPromiseRef.current) return ticketAddressModifyLimitPromiseRef.current
ticketAddressModifyLimitPromiseRef.current = (async () => {
if (!userId) return { loaded: true, canModify: true }
const now = dayjs()
const pageSize = 20
let page = 1
const all: GltTicketOrder[] = []
let latestKey = ''
let latestAddressId: number | undefined = undefined
while (true) {
const res = await pageGltTicketOrder({ page, limit: pageSize, userId })
const list = Array.isArray(res?.list) ? res.list : []
if (page === 1) {
const first = list[0]
latestKey = getOrderAddressKey(first)
const id = Number(first?.addressId)
latestAddressId = Number.isFinite(id) && id > 0 ? id : undefined
}
if (!list.length) break
all.push(...list)
// Find the oldest order in the newest contiguous block of the latest address key.
// That order's time represents the last time user "set/changed" the ticket delivery address.
const currentKey = latestKey
if (!currentKey) {
return { loaded: true, canModify: true }
}
let lastSameIndex = 0
let foundDifferent = false
for (let i = 1; i < all.length; i++) {
const k = getOrderAddressKey(all[i])
if (!k) continue
if (k === currentKey) {
lastSameIndex = i
continue
}
foundDifferent = true
break
}
if (foundDifferent) {
const lastSetAt = getOrderTime(all[lastSameIndex])
if (!lastSetAt) return { loaded: true, canModify: true, lockedAddressId: latestAddressId }
const nextAllowed = lastSetAt.add(ADDRESS_CHANGE_COOLDOWN_DAYS, 'day')
const canModify = now.isAfter(nextAllowed)
return {
loaded: true,
canModify,
nextAllowedText: nextAllowed.format('YYYY-MM-DD'),
lockedAddressId: latestAddressId,
}
}
const oldest = getOrderTime(all[all.length - 1])
if (oldest && now.diff(oldest, 'day') >= ADDRESS_CHANGE_COOLDOWN_DAYS) {
// We have enough history beyond the cooldown window, and still no different address found.
return { loaded: true, canModify: true, lockedAddressId: latestAddressId }
}
const totalCount = typeof (res as any)?.count === 'number' ? Number((res as any).count) : undefined
if (totalCount !== undefined && all.length >= totalCount) break
if (list.length < pageSize) break
page += 1
if (page > 10) break // safety: avoid excessive paging
}
if (!all.length) return { loaded: true, canModify: true }
// If we can't prove the last-set time is older than the cooldown window, be conservative and lock.
const lastSetAt = getOrderTime(all[all.length - 1])
if (!lastSetAt) return { loaded: true, canModify: true, lockedAddressId: latestAddressId }
const nextAllowed = lastSetAt.add(ADDRESS_CHANGE_COOLDOWN_DAYS, 'day')
const canModify = now.isAfter(nextAllowed)
return {
loaded: true,
canModify,
nextAllowedText: nextAllowed.format('YYYY-MM-DD'),
lockedAddressId: latestAddressId,
}
})()
.finally(() => {
ticketAddressModifyLimitPromiseRef.current = null
})
return ticketAddressModifyLimitPromiseRef.current
}
const getTicketAvailableQty = (t?: Partial<GltUserTicket> | null) => { const getTicketAvailableQty = (t?: Partial<GltUserTicket> | null) => {
if (!t) return 0 if (!t) return 0
const anyT: any = t const anyT: any = t
@@ -199,6 +333,22 @@ const OrderConfirm = () => {
return parseLngLatFromText((s.lngAndLat || s.location || '').trim()) return parseLngLatFromText((s.lngAndLat || s.location || '').trim())
} }
const openAddressPage = async () => {
const limit = ticketAddressModifyLimit.loaded
? ticketAddressModifyLimit
: await loadTicketAddressModifyLimit().catch(() => ({ loaded: true, canModify: true } as TicketAddressModifyLimit))
if (!ticketAddressModifyLimit.loaded) setTicketAddressModifyLimit(limit)
if (!limit.canModify) {
Taro.showToast({
title: `送水地址每${ADDRESS_CHANGE_COOLDOWN_DAYS}天可修改一次${limit.nextAllowedText ? '' + limit.nextAllowedText + ' 后可修改' : ''}`,
icon: 'none',
})
return
}
Taro.navigateTo({ url: '/user/address/index' })
}
const loadFences = async (): Promise<ShopStoreFence[]> => { const loadFences = async (): Promise<ShopStoreFence[]> => {
if (fencesLoadedRef.current) return fences if (fencesLoadedRef.current) return fences
if (fencesPromiseRef.current) return fencesPromiseRef.current if (fencesPromiseRef.current) return fencesPromiseRef.current
@@ -484,6 +634,25 @@ const OrderConfirm = () => {
Taro.showToast({ title: '请选择收货地址', icon: 'none' }) Taro.showToast({ title: '请选择收货地址', icon: 'none' })
return return
} }
// Ticket delivery address is based on order snapshot. Enforce "once per 30 days" by latest ticket-order history.
const limit = ticketAddressModifyLimit.loaded
? ticketAddressModifyLimit
: await loadTicketAddressModifyLimit().catch(() => ({ loaded: true, canModify: true } as TicketAddressModifyLimit))
if (!ticketAddressModifyLimit.loaded) setTicketAddressModifyLimit(limit)
if (!limit.canModify && limit.lockedAddressId && address.id !== limit.lockedAddressId) {
Taro.showToast({
title: `送水地址每${ADDRESS_CHANGE_COOLDOWN_DAYS}天可修改一次,请使用上次下单地址${limit.nextAllowedText ? '' + limit.nextAllowedText + ' 后可修改)' : ''}`,
icon: 'none',
})
try {
const locked = await getShopUserAddress(limit.lockedAddressId)
if (locked?.id) setAddress(locked)
} catch (_e) {
// ignore: keep current address, but still block submission
}
return
}
if (!addressHasCoords) { if (!addressHasCoords) {
Taro.showToast({ title: '该收货地址缺少经纬度,请在地址里选择地图定位后重试', icon: 'none' }) Taro.showToast({ title: '该收货地址缺少经纬度,请在地址里选择地图定位后重试', icon: 'none' })
return return
@@ -618,6 +787,20 @@ const OrderConfirm = () => {
if (addressRes && addressRes.length > 0) { if (addressRes && addressRes.length > 0) {
setAddress(addressRes[0]) setAddress(addressRes[0])
} }
// Load ticket-order history to enforce "address can be modified once per 30 days".
// If currently locked, force using last ticket-order address (snapshot) to avoid getting stuck with a new default address.
try {
const limit = await loadTicketAddressModifyLimit()
setTicketAddressModifyLimit(limit)
if (!limit.canModify && limit.lockedAddressId) {
const locked = await getShopUserAddress(limit.lockedAddressId)
if (locked?.id) setAddress(locked)
}
} catch (e) {
console.error('加载送水地址修改限制失败:', e)
setTicketAddressModifyLimit({ loaded: true, canModify: true })
}
// Tickets are non-blocking for first paint; load in background. // Tickets are non-blocking for first paint; load in background.
loadUserTickets() loadUserTickets()
} catch (err) { } catch (err) {
@@ -777,13 +960,13 @@ const OrderConfirm = () => {
{/* onClick={openStorePopup}*/} {/* onClick={openStorePopup}*/}
{/* />*/} {/* />*/}
{/*</CellGroup>*/} {/*</CellGroup>*/}
<CellGroup> <CellGroup>
{ {
address && ( address && (
<Cell <Cell
className={'address-bottom-line'} className={'address-bottom-line'}
onClick={() => Taro.navigateTo({ url: '/user/address/index' })} onClick={openAddressPage}
> >
<Space> <Space>
<Location className={'text-gray-500'}/> <Location className={'text-gray-500'}/>
<View className={'flex flex-col w-full justify-between items-start'}> <View className={'flex flex-col w-full justify-between items-start'}>
@@ -795,14 +978,22 @@ const OrderConfirm = () => {
<ArrowRight className={'text-gray-400'} size={14}/> <ArrowRight className={'text-gray-400'} size={14}/>
</View> </View>
</Space> </Space>
<View className={'pt-1 pb-3 text-gray-500'}>{address.name} {address.phone}</View> <View className={'pt-1 pb-3'}>
</View> <View className={'text-gray-500'}>{address.name} {address.phone}</View>
</Space> {ticketAddressModifyLimit.loaded && !ticketAddressModifyLimit.canModify && (
</Cell> <View className={'pt-1 text-xs text-orange-500 hidden'}>
) {ADDRESS_CHANGE_COOLDOWN_DAYS}
} {ticketAddressModifyLimit.nextAllowedText ? `${ticketAddressModifyLimit.nextAllowedText} 后可修改` : ''}
</View>
)}
</View>
</View>
</Space>
</Cell>
)
}
{!address && ( {!address && (
<Cell className={''} onClick={() => Taro.navigateTo({url: '/user/address/index'})}> <Cell className={''} onClick={openAddressPage}>
<Space> <Space>
<Location/> <Location/>