import { Button, Image, Input, Text, View } from '@tarojs/components' import Taro, { useDidShow, useLoad } from '@tarojs/taro' import { useMemo, useRef, useState } from 'react' import { getOrdersByPhone } from '@/api/pay' import { LOCAL_IMAGES } from '@/lib/assets' import { bootstrapSession } from '@/lib/bootstrap' import { COPY_ICON, PAGE_TITLE } from '@/lib/cdn' import { getNavMetrics } from '@/lib/nav' import { pickServiceContact, type ServiceContact } from '@/lib/contact' import { setOrderDraft } from '@/lib/orderDraft' import { filterOrdersForProduct, getOrderDisplayName, type OrderLike } from '@/lib/order' import { isValidPhone } from '@/lib/phone' import { getSession } from '@/lib/session' import { filterOrdersByTab, getOrderCardActions, getStatusMeta, ORDER_TABS, type OrderTabKey, } from '@/lib/status' import ContactModal, { ContactFab } from './contact' import DetailDrawer from './detail' import './index.scss' export default function Index () { const nav = useMemo(() => getNavMetrics(), []) const [ready, setReady] = useState(false) const [inputPhone, setInputPhone] = useState('') const [checking, setChecking] = useState(false) const [searched, setSearched] = useState(false) const [activeTab, setActiveTab] = useState('all') const [orders, setOrders] = useState([]) const [detailOrder, setDetailOrder] = useState(null) const [showContact, setShowContact] = useState(false) const [contact, setContact] = useState({ online: '', phone: '', weixin: '' }) const inputPhoneRef = useRef(inputPhone) const searchedRef = useRef(searched) inputPhoneRef.current = inputPhone searchedRef.current = searched useLoad(async (options) => { try { const session = await bootstrapSession(options || {}) setInputPhone(session.phone) setContact(pickServiceContact(session.config)) setReady(true) if (session.phone) { await queryOrders(session.phone) } } catch (error) { Taro.showToast({ title: error instanceof Error ? error.message : '初始化失败', icon: 'none', }) } }) async function queryOrders (phone: string) { if (!phone) { Taro.showToast({ title: '请输入联系电话', icon: 'none' }) return } if (!isValidPhone(phone)) { Taro.showToast({ title: '请填写正确联系电话', icon: 'none' }) return } setChecking(true) try { const res = await getOrdersByPhone(phone) const nextOrders = filterOrdersForProduct(getSession().goodsType, res.data || []) setOrders(nextOrders) setSearched(true) } catch { Taro.showToast({ title: '当前无法查询,请稍后再试', icon: 'none' }) } finally { setChecking(false) } } useDidShow(() => { if (searchedRef.current && inputPhoneRef.current) { void queryOrders(inputPhoneRef.current) } }) const visibleOrders = filterOrdersByTab(orders, activeTab) const emptyText = searched && orders.length === 0 ? '该手机号未查询到订单,请联系客服处理' : '暂无订单列表' const openOrderPage = (path: string, order: OrderLike) => { setOrderDraft(order) Taro.navigateTo({ url: path }) } return ( {PAGE_TITLE} setInputPhone(event.detail.value.replace(/\D/g, ''))} /> {inputPhone ? ( setInputPhone('')} /> ) : null} {ORDER_TABS.map((tab) => ( setActiveTab(tab.key)} > {tab.label} {activeTab === tab.key ? : null} ))} {visibleOrders.length ? ( {visibleOrders.map((order, index) => ( { if (!order.transaction_id) return Taro.setClipboardData({ data: String(order.transaction_id) }) }} onViewDetail={() => setDetailOrder(order)} onResubmit={() => openOrderPage('/pages/resubmit/index', order)} onComplaint={() => openOrderPage('/pages/complaint/index', order)} /> ))} ) : ( {emptyText} )} setShowContact(true)} /> {detailOrder ? ( setDetailOrder(null)} /> ) : null} setShowContact(false)} /> ) } function OrderCard ({ order, onCopy, onViewDetail, onResubmit, onComplaint, }: { order: OrderLike onCopy: () => void onViewDetail: () => void onResubmit: () => void onComplaint: () => void }) { const status = getStatusMeta(order.process_status, order.process_status_name) const actions = getOrderCardActions(order) return ( {getOrderDisplayName(order)} {status.result} 交易单号: {order.transaction_id || '-'} {order.transaction_id ? ( ) : null} 订单金额: ¥{order.total_fee || '-'} {order.reject_reason ? ( 驳回理由:{order.reject_reason} ) : null} {actions.complaint || actions.submit || actions.detail ? ( <> {actions.complaint ? ( 投诉 ) : null} {actions.detail ? ( 查看详情 ) : null} {actions.submit ? ( 补充资料 ) : null} ) : null} ) } function InfoLine ({ label, value }: { label: string, value: string }) { return ( {label} {value} ) }