SettingsLog.js 4.4 KB

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