PointsExchangeModal.tsx 2.7 KB
import { postIntegralExchangeIntegral } from '@/services/request';
import {
  ModalForm,
  ProFormInstance,
  ProFormTextArea,
} from '@ant-design/pro-components';
import { Form, Input, message } from 'antd';
import React, { useRef } from 'react';
import '../index.less';

interface PointsExchangeModalProps {
  setVisible: (visible: boolean) => void;
  userInfoObj: {
    uid: string;
    nickname?: string;
    realName?: string;
    [key: string]: any;
  };
  onClose: () => void;
}

const PointsExchangeModal: React.FC<PointsExchangeModalProps> = ({
  setVisible,
  userInfoObj,
  onClose,
}) => {
  const [form] = Form.useForm<{ delta: string; remark: string }>();
  const formRef = useRef<ProFormInstance>();
  const uid = userInfoObj?.uid;
  const userName = userInfoObj?.nickname || userInfoObj?.realName || '';

  return (
    <div className="prepaid-index">
      <ModalForm<{
        delta: string;
        remark: string;
      }>
        width={600}
        open
        title="积分兑换"
        form={form}
        formRef={formRef}
        autoFocusFirstInput
        submitter={{
          searchConfig: {
            submitText: '确认兑换',
            resetText: '取消',
          },
        }}
        modalProps={{
          destroyOnClose: true,
          onCancel: () => {
            setVisible(false);
          },
        }}
        onFinish={async (values) => {
          try {
            // 调用积分兑换API
            await postIntegralExchangeIntegral({
              data: {
                id: Number(uid), // 使用用户的uid作为id参数
                delta: values.delta, // 将delta设为负数
                remark: values.remark, // 兑换说明
                createByName: userName, // 使用昵称或真实姓名作为createByName
              },
            });

            message.success('积分兑换成功');
            setVisible(false);
            onClose();
            return true;
          } catch (error) {
            console.error(error);
            return false;
          }
        }}
        onOpenChange={setVisible}
      >
        <Form.Item
          label="兑换积分"
          name="delta"
          rules={[{ required: true, message: '请输入兑换积分' }]}
        >
          <Input style={{ height: '30px' }} placeholder="请输入兑换积分数量" />
        </Form.Item>

        <Form.Item
          label="兑换说明"
          name="remark"
          rules={[{ required: true, message: '请输入兑换说明' }]}
        >
          <ProFormTextArea
            style={{ height: '100px' }}
            placeholder="请输入兑换说明"
          />
        </Form.Item>
      </ModalForm>
    </div>
  );
};

export default PointsExchangeModal;