SettingsGeneral.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import React, { useEffect, useState, useRef } from 'react';
  2. import { Banner, Button, Col, Form, Row, Spin, Collapse, Modal } from '@douyinfe/semi-ui';
  3. import {
  4. compareObjects,
  5. API,
  6. showError,
  7. showSuccess,
  8. showWarning,
  9. } from '../../../helpers';
  10. import { useTranslation } from 'react-i18next';
  11. export default function GeneralSettings(props) {
  12. const { t } = useTranslation();
  13. const [loading, setLoading] = useState(false);
  14. const [showQuotaWarning, setShowQuotaWarning] = useState(false);
  15. const [inputs, setInputs] = useState({
  16. TopUpLink: '',
  17. 'general_setting.docs_link': '',
  18. QuotaPerUnit: '',
  19. RetryTimes: '',
  20. DisplayInCurrencyEnabled: false,
  21. DisplayTokenStatEnabled: false,
  22. DefaultCollapseSidebar: false,
  23. DemoSiteEnabled: false,
  24. SelfUseModeEnabled: false,
  25. });
  26. const refForm = useRef();
  27. const [inputsRow, setInputsRow] = useState(inputs);
  28. function handleFieldChange(fieldName) {
  29. return (value) => {
  30. setInputs((inputs) => ({ ...inputs, [fieldName]: value }));
  31. };
  32. }
  33. function onSubmit() {
  34. const updateArray = compareObjects(inputs, inputsRow);
  35. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  36. const requestQueue = updateArray.map((item) => {
  37. let value = '';
  38. if (typeof inputs[item.key] === 'boolean') {
  39. value = String(inputs[item.key]);
  40. } else {
  41. value = inputs[item.key];
  42. }
  43. return API.put('/api/option/', {
  44. key: item.key,
  45. value,
  46. });
  47. });
  48. setLoading(true);
  49. Promise.all(requestQueue)
  50. .then((res) => {
  51. if (requestQueue.length === 1) {
  52. if (res.includes(undefined)) return;
  53. } else if (requestQueue.length > 1) {
  54. if (res.includes(undefined)) return showError(t('部分保存失败,请重试'));
  55. }
  56. showSuccess(t('保存成功'));
  57. props.refresh();
  58. })
  59. .catch(() => {
  60. showError(t('保存失败,请重试'));
  61. })
  62. .finally(() => {
  63. setLoading(false);
  64. });
  65. }
  66. useEffect(() => {
  67. const currentInputs = {};
  68. for (let key in props.options) {
  69. if (Object.keys(inputs).includes(key)) {
  70. currentInputs[key] = props.options[key];
  71. }
  72. }
  73. setInputs(currentInputs);
  74. setInputsRow(structuredClone(currentInputs));
  75. refForm.current.setValues(currentInputs);
  76. }, [props.options]);
  77. return (
  78. <>
  79. <Spin spinning={loading}>
  80. <Banner
  81. type='warning'
  82. description={t('聊天链接功能已经弃用,请使用下方聊天设置功能')}
  83. />
  84. <Form
  85. values={inputs}
  86. getFormApi={(formAPI) => (refForm.current = formAPI)}
  87. style={{ marginBottom: 15 }}
  88. >
  89. <Form.Section text={t('通用设置')}>
  90. <Row gutter={16}>
  91. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  92. <Form.Input
  93. field={'TopUpLink'}
  94. label={t('充值链接')}
  95. initValue={''}
  96. placeholder={t('例如发卡网站的购买链接')}
  97. onChange={handleFieldChange('TopUpLink')}
  98. showClear
  99. />
  100. </Col>
  101. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  102. <Form.Input
  103. field={'general_setting.docs_link'}
  104. label={t('文档地址')}
  105. initValue={''}
  106. placeholder={t('例如 https://docs.newapi.pro')}
  107. onChange={handleFieldChange('general_setting.docs_link')}
  108. showClear
  109. />
  110. </Col>
  111. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  112. <Form.Input
  113. field={'QuotaPerUnit'}
  114. label={t('单位美元额度')}
  115. initValue={''}
  116. placeholder={t('一单位货币能兑换的额度')}
  117. onChange={handleFieldChange('QuotaPerUnit')}
  118. showClear
  119. onClick={() => setShowQuotaWarning(true)}
  120. />
  121. </Col>
  122. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  123. <Form.Input
  124. field={'RetryTimes'}
  125. label={t('失败重试次数')}
  126. initValue={''}
  127. placeholder={t('失败重试次数')}
  128. onChange={handleFieldChange('RetryTimes')}
  129. showClear
  130. />
  131. </Col>
  132. </Row>
  133. <Row gutter={16}>
  134. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  135. <Form.Switch
  136. field={'DisplayInCurrencyEnabled'}
  137. label={t('以货币形式显示额度')}
  138. size='default'
  139. checkedText='|'
  140. uncheckedText='〇'
  141. onChange={handleFieldChange('DisplayInCurrencyEnabled')}
  142. />
  143. </Col>
  144. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  145. <Form.Switch
  146. field={'DisplayTokenStatEnabled'}
  147. label={t('额度查询接口返回令牌额度而非用户额度')}
  148. size='default'
  149. checkedText='|'
  150. uncheckedText='〇'
  151. onChange={handleFieldChange('DisplayTokenStatEnabled')}
  152. />
  153. </Col>
  154. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  155. <Form.Switch
  156. field={'DefaultCollapseSidebar'}
  157. label={t('默认折叠侧边栏')}
  158. size='default'
  159. checkedText='|'
  160. uncheckedText='〇'
  161. onChange={handleFieldChange('DefaultCollapseSidebar')}
  162. />
  163. </Col>
  164. </Row>
  165. <Row>
  166. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  167. <Form.Switch
  168. field={'DemoSiteEnabled'}
  169. label={t('演示站点模式')}
  170. size='default'
  171. checkedText='|'
  172. uncheckedText='〇'
  173. onChange={handleFieldChange('DemoSiteEnabled')}
  174. />
  175. </Col>
  176. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  177. <Form.Switch
  178. field={'SelfUseModeEnabled'}
  179. label={t('自用模式')}
  180. extraText={t('开启后不限制:必须设置模型倍率')}
  181. size='default'
  182. checkedText='|'
  183. uncheckedText='〇'
  184. onChange={handleFieldChange('SelfUseModeEnabled')}
  185. />
  186. </Col>
  187. </Row>
  188. <Row>
  189. <Button size='default' onClick={onSubmit}>
  190. {t('保存通用设置')}
  191. </Button>
  192. </Row>
  193. </Form.Section>
  194. </Form>
  195. </Spin>
  196. <Modal
  197. title={t('警告')}
  198. visible={showQuotaWarning}
  199. onOk={() => setShowQuotaWarning(false)}
  200. onCancel={() => setShowQuotaWarning(false)}
  201. closeOnEsc={true}
  202. width={500}
  203. >
  204. <Banner
  205. type='warning'
  206. description={t('此设置用于系统内部计算,默认值500000是为了精确到6位小数点设计,不推荐修改。')}
  207. bordered
  208. fullMode={false}
  209. closeIcon={null}
  210. />
  211. </Modal>
  212. </>
  213. );
  214. }