PaymentSetting.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React, { useEffect, useState } from 'react';
  2. import { Card, Spin } from '@douyinfe/semi-ui';
  3. import SettingsGeneralPayment from '../../pages/Setting/Payment/SettingsGeneralPayment.js';
  4. import SettingsPaymentGateway from '../../pages/Setting/Payment/SettingsPaymentGateway.js';
  5. import { API, showError } from '../../helpers';
  6. import { useTranslation } from 'react-i18next';
  7. const PaymentSetting = () => {
  8. const { t } = useTranslation();
  9. let [inputs, setInputs] = useState({
  10. ServerAddress: '',
  11. PayAddress: '',
  12. EpayId: '',
  13. EpayKey: '',
  14. Price: 7.3,
  15. MinTopUp: 1,
  16. TopupGroupRatio: '',
  17. CustomCallbackAddress: '',
  18. PayMethods: '',
  19. });
  20. let [loading, setLoading] = useState(false);
  21. const getOptions = async () => {
  22. const res = await API.get('/api/option/');
  23. const { success, message, data } = res.data;
  24. if (success) {
  25. let newInputs = {};
  26. data.forEach((item) => {
  27. switch (item.key) {
  28. case 'TopupGroupRatio':
  29. try {
  30. newInputs[item.key] = JSON.stringify(JSON.parse(item.value), null, 2);
  31. } catch (error) {
  32. console.error('解析TopupGroupRatio出错:', error);
  33. newInputs[item.key] = item.value;
  34. }
  35. break;
  36. case 'Price':
  37. case 'MinTopUp':
  38. newInputs[item.key] = parseFloat(item.value);
  39. break;
  40. default:
  41. if (item.key.endsWith('Enabled')) {
  42. newInputs[item.key] = item.value === 'true' ? true : false;
  43. } else {
  44. newInputs[item.key] = item.value;
  45. }
  46. break;
  47. }
  48. });
  49. setInputs(newInputs);
  50. } else {
  51. showError(t(message));
  52. }
  53. };
  54. async function onRefresh() {
  55. try {
  56. setLoading(true);
  57. await getOptions();
  58. } catch (error) {
  59. showError(t('刷新失败'));
  60. } finally {
  61. setLoading(false);
  62. }
  63. }
  64. useEffect(() => {
  65. onRefresh();
  66. }, []);
  67. return (
  68. <>
  69. <Spin spinning={loading} size='large'>
  70. <Card style={{ marginTop: '10px' }}>
  71. <SettingsGeneralPayment options={inputs} refresh={onRefresh} />
  72. </Card>
  73. <Card style={{ marginTop: '10px' }}>
  74. <SettingsPaymentGateway options={inputs} refresh={onRefresh} />
  75. </Card>
  76. </Spin>
  77. </>
  78. );
  79. };
  80. export default PaymentSetting;