TransferDetailDialog.tsx 6.35 KB
import { useQuery } from '@tanstack/react-query';
import { UButton, UModal, UText, UTitle, UTooltip, useUMessage } from '@uj/ui';
import { withdrawApi, type WithdrawRow } from '@/api';
import {
  PAY_MODE,
  TRANSFER_STATUS,
  WITHDRAWAL_SOURCE,
  WITHDRAWAL_TYPE,
  findChannelNode,
  optionLabel,
  operationLabel,
  statusMeta,
} from '@/features/shared/dictionaries';
import { centsToYuan, dateTimeOr, EMPTY, numberOr, textOr } from '@/features/shared/format';
import { useChannelTree } from '@/features/shared/options';
import { DetailGrid, type DetailValue } from '@/features/shared/DetailCell';

export interface TransferDetailDialogProps {
  open: boolean;
  /** 老系统弹窗只传 id,内容由 getById 再查一次。 */
  id?: string;
  onClose: () => void;
}

/** 复制失败原因:老系统用 execCommand,这里用剪贴板 API 并按结果说话。 */
async function copyText(text: string): Promise<boolean> {
  try {
    await navigator.clipboard.writeText(text);
    return true;
  } catch {
    return false;
  }
}

/**
 * 提现详情弹窗:三段内容与字段顺序逐字对齐老系统 detail.vue(订单信息 11 项、付款信息 13 项、
 * 操作记录按条铺开),失败原因只在状态为「失败」时出现。
 */
export function TransferDetailDialog({ open, id, onClose }: TransferDetailDialogProps) {
  const message = useUMessage();
  const channels = useChannelTree(open);
  const detail = useQuery({
    queryKey: ['points', 'transfer', id ?? ''],
    queryFn: () => withdrawApi.transferDetail(String(id)),
    enabled: open && !!id,
  });
  const row = detail.data as WithdrawRow | undefined;

  const orderCells: DetailValue[] = row
    ? [
        { label: '提现单号', value: textOr(row.outSettleOrderId), code: true },
        { label: '提现类型', value: optionLabel(WITHDRAWAL_TYPE, row.transferType) || EMPTY },
        { label: '提现方式', value: optionLabel(PAY_MODE, row.mode) || EMPTY },
        { label: '提现金额', value: centsToYuan(row.payAmount) },
        { label: '客户名称', value: textOr(row.customerName) },
        { label: '客户手机', value: textOr(row.customerPhone), code: true },
        { label: '提现积分', value: numberOr(row.integralAmount) },
        { label: '所属门店', value: textOr(row.merchantName) },
        { label: '平台方', value: textOr(row.realityMerchantName) },
        { label: '申请时间', value: dateTimeOr(row.createdDate) },
        { label: '来源', value: optionLabel(WITHDRAWAL_SOURCE, row.withdrawalSource) || EMPTY },
      ]
    : [];
  const paymentCells: DetailValue[] = row
    ? [
        { label: '收款人姓名', value: textOr(row.name) },
        { label: '收款人身份证', value: textOr(row.idCardNo), code: true },
        { label: '收款银行卡号', value: textOr(row.recvBankCardNo), code: true },
        { label: '收款银行', value: textOr(row.recvBank) },
        { label: '收款手机号', value: textOr(row.mobile), code: true },
        { label: '微信openid', value: textOr(row.customerOpenId), code: true },
        {
          label: '付款渠道',
          // 接口只回渠道 code,名字要在渠道树里递归找(与列表同一份树)。
          value: textOr(findChannelNode(channels.nodes, row.channel)?.name),
        },
        { label: '付款主体', value: textOr(row.paymentEntityName) },
        { label: '代征主体', value: textOr(row.levyName) },
        { label: '银行渠道', value: textOr(row.levyChannelName) },
        { label: '结算金额', value: centsToYuan(row.settleAmount) },
        { label: '到账时间', value: dateTimeOr(row.receiptDate) },
        { label: '状态', value: '', tag: statusMeta(TRANSFER_STATUS, row.status) },
      ]
    : [];

  return (
    <UModal title="详细信息" open={open} footer={null} width={900} onCancel={onClose}>
      {detail.isError ? (
        <UText type="danger">提现记录加载失败:{detail.error.message}</UText>
      ) : (
        <div className="uj-points-detail">
          <UTitle level={5}>订单信息</UTitle>
          <DetailGrid cells={orderCells} />
          <UTitle level={5}>付款信息</UTitle>
          <DetailGrid cells={paymentCells} />
          {Number(row?.status) === 4 ? (
            <div className="uj-points-error-row">
              <span className="uj-points-error-label">失败原因</span>
              <UTooltip title={row?.errorMessage}>
                <span className="uj-points-error-text">{textOr(row?.errorMessage)}</span>
              </UTooltip>
              <UButton
                type="link"
                onClick={() => {
                  void copyText(row?.errorMessage ?? '').then((ok) =>
                    message[ok ? 'success' : 'error'](ok ? '已复制' : '复制失败,请手动选择'),
                  );
                }}
              >
                复制
              </UButton>
            </div>
          ) : null}
          {row?.operationList?.length ? (
            <>
              <UTitle level={5}>操作记录</UTitle>
              {row.operationList.map((record, index) => {
                const isReject = Number(record.result) === 1;
                const result = isReject
                  ? { label: '驳回', color: 'red' }
                  : Number(record.result) === 2
                    ? { label: '失败', color: 'default' }
                    : { label: '通过', color: 'green' };
                return (
                  <DetailGrid
                    key={`${record.createdDate ?? ''}-${index}`}
                    cells={[
                      { label: operationLabel(record.type, 'name') || '操作', tag: result },
                      {
                        label: isReject
                          ? '操作人'
                          : operationLabel(record.type, 'person') || '操作人',
                        value: textOr(record.createdBy),
                      },
                      {
                        label: isReject
                          ? '操作时间'
                          : operationLabel(record.type, 'time') || '操作时间',
                        value: dateTimeOr(record.createdDate),
                      },
                      { label: '备注', value: textOr(record.remark) },
                    ]}
                  />
                );
              })}
            </>
          ) : null}
        </div>
      )}
    </UModal>
  );
}