SettingsGeneral.jsx 8.6 KB

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