SettingGlobalModel.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin, Banner } from '@douyinfe/semi-ui';
  3. import {
  4. compareObjects,
  5. API,
  6. showError,
  7. showSuccess,
  8. showWarning,
  9. verifyJSON,
  10. } from '../../../helpers';
  11. import { useTranslation } from 'react-i18next';
  12. export default function SettingGlobalModel(props) {
  13. const { t } = useTranslation();
  14. const [loading, setLoading] = useState(false);
  15. const [inputs, setInputs] = useState({
  16. 'global.pass_through_request_enabled': false,
  17. 'general_setting.ping_interval_enabled': false,
  18. 'general_setting.ping_interval_seconds': 60,
  19. });
  20. const refForm = useRef();
  21. const [inputsRow, setInputsRow] = useState(inputs);
  22. function onSubmit() {
  23. const updateArray = compareObjects(inputs, inputsRow);
  24. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  25. const requestQueue = updateArray.map((item) => {
  26. let value = String(inputs[item.key]);
  27. return API.put('/api/option/', {
  28. key: item.key,
  29. value,
  30. });
  31. });
  32. setLoading(true);
  33. Promise.all(requestQueue)
  34. .then((res) => {
  35. if (requestQueue.length === 1) {
  36. if (res.includes(undefined)) return;
  37. } else if (requestQueue.length > 1) {
  38. if (res.includes(undefined))
  39. return showError(t('部分保存失败,请重试'));
  40. }
  41. showSuccess(t('保存成功'));
  42. props.refresh();
  43. })
  44. .catch(() => {
  45. showError(t('保存失败,请重试'));
  46. })
  47. .finally(() => {
  48. setLoading(false);
  49. });
  50. }
  51. useEffect(() => {
  52. const currentInputs = {};
  53. for (let key in props.options) {
  54. if (Object.keys(inputs).includes(key)) {
  55. currentInputs[key] = props.options[key];
  56. }
  57. }
  58. setInputs(currentInputs);
  59. setInputsRow(structuredClone(currentInputs));
  60. refForm.current.setValues(currentInputs);
  61. }, [props.options]);
  62. return (
  63. <>
  64. <Spin spinning={loading}>
  65. <Form
  66. values={inputs}
  67. getFormApi={(formAPI) => (refForm.current = formAPI)}
  68. style={{ marginBottom: 15 }}
  69. >
  70. <Form.Section text={t('全局设置')}>
  71. <Row>
  72. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  73. <Form.Switch
  74. label={t('启用请求透传')}
  75. field={'global.pass_through_request_enabled'}
  76. onChange={(value) =>
  77. setInputs({
  78. ...inputs,
  79. 'global.pass_through_request_enabled': value,
  80. })
  81. }
  82. extraText={
  83. '开启后,所有请求将直接透传给上游,不会进行任何处理(重定向和渠道适配也将失效),请谨慎开启'
  84. }
  85. />
  86. </Col>
  87. </Row>
  88. <Form.Section text={t('连接保活设置')}>
  89. <Row style={{ marginTop: 10 }}>
  90. <Col span={24}>
  91. <Banner
  92. type="warning"
  93. description="警告:启用保活后,如果已经写入保活数据后渠道出错,系统无法重试,如果必须开启,推荐设置尽可能大的Ping间隔"
  94. />
  95. </Col>
  96. </Row>
  97. <Row>
  98. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  99. <Form.Switch
  100. label={t('启用Ping间隔')}
  101. field={'general_setting.ping_interval_enabled'}
  102. onChange={(value) => setInputs({ ...inputs, 'general_setting.ping_interval_enabled': value })}
  103. extraText={'开启后,将定期发送ping数据保持连接活跃'}
  104. />
  105. </Col>
  106. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  107. <Form.InputNumber
  108. label={t('Ping间隔(秒)')}
  109. field={'general_setting.ping_interval_seconds'}
  110. onChange={(value) => setInputs({ ...inputs, 'general_setting.ping_interval_seconds': value })}
  111. min={1}
  112. disabled={!inputs['general_setting.ping_interval_enabled']}
  113. />
  114. </Col>
  115. </Row>
  116. </Form.Section>
  117. <Row>
  118. <Button size='default' onClick={onSubmit}>
  119. {t('保存')}
  120. </Button>
  121. </Row>
  122. </Form.Section>
  123. </Form>
  124. </Spin>
  125. </>
  126. );
  127. }