SettingsGeneral.jsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. {inputs.QuotaPerUnit !== '500000' && inputs.QuotaPerUnit !== 500000 && (
  124. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  125. <Form.Input
  126. field={'QuotaPerUnit'}
  127. label={t('单位美元额度')}
  128. initValue={''}
  129. placeholder={t('一单位货币能兑换的额度')}
  130. onChange={handleFieldChange('QuotaPerUnit')}
  131. showClear
  132. onClick={() => setShowQuotaWarning(true)}
  133. />
  134. </Col>
  135. )}
  136. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  137. <Form.Input
  138. field={'USDExchangeRate'}
  139. label={t('美元汇率(非充值汇率,仅用于定价页面换算)')}
  140. initValue={''}
  141. placeholder={t('美元汇率')}
  142. onChange={handleFieldChange('USDExchangeRate')}
  143. showClear
  144. />
  145. </Col>
  146. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  147. <Form.Input
  148. field={'RetryTimes'}
  149. label={t('失败重试次数')}
  150. initValue={''}
  151. placeholder={t('失败重试次数')}
  152. onChange={handleFieldChange('RetryTimes')}
  153. showClear
  154. />
  155. </Col>
  156. </Row>
  157. <Row gutter={16}>
  158. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  159. <Form.Switch
  160. field={'DisplayInCurrencyEnabled'}
  161. label={t('以货币形式显示额度')}
  162. size='default'
  163. checkedText='|'
  164. uncheckedText='〇'
  165. onChange={handleFieldChange('DisplayInCurrencyEnabled')}
  166. />
  167. </Col>
  168. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  169. <Form.Switch
  170. field={'DisplayTokenStatEnabled'}
  171. label={t('额度查询接口返回令牌额度而非用户额度')}
  172. size='default'
  173. checkedText='|'
  174. uncheckedText='〇'
  175. onChange={handleFieldChange('DisplayTokenStatEnabled')}
  176. />
  177. </Col>
  178. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  179. <Form.Switch
  180. field={'DefaultCollapseSidebar'}
  181. label={t('默认折叠侧边栏')}
  182. size='default'
  183. checkedText='|'
  184. uncheckedText='〇'
  185. onChange={handleFieldChange('DefaultCollapseSidebar')}
  186. />
  187. </Col>
  188. </Row>
  189. <Row gutter={16}>
  190. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  191. <Form.Switch
  192. field={'DemoSiteEnabled'}
  193. label={t('演示站点模式')}
  194. size='default'
  195. checkedText='|'
  196. uncheckedText='〇'
  197. onChange={handleFieldChange('DemoSiteEnabled')}
  198. />
  199. </Col>
  200. <Col xs={24} sm={12} md={8} lg={8} xl={8}>
  201. <Form.Switch
  202. field={'SelfUseModeEnabled'}
  203. label={t('自用模式')}
  204. extraText={t('开启后不限制:必须设置模型倍率')}
  205. size='default'
  206. checkedText='|'
  207. uncheckedText='〇'
  208. onChange={handleFieldChange('SelfUseModeEnabled')}
  209. />
  210. </Col>
  211. </Row>
  212. <Row>
  213. <Button size='default' onClick={onSubmit}>
  214. {t('保存通用设置')}
  215. </Button>
  216. </Row>
  217. </Form.Section>
  218. </Form>
  219. </Spin>
  220. <Modal
  221. title={t('警告')}
  222. visible={showQuotaWarning}
  223. onOk={() => setShowQuotaWarning(false)}
  224. onCancel={() => setShowQuotaWarning(false)}
  225. closeOnEsc={true}
  226. width={500}
  227. >
  228. <Banner
  229. type='warning'
  230. description={t(
  231. '此设置用于系统内部计算,默认值500000是为了精确到6位小数点设计,不推荐修改。',
  232. )}
  233. bordered
  234. fullMode={false}
  235. closeIcon={null}
  236. />
  237. </Modal>
  238. </>
  239. );
  240. }