SettingsLog.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Button, Col, Form, Row, Spin, DatePicker } from '@douyinfe/semi-ui';
  3. import dayjs from 'dayjs';
  4. import { useTranslation } from 'react-i18next';
  5. import {
  6. compareObjects,
  7. API,
  8. showError,
  9. showSuccess,
  10. showWarning,
  11. } from '../../../helpers';
  12. export default function SettingsLog(props) {
  13. const { t } = useTranslation();
  14. const [loading, setLoading] = useState(false);
  15. const [loadingCleanHistoryLog, setLoadingCleanHistoryLog] = useState(false);
  16. const [inputs, setInputs] = useState({
  17. LogConsumeEnabled: false,
  18. historyTimestamp: dayjs().subtract(1, 'month').toDate(),
  19. });
  20. const refForm = useRef();
  21. const [inputsRow, setInputsRow] = useState(inputs);
  22. function onSubmit() {
  23. const updateArray = compareObjects(inputs, inputsRow).filter(
  24. (item) => item.key !== 'historyTimestamp',
  25. );
  26. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  27. const requestQueue = updateArray.map((item) => {
  28. let value = '';
  29. if (typeof inputs[item.key] === 'boolean') {
  30. value = String(inputs[item.key]);
  31. } else {
  32. value = inputs[item.key];
  33. }
  34. return API.put('/api/option/', {
  35. key: item.key,
  36. value,
  37. });
  38. });
  39. setLoading(true);
  40. Promise.all(requestQueue)
  41. .then((res) => {
  42. if (requestQueue.length === 1) {
  43. if (res.includes(undefined)) return;
  44. } else if (requestQueue.length > 1) {
  45. if (res.includes(undefined))
  46. return showError(t('部分保存失败,请重试'));
  47. }
  48. showSuccess(t('保存成功'));
  49. props.refresh();
  50. })
  51. .catch(() => {
  52. showError(t('保存失败,请重试'));
  53. })
  54. .finally(() => {
  55. setLoading(false);
  56. });
  57. }
  58. async function onCleanHistoryLog() {
  59. try {
  60. setLoadingCleanHistoryLog(true);
  61. if (!inputs.historyTimestamp) throw new Error(t('请选择日志记录时间'));
  62. const res = await API.delete(
  63. `/api/log/?target_timestamp=${Date.parse(inputs.historyTimestamp) / 1000}`,
  64. );
  65. const { success, message, data } = res.data;
  66. if (success) {
  67. showSuccess(`${data} ${t('条日志已清理!')}`);
  68. return;
  69. } else {
  70. throw new Error(t('日志清理失败:') + message);
  71. }
  72. } catch (error) {
  73. showError(error.message);
  74. } finally {
  75. setLoadingCleanHistoryLog(false);
  76. }
  77. }
  78. useEffect(() => {
  79. const currentInputs = {};
  80. for (let key in props.options) {
  81. if (Object.keys(inputs).includes(key)) {
  82. currentInputs[key] = props.options[key];
  83. }
  84. }
  85. currentInputs['historyTimestamp'] = inputs.historyTimestamp;
  86. setInputs(Object.assign(inputs, currentInputs));
  87. setInputsRow(structuredClone(currentInputs));
  88. refForm.current.setValues(currentInputs);
  89. }, [props.options]);
  90. return (
  91. <>
  92. <Spin spinning={loading}>
  93. <Form
  94. values={inputs}
  95. getFormApi={(formAPI) => (refForm.current = formAPI)}
  96. style={{ marginBottom: 15 }}
  97. >
  98. <Form.Section text={t('日志设置')}>
  99. <Row gutter={16}>
  100. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  101. <Form.Switch
  102. field={'LogConsumeEnabled'}
  103. label={t('启用额度消费日志记录')}
  104. size='default'
  105. checkedText='|'
  106. uncheckedText='〇'
  107. onChange={(value) => {
  108. setInputs({
  109. ...inputs,
  110. LogConsumeEnabled: value,
  111. });
  112. }}
  113. />
  114. </Col>
  115. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  116. <Spin spinning={loadingCleanHistoryLog}>
  117. <Form.DatePicker
  118. label={t('日志记录时间')}
  119. field={'historyTimestamp'}
  120. type='dateTime'
  121. inputReadOnly={true}
  122. onChange={(value) => {
  123. setInputs({
  124. ...inputs,
  125. historyTimestamp: value,
  126. });
  127. }}
  128. />
  129. <Button size='default' onClick={onCleanHistoryLog}>
  130. {t('清除历史日志')}
  131. </Button>
  132. </Spin>
  133. </Col>
  134. </Row>
  135. <Row>
  136. <Button size='default' onClick={onSubmit}>
  137. {t('保存日志设置')}
  138. </Button>
  139. </Row>
  140. </Form.Section>
  141. </Form>
  142. </Spin>
  143. </>
  144. );
  145. }