import { useCallback, useEffect, useMemo, useState } from 'react' import Taro, { useDidShow } from '@tarojs/taro' import { View, Text } from '@tarojs/components' import { Button, ConfigProvider, Empty, Input, Loading } from '@nutui/nutui-react-taro' import { ArrowRight, Search } from '@nutui/icons-react-taro' import { pageCreditMpCustomer } from '@/api/credit/creditMpCustomer' import type { CreditMpCustomer } from '@/api/credit/creditMpCustomer/model' type ListQuery = { page: number limit: number keywords?: string userId?: number } const getCurrentUserId = (): number | undefined => { try { const raw = Taro.getStorageSync('UserId') const n = Number(raw) return Number.isFinite(n) && n > 0 ? n : undefined } catch { return undefined } } const buildDesc = (row: CreditMpCustomer) => { const price = row.price ? `${row.price}元` : '' const years = row.years ? `${row.years}年` : '' const location = [row.province, row.city, row.region].filter(Boolean).join(' ') return [price, years, location].filter(Boolean).join(' · ') } const getStatusBadgeClass = (s?: string) => { const txt = String(s || '').trim() if (!txt) return 'bg-gray-400' if (txt.includes('退回')) return 'bg-red-500' if (txt.includes('完结') || txt.includes('已办结')) return 'bg-green-600' if (txt.includes('受理') || txt.includes('通过') || txt.includes('签订')) return 'bg-orange-500' return 'bg-blue-500' } export default function CreditMyOrderPage() { const [list, setList] = useState([]) const [count, setCount] = useState(0) const [loading, setLoading] = useState(false) const [loadingMore, setLoadingMore] = useState(false) const [keywords, setKeywords] = useState('') const [page, setPage] = useState(1) const limit = 10 const userId = useMemo(() => getCurrentUserId(), []) const hasMore = useMemo(() => list.length < count, [count, list.length]) const fetchPage = useCallback( async (opts: { nextPage: number; replace: boolean }) => { if (!userId) { setList([]) setCount(0) return } const query: ListQuery = { page: opts.nextPage, limit, keywords: keywords.trim() || undefined, userId } try { if (opts.replace) setLoading(true) else setLoadingMore(true) const res = await pageCreditMpCustomer(query as any) const incoming = (res?.list || []) as CreditMpCustomer[] const total = Number(res?.count || 0) setCount(Number.isFinite(total) ? total : 0) setPage(opts.nextPage) setList(prev => (opts.replace ? incoming : prev.concat(incoming))) } catch (e) { console.error('获取我的需求失败:', e) Taro.showToast({ title: (e as any)?.message || '获取数据失败', icon: 'none' }) } finally { setLoading(false) setLoadingMore(false) } }, [keywords, limit, userId] ) const reload = useCallback(async () => { await fetchPage({ nextPage: 1, replace: true }) }, [fetchPage]) const loadMore = useCallback(async () => { if (loading || loadingMore || !hasMore) return await fetchPage({ nextPage: page + 1, replace: false }) }, [fetchPage, hasMore, loading, loadingMore, page]) useDidShow(() => { reload().then() }) useEffect(() => { const handler = () => reload() Taro.eventCenter.on('credit:order:created', handler) return () => { Taro.eventCenter.off('credit:order:created', handler) } }, [reload]) const goDetail = (id?: number) => { if (!id) return Taro.navigateTo({ url: `/credit/my-order/detail?id=${id}` }) } return ( 仅展示我发布的需求 共{count}条 {loading ? ( 加载中... ) : !list.length ? ( ) : ( list.map(row => { const title = String(row.toUser || '-').trim() const desc = buildDesc(row) const statusTxt = String(row.statusTxt || '处理中').trim() const time = String(row.createTime || '').slice(0, 19).replace('T', ' ') return ( goDetail(row.id)} > {title} {statusTxt} {!!desc && ( {desc} )} 需求ID:{row.id ?? '-'} {time || ''} ) }) )} {!!list.length && ( )} ) }