fix(order): 修复订单列表页登录后仍看到别人订单问题

- 使用 userIdRef 避免闭包过期,确保加载订单时 userId 正确
- loadOrders 函数开头增加硬拦截,没有 userId 则不加载订单
- useEffect 中添加 user?.userId 判断,等待有效 userId 再加载
- useEffect 依赖数组加入 user?.userId,userId 变化时重新加载订单

feat(index): 首页铃铛接入新订单轮询与提示

- 登录后获取店员身份,使用 isClerkRef 避免闭包问题
- 通过 useNewOrderDetector 每30秒轮询门店订单,仅店员有效
- 新订单时触发震动和提示,铃铛数字角标动态显示新订单数
- 铃铛点击区分店员和普通用户,店员跳门店订单页并清除未读
- 页面展示时刷新店员身份,确保身份状态及时更新
This commit is contained in:
2026-07-14 02:05:26 +08:00
parent 596fa27d60
commit 7a14272fe8
3 changed files with 124 additions and 6 deletions

View File

@@ -58,6 +58,10 @@ const OrderListPage: React.FC = () => {
const tabStatesRef = useRef(tabStates)
tabStatesRef.current = tabStates
// userId 也用 ref防止闭包过期导致登录后仍传 undefined
const userIdRef = useRef(user?.userId)
userIdRef.current = user?.userId
const updateTabState = useCallback(
(index: number, partial: Partial<TabState>) => {
setTabStates(prev => {
@@ -71,13 +75,17 @@ const OrderListPage: React.FC = () => {
const loadOrders = useCallback(
async (targetIndex: number, p: number) => {
// 没有当前登录用户 userId禁止加载防止看到别人的订单
const userId = userIdRef.current
if (!userId) return
const state = tabStatesRef.current[targetIndex]
if (state.loading) return
updateTabState(targetIndex, { loading: true })
try {
const params: ShopOrderParam = { page: p, limit: 10, userId: user?.userId }
const params: ShopOrderParam = { page: p, limit: 10, userId }
const status = tabList[targetIndex]?.status
// 使用后端 statusFilter 字段进行筛选
@@ -109,10 +117,12 @@ const OrderListPage: React.FC = () => {
requireLogin({ action: 'viewOrder', redirect: '/pages/order/list', content: '登录后才能查看订单,是否前往登录?' })
return
}
// 已登录但 userId 还没拿到,等下次渲染再加载
if (!user?.userId) return
if (!tabStates[tabIndex].initialized) {
loadOrders(tabIndex, 1)
}
}, [tabIndex, isLoggedIn])
}, [tabIndex, isLoggedIn, user?.userId])
const handleLoadMore = () => {
if (!currentState.finished && !currentState.loading) {