SettingsGeneral.jsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. Copyright (C) 2025 QuantumNous
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>.
  13. For commercial licensing, please contact support@quantumnous.com
  14. */
  15. import React, { useEffect, useState, useRef } from 'react';
  16. import { Banner, Button, Col, Form, Row, Spin, Modal } from '@douyinfe/semi-ui';
  17. import {
  18. compareObjects,
  19. API,
  20. showError,
  21. showSuccess,
  22. showWarning,
  23. } from '../../../helpers';
  24. import { useTranslation } from 'react-i18next';
  25. export default function GeneralSettings(props) {
  26. const { t } = useTranslation();
  27. const [loading, setLoading] = useState(false);
  28. const [showQuotaWarning, setShowQuotaWarning] = useState(false);
  29. const [inputs, setInputs] = useState({
  30. TopUpLink: '',
  31. 'general_setting.docs_link': '',
  32. QuotaPerUnit: '',
  33. RetryTimes: '',
  34. USDExchangeRate: '',
  35. DisplayInCurrencyEnabled: false,
  36. DisplayTokenStatEnabled: false,
  37. DefaultCollapseSidebar: false,
  38. DemoSiteEnabled: false,
  39. SelfUseModeEnabled: false,
  40. });
  41. const refForm = useRef();
  42. const [inputsRow, setInputsRow] = useState(inputs);
  43. function handleFieldChange(fieldName) {
  44. return (value) => {
  45. setInputs((inputs) => ({ ...inputs, [fieldName]: value }));
  46. };
  47. }
  48. function onSubmit() {
  49. const updateArray = compareObjects(inputs, inputsRow);
  50. if (!updateArray.length) return showWarning(t('你似乎并没有修改什么'));
  51. const requestQueue = updateArray.map((item) => {
  52. let value = '';
  53. if (typeof inputs[item.key] === 'boolean') {
  54. value = String(inputs[item.key]);
  55. } else {
  56. value = inputs[item.key];
  57. }
  58. return API.put('/api/option/', {
  59. key: item.key,
  60. value,
  61. });
  62. });
  63. setLoading(true);
  64. Promise.all(requestQueue)
  65. .then((res) => {
  66. if (requestQueue.length === 1) {
  67. if (res.includes(undefined)) return;
  68. } else if (requestQueue.length > 1) {
  69. if (res.includes(undefined))
  70. return showError(t('部分保存失败,请重试'));
  71. }
  72. showSuccess(t('保存成功'));
  73. props.refresh();
  74. })
  75. .catch(() => {
  76. showError(t('保存失败,请重试'));
  77. })
  78. .finally(() => {
  79. setLoading(false);
  80. });
  81. }
  82. useEffect(() => {
  83. const currentInputs = {};
  84. for (let key in props.options) {
  85. if (Object.keys(inputs).includes(key)) {
  86. currentInputs[key] = props.options[key];
  87. }
  88. }
  89. setInputs(currentInputs);
  90. setInputsRow(structuredClone(currentInputs));
  91. refForm.current.setValues(currentInputs);
  92. }, [props.options]);
  93. return (
  94. <>
  95. <Spin spinning={loading}>
  96. <Form
  97. values={inputs}
  98. getFormApi={(formAPI) => (refForm.current = formAPI)}
  99. style={{ marginBottom: 15 }}
  100. >
  101. <Form.Section text={t('通用设置')}>
  102. <Row gutter={16}>
  103. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  104. <Form.Input
  105. field={'TopUpLink'}
  106. label={t('充值链接')}
  107. initValue={''}
  108. placeholder={t('例如发卡网站的购买链接')}
  109. onChange={handleFieldChange('TopUpLink')}
  110. showClear
  111. />
  112. </Col>
  113. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  114. <Form.Input
  115. field={'general_setting.docs_link'}
  116. label={t('文档地址')}
  117. initValue={''}
  118. placeholder={t('例如 https://docs.newapi.pro')}
  119. onChange={handleFieldChange('general_setting.docs_link')}
  120. showClear
  121. />
  122. </Col>
  123. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  124. <Form.Input
  125. field={'QuotaPerUnit'}
  126. label={t('单位美元额度')}
  127. initValue={''}
  128. placeholder={t('一单位货币能兑换的额度')}
  129. onChange={handleFieldChange('QuotaPerUnit')}
  130. showClear
  131. onClick={() => setShowQuotaWarning(true)}
  132. />
  133. </Col>
  134. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  135. <Form.Input
  136. field={'USDExchangeRate'}
  137. label={t('美元汇率(非充值汇率,仅用于定价页面换算)')}
  138. initValue={''}
  139. placeholder={t('美元汇率')}
  140. onChange={handleFieldChange('USDExchangeRate')}
  141. showClear
  142. />
  143. </Col>
  144. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  145. <Form.Input
  146. field={'RetryTimes'}
  147. label={t('失败重试次数')}
  148. initValue={''}
  149. placeholder={t('失败重试次数')}
  150. onChange={handleFieldChange('RetryTimes')}
  151. showClear
  152. />
  153. </Col>
  154. </Row>
  155. <Row gutter={16}>
  156. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  157. <Form.Switch
  158. field={'DisplayInCurrencyEnabled'}
  159. label={t('以货币形式显示额度')}
  160. size='default'
  161. checkedText='|'
  162. uncheckedText='〇'
  163. onChange={handleFieldChange('DisplayInCurrencyEnabled')}
  164. />
  165. </Col>
  166. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  167. <Form.Switch
  168. field={'DisplayTokenStatEnabled'}
  169. label={t('额度查询接口返回令牌额度而非用户额度')}
  170. size='default'
  171. checkedText='|'
  172. uncheckedText='〇'
  173. onChange={handleFieldChange('DisplayTokenStatEnabled')}
  174. />
  175. </Col>
  176. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  177. <Form.Switch
  178. field={'DefaultCollapseSidebar'}
  179. label={t('默认折叠侧边栏')}
  180. size='default'
  181. checkedText='|'
  182. uncheckedText='〇'
  183. onChange={handleFieldChange('DefaultCollapseSidebar')}
  184. />
  185. </Col>
  186. </Row>
  187. <Row gutter={16}>
  188. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  189. <Form.Switch
  190. field={'DemoSiteEnabled'}
  191. label={t('演示站点模式')}
  192. size='default'
  193. checkedText='|'
  194. uncheckedText='〇'
  195. onChange={handleFieldChange('DemoSiteEnabled')}
  196. />
  197. </Col>
  198. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  199. <Form.Switch
  200. field={'SelfUseModeEnabled'}
  201. label={t('自用模式')}
  202. extraText={t('开启后不限制:必须设置模型倍率')}
  203. size='default'
  204. checkedText='|'
  205. uncheckedText='〇'
  206. onChange={handleFieldChange('SelfUseModeEnabled')}
  207. />
  208. </Col>
  209. </Row>
  210. <Row>
  211. <Button size='default' onClick={onSubmit}>
  212. {t('保存通用设置')}
  213. </Button>
  214. </Row>
  215. </Form.Section>
  216. </Form>
  217. </Spin>
  218. <Modal
  219. title={t('警告')}
  220. visible={showQuotaWarning}
  221. onOk={() => setShowQuotaWarning(false)}
  222. onCancel={() => setShowQuotaWarning(false)}
  223. closeOnEsc={true}
  224. width={500}
  225. >
  226. <Banner
  227. type='warning'
  228. description={t(
  229. '此设置用于系统内部计算,默认值500000是为了精确到6位小数点设计,不推荐修改。',
  230. )}
  231. bordered
  232. fullMode={false}
  233. closeIcon={null}
  234. />
  235. </Modal>
  236. </>
  237. );
  238. }