Blame view

src/pages/Prepaid/components/PointsExchangeModal.tsx 3.54 KB
1
2
3
4
5
6
7
import { postIntegralExchangeIntegral } from '@/services/request';
import {
  ModalForm,
  ProFormInstance,
  ProFormTextArea,
} from '@ant-design/pro-components';
import { Form, Input, message } from 'antd';
8
import React, { useEffect, useRef, useState } from 'react';
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 || '';
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  const [accountPoints, setAccountPoints] = useState<number>(0);

  useEffect(() => {
    if (userInfoObj && userInfoObj.delta) {
      setAccountPoints(Number(userInfoObj.delta) || 0);
    }
  }, [userInfoObj]);

  // Validate that delta is not greater than available points
  const validateDelta = (rule: any, value: string) => {
    const deltaValue = Number(value);
    if (isNaN(deltaValue)) {
      return Promise.reject('请输入有效的积分数值');
    }
    if (deltaValue <= 0) {
      return Promise.reject('兑换积分必须大于0');
    }
    if (deltaValue > accountPoints) {
      return Promise.reject('兑换积分不能大于账户积分');
    }
    return Promise.resolve();
  };
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

  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, // 兑换说明
86
                relationEntityType: 'USER', // 关联实体类型
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
                createByName: userName, // 使用昵称或真实姓名作为createByName
              },
            });

            message.success('积分兑换成功');
            setVisible(false);
            onClose();
            return true;
          } catch (error) {
            console.error(error);
            return false;
          }
        }}
        onOpenChange={setVisible}
      >
        <Form.Item
          label="兑换积分"
          name="delta"
105
106
107
108
          rules={[
            { required: true, message: '请输入兑换积分' },
            { validator: validateDelta },
          ]}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        >
          <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;