fix(credit): 解决客户分配功能在页面跳转时的执行时机问题
- 添加 pendingAssignRef 引用跟踪待分配状态 - 重构 useDidShow 钩子中的异步处理逻辑 - 实现从选择业务员页面返回后的延迟分配机制 - 修改 submitAssign 函数支持传入指定客户ID数组 - 更新页面跳转参数传递 token 验证机制 - 添加本地存储缓存选择结果的处理逻辑
This commit is contained in:
@@ -110,6 +110,7 @@ const getStepTagType = (code: number | null): any => {
|
|||||||
export default function CreditCompanyPage() {
|
export default function CreditCompanyPage() {
|
||||||
const serverPageRef = useRef(1)
|
const serverPageRef = useRef(1)
|
||||||
const staffLoadingPromiseRef = useRef<Promise<User[]> | null>(null)
|
const staffLoadingPromiseRef = useRef<Promise<User[]> | null>(null)
|
||||||
|
const pendingAssignRef = useRef<{ token: string; customerIds: number[] } | null>(null)
|
||||||
const [list, setList] = useState<CreditMpCustomer[]>([])
|
const [list, setList] = useState<CreditMpCustomer[]>([])
|
||||||
const [hasMore, setHasMore] = useState(true)
|
const [hasMore, setHasMore] = useState(true)
|
||||||
const [loading, setLoading] = useState(false)
|
const [loading, setLoading] = useState(false)
|
||||||
@@ -277,9 +278,37 @@ export default function CreditCompanyPage() {
|
|||||||
}, [hasMore, loading, reload])
|
}, [hasMore, loading, reload])
|
||||||
|
|
||||||
useDidShow(() => {
|
useDidShow(() => {
|
||||||
reload(true).then()
|
const run = async () => {
|
||||||
// 预加载员工列表,保证“跟进人(realName)”可展示
|
// 从“选择业务员页”返回:优先处理分配,避免在隐藏页触发 showModal/接口调用
|
||||||
ensureStaffLoaded().then()
|
const picked = safeParseJSON<any>(Taro.getStorageSync('credit_pageUserSelected'))
|
||||||
|
const pending = pendingAssignRef.current
|
||||||
|
|
||||||
|
if (pending?.token && picked?.token && String(picked.token) === pending.token) {
|
||||||
|
try {
|
||||||
|
Taro.removeStorageSync('credit_pageUserSelected')
|
||||||
|
} catch (_e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
const uid = Number(picked?.userId ?? picked?.user?.userId)
|
||||||
|
const name = String(picked?.userName || picked?.realName || picked?.nickname || picked?.username || '').trim()
|
||||||
|
pendingAssignRef.current = null
|
||||||
|
await submitAssign(uid, name, pending.customerIds)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 若用户取消选择,则保留当前勾选状态,不强制刷新/退出选择模式
|
||||||
|
if (pending) {
|
||||||
|
pendingAssignRef.current = null
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
await reload(true)
|
||||||
|
// 预加载员工列表,保证“跟进人(realName)”可展示
|
||||||
|
await ensureStaffLoaded()
|
||||||
|
}
|
||||||
|
|
||||||
|
run().then()
|
||||||
})
|
})
|
||||||
|
|
||||||
const visibleStatusOptions = useMemo(() => {
|
const visibleStatusOptions = useMemo(() => {
|
||||||
@@ -369,30 +398,27 @@ export default function CreditCompanyPage() {
|
|||||||
Taro.showToast({ title: '请先勾选客户', icon: 'none' })
|
Taro.showToast({ title: '请先勾选客户', icon: 'none' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 记录待分配的客户ID,返回后再执行分配(避免在隐藏页执行)
|
||||||
|
const token = `${Date.now()}_${Math.random().toString(36).slice(2)}`
|
||||||
|
pendingAssignRef.current = { token, customerIds: selectedIds.slice() }
|
||||||
|
try {
|
||||||
|
Taro.removeStorageSync('credit_pageUserSelected')
|
||||||
|
} catch (_e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
Taro.navigateTo({
|
Taro.navigateTo({
|
||||||
url: '/credit/pageUser/index?isAdmin=1&title=选择业务员',
|
url: `/credit/pageUser/index?isAdmin=1&title=选择业务员&token=${encodeURIComponent(token)}`
|
||||||
events: {
|
|
||||||
userSelected: (payload: any) => {
|
|
||||||
const uid = Number(payload?.userId ?? payload?.user?.userId)
|
|
||||||
if (!Number.isFinite(uid) || uid <= 0) return
|
|
||||||
const u = payload?.user as User | undefined
|
|
||||||
const pickedName = String(
|
|
||||||
payload?.realName ||
|
|
||||||
payload?.nickname ||
|
|
||||||
payload?.username ||
|
|
||||||
u?.realName ||
|
|
||||||
u?.nickname ||
|
|
||||||
u?.username ||
|
|
||||||
''
|
|
||||||
).trim()
|
|
||||||
submitAssign(uid, pickedName).then()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const submitAssign = async (userId: number, userName?: string) => {
|
const submitAssign = async (userId: number, userName?: string, customerIds?: number[]) => {
|
||||||
if (!selectedIds.length) {
|
const ids = (customerIds && customerIds.length ? customerIds : selectedIds).filter(v => Number.isFinite(v) && v > 0)
|
||||||
|
if (!Number.isFinite(userId) || userId <= 0) {
|
||||||
|
Taro.showToast({ title: '请选择分配对象', icon: 'none' })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!ids.length) {
|
||||||
Taro.showToast({ title: '请先勾选客户', icon: 'none' })
|
Taro.showToast({ title: '请先勾选客户', icon: 'none' })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -402,11 +428,11 @@ export default function CreditCompanyPage() {
|
|||||||
const staffName = String(userName || '').trim() || staffNameMap.get(Number(userId)) || `员工${userId}`
|
const staffName = String(userName || '').trim() || staffNameMap.get(Number(userId)) || `员工${userId}`
|
||||||
const confirmRes = await Taro.showModal({
|
const confirmRes = await Taro.showModal({
|
||||||
title: '确认分配',
|
title: '确认分配',
|
||||||
content: `确定将 ${selectedIds.length} 个客户分配给「${staffName}」吗?`
|
content: `确定将 ${ids.length} 个客户分配给「${staffName}」吗?`
|
||||||
})
|
})
|
||||||
if (!confirmRes.confirm) return
|
if (!confirmRes.confirm) return
|
||||||
|
|
||||||
for (const id of selectedIds) {
|
for (const id of ids) {
|
||||||
const detail = await getCreditMpCustomer(Number(id))
|
const detail = await getCreditMpCustomer(Number(id))
|
||||||
await updateCreditMpCustomer({ ...(detail || {}), id, userId } as any)
|
await updateCreditMpCustomer({ ...(detail || {}), id, userId } as any)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export default function PageUserSelectPage() {
|
|||||||
const n = Number(params?.isAdmin)
|
const n = Number(params?.isAdmin)
|
||||||
return Number.isFinite(n) ? n : undefined
|
return Number.isFinite(n) ? n : undefined
|
||||||
}, [params?.isAdmin])
|
}, [params?.isAdmin])
|
||||||
|
const token = useMemo(() => String(params?.token || ''), [params?.token])
|
||||||
|
|
||||||
const [list, setList] = useState<User[]>([])
|
const [list, setList] = useState<User[]>([])
|
||||||
const [page, setPage] = useState(1)
|
const [page, setPage] = useState(1)
|
||||||
@@ -71,9 +72,18 @@ export default function PageUserSelectPage() {
|
|||||||
|
|
||||||
const onPick = (u: User) => {
|
const onPick = (u: User) => {
|
||||||
if (!u?.userId) return
|
if (!u?.userId) return
|
||||||
|
const userName = String(u.realName || u.nickname || u.username || `用户${u.userId}`)
|
||||||
|
const payload = { token, userId: u.userId, userName, user: u }
|
||||||
|
try {
|
||||||
|
Taro.setStorageSync('credit_pageUserSelected', JSON.stringify(payload))
|
||||||
|
} catch (_e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
|
||||||
const pageInst: any = Taro.getCurrentInstance().page
|
const pageInst: any = Taro.getCurrentInstance().page
|
||||||
const eventChannel = pageInst?.getOpenerEventChannel?.()
|
const eventChannel = pageInst?.getOpenerEventChannel?.()
|
||||||
eventChannel?.emit('userSelected', { userId: u.userId, user: u })
|
eventChannel?.emit('userSelected', payload)
|
||||||
|
Taro.eventCenter?.trigger?.('credit:pageUserSelected', payload)
|
||||||
Taro.navigateBack()
|
Taro.navigateBack()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user