SystemSetting.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. import React, { useEffect, useState } from 'react';
  2. import { Divider, Form, Grid, Header, Message } from 'semantic-ui-react';
  3. import { API, removeTrailingSlash, showError, verifyJSON } from '../helpers';
  4. const SystemSetting = () => {
  5. let [inputs, setInputs] = useState({
  6. PasswordLoginEnabled: '',
  7. PasswordRegisterEnabled: '',
  8. EmailVerificationEnabled: '',
  9. GitHubOAuthEnabled: '',
  10. GitHubClientId: '',
  11. GitHubClientSecret: '',
  12. Notice: '',
  13. SMTPServer: '',
  14. SMTPPort: '',
  15. SMTPAccount: '',
  16. SMTPFrom: '',
  17. SMTPToken: '',
  18. ServerAddress: '',
  19. Footer: '',
  20. WeChatAuthEnabled: '',
  21. WeChatServerAddress: '',
  22. WeChatServerToken: '',
  23. WeChatAccountQRCodeImageURL: '',
  24. TurnstileCheckEnabled: '',
  25. TurnstileSiteKey: '',
  26. TurnstileSecretKey: '',
  27. RegisterEnabled: '',
  28. QuotaForNewUser: 0,
  29. QuotaForInviter: 0,
  30. QuotaForInvitee: 0,
  31. QuotaRemindThreshold: 0,
  32. PreConsumedQuota: 0,
  33. ModelRatio: '',
  34. GroupRatio: '',
  35. TopUpLink: '',
  36. ChatLink: '',
  37. AutomaticDisableChannelEnabled: '',
  38. ChannelDisableThreshold: 0,
  39. LogConsumeEnabled: ''
  40. });
  41. const [originInputs, setOriginInputs] = useState({});
  42. let [loading, setLoading] = useState(false);
  43. const getOptions = async () => {
  44. const res = await API.get('/api/option/');
  45. const { success, message, data } = res.data;
  46. if (success) {
  47. let newInputs = {};
  48. data.forEach((item) => {
  49. newInputs[item.key] = item.value;
  50. });
  51. setInputs(newInputs);
  52. setOriginInputs(newInputs);
  53. } else {
  54. showError(message);
  55. }
  56. };
  57. useEffect(() => {
  58. getOptions().then();
  59. }, []);
  60. const updateOption = async (key, value) => {
  61. setLoading(true);
  62. switch (key) {
  63. case 'PasswordLoginEnabled':
  64. case 'PasswordRegisterEnabled':
  65. case 'EmailVerificationEnabled':
  66. case 'GitHubOAuthEnabled':
  67. case 'WeChatAuthEnabled':
  68. case 'TurnstileCheckEnabled':
  69. case 'RegisterEnabled':
  70. case 'AutomaticDisableChannelEnabled':
  71. case 'LogConsumeEnabled':
  72. value = inputs[key] === 'true' ? 'false' : 'true';
  73. break;
  74. default:
  75. break;
  76. }
  77. const res = await API.put('/api/option/', {
  78. key,
  79. value
  80. });
  81. const { success, message } = res.data;
  82. if (success) {
  83. setInputs((inputs) => ({ ...inputs, [key]: value }));
  84. } else {
  85. showError(message);
  86. }
  87. setLoading(false);
  88. };
  89. const handleInputChange = async (e, { name, value }) => {
  90. if (
  91. name === 'Notice' ||
  92. name.startsWith('SMTP') ||
  93. name === 'ServerAddress' ||
  94. name === 'GitHubClientId' ||
  95. name === 'GitHubClientSecret' ||
  96. name === 'WeChatServerAddress' ||
  97. name === 'WeChatServerToken' ||
  98. name === 'WeChatAccountQRCodeImageURL' ||
  99. name === 'TurnstileSiteKey' ||
  100. name === 'TurnstileSecretKey' ||
  101. name === 'QuotaForNewUser' ||
  102. name === 'QuotaForInviter' ||
  103. name === 'QuotaForInvitee' ||
  104. name === 'QuotaRemindThreshold' ||
  105. name === 'PreConsumedQuota' ||
  106. name === 'ModelRatio' ||
  107. name === 'GroupRatio' ||
  108. name === 'TopUpLink' ||
  109. name === 'ChatLink'
  110. ) {
  111. setInputs((inputs) => ({ ...inputs, [name]: value }));
  112. } else {
  113. await updateOption(name, value);
  114. }
  115. };
  116. const submitServerAddress = async () => {
  117. let ServerAddress = removeTrailingSlash(inputs.ServerAddress);
  118. await updateOption('ServerAddress', ServerAddress);
  119. };
  120. const submitOperationConfig = async () => {
  121. if (originInputs['QuotaForNewUser'] !== inputs.QuotaForNewUser) {
  122. await updateOption('QuotaForNewUser', inputs.QuotaForNewUser);
  123. }
  124. if (originInputs['QuotaForInvitee'] !== inputs.QuotaForInvitee) {
  125. await updateOption('QuotaForInvitee', inputs.QuotaForInvitee);
  126. }
  127. if (originInputs['QuotaForInviter'] !== inputs.QuotaForInviter) {
  128. await updateOption('QuotaForInviter', inputs.QuotaForInviter);
  129. }
  130. if (originInputs['QuotaRemindThreshold'] !== inputs.QuotaRemindThreshold) {
  131. await updateOption('QuotaRemindThreshold', inputs.QuotaRemindThreshold);
  132. }
  133. if (originInputs['PreConsumedQuota'] !== inputs.PreConsumedQuota) {
  134. await updateOption('PreConsumedQuota', inputs.PreConsumedQuota);
  135. }
  136. if (originInputs['ModelRatio'] !== inputs.ModelRatio) {
  137. if (!verifyJSON(inputs.ModelRatio)) {
  138. showError('模型倍率不是合法的 JSON 字符串');
  139. return;
  140. }
  141. await updateOption('ModelRatio', inputs.ModelRatio);
  142. }
  143. if (originInputs['GroupRatio'] !== inputs.GroupRatio) {
  144. if (!verifyJSON(inputs.GroupRatio)) {
  145. showError('分组倍率不是合法的 JSON 字符串');
  146. return;
  147. }
  148. await updateOption('GroupRatio', inputs.GroupRatio);
  149. }
  150. if (originInputs['TopUpLink'] !== inputs.TopUpLink) {
  151. await updateOption('TopUpLink', inputs.TopUpLink);
  152. }
  153. if (originInputs['ChatLink'] !== inputs.ChatLink) {
  154. await updateOption('ChatLink', inputs.ChatLink);
  155. }
  156. };
  157. const submitSMTP = async () => {
  158. if (originInputs['SMTPServer'] !== inputs.SMTPServer) {
  159. await updateOption('SMTPServer', inputs.SMTPServer);
  160. }
  161. if (originInputs['SMTPAccount'] !== inputs.SMTPAccount) {
  162. await updateOption('SMTPAccount', inputs.SMTPAccount);
  163. }
  164. if (originInputs['SMTPFrom'] !== inputs.SMTPFrom) {
  165. await updateOption('SMTPFrom', inputs.SMTPFrom);
  166. }
  167. if (
  168. originInputs['SMTPPort'] !== inputs.SMTPPort &&
  169. inputs.SMTPPort !== ''
  170. ) {
  171. await updateOption('SMTPPort', inputs.SMTPPort);
  172. }
  173. if (
  174. originInputs['SMTPToken'] !== inputs.SMTPToken &&
  175. inputs.SMTPToken !== ''
  176. ) {
  177. await updateOption('SMTPToken', inputs.SMTPToken);
  178. }
  179. };
  180. const submitWeChat = async () => {
  181. if (originInputs['WeChatServerAddress'] !== inputs.WeChatServerAddress) {
  182. await updateOption(
  183. 'WeChatServerAddress',
  184. removeTrailingSlash(inputs.WeChatServerAddress)
  185. );
  186. }
  187. if (
  188. originInputs['WeChatAccountQRCodeImageURL'] !==
  189. inputs.WeChatAccountQRCodeImageURL
  190. ) {
  191. await updateOption(
  192. 'WeChatAccountQRCodeImageURL',
  193. inputs.WeChatAccountQRCodeImageURL
  194. );
  195. }
  196. if (
  197. originInputs['WeChatServerToken'] !== inputs.WeChatServerToken &&
  198. inputs.WeChatServerToken !== ''
  199. ) {
  200. await updateOption('WeChatServerToken', inputs.WeChatServerToken);
  201. }
  202. };
  203. const submitGitHubOAuth = async () => {
  204. if (originInputs['GitHubClientId'] !== inputs.GitHubClientId) {
  205. await updateOption('GitHubClientId', inputs.GitHubClientId);
  206. }
  207. if (
  208. originInputs['GitHubClientSecret'] !== inputs.GitHubClientSecret &&
  209. inputs.GitHubClientSecret !== ''
  210. ) {
  211. await updateOption('GitHubClientSecret', inputs.GitHubClientSecret);
  212. }
  213. };
  214. const submitTurnstile = async () => {
  215. if (originInputs['TurnstileSiteKey'] !== inputs.TurnstileSiteKey) {
  216. await updateOption('TurnstileSiteKey', inputs.TurnstileSiteKey);
  217. }
  218. if (
  219. originInputs['TurnstileSecretKey'] !== inputs.TurnstileSecretKey &&
  220. inputs.TurnstileSecretKey !== ''
  221. ) {
  222. await updateOption('TurnstileSecretKey', inputs.TurnstileSecretKey);
  223. }
  224. };
  225. return (
  226. <Grid columns={1}>
  227. <Grid.Column>
  228. <Form loading={loading}>
  229. <Header as='h3'>通用设置</Header>
  230. <Form.Group widths='equal'>
  231. <Form.Input
  232. label='服务器地址'
  233. placeholder='例如:https://yourdomain.com'
  234. value={inputs.ServerAddress}
  235. name='ServerAddress'
  236. onChange={handleInputChange}
  237. />
  238. </Form.Group>
  239. <Form.Button onClick={submitServerAddress}>
  240. 更新服务器地址
  241. </Form.Button>
  242. <Divider />
  243. <Header as='h3'>配置登录注册</Header>
  244. <Form.Group inline>
  245. <Form.Checkbox
  246. checked={inputs.PasswordLoginEnabled === 'true'}
  247. label='允许通过密码进行登录'
  248. name='PasswordLoginEnabled'
  249. onChange={handleInputChange}
  250. />
  251. <Form.Checkbox
  252. checked={inputs.PasswordRegisterEnabled === 'true'}
  253. label='允许通过密码进行注册'
  254. name='PasswordRegisterEnabled'
  255. onChange={handleInputChange}
  256. />
  257. <Form.Checkbox
  258. checked={inputs.EmailVerificationEnabled === 'true'}
  259. label='通过密码注册时需要进行邮箱验证'
  260. name='EmailVerificationEnabled'
  261. onChange={handleInputChange}
  262. />
  263. <Form.Checkbox
  264. checked={inputs.GitHubOAuthEnabled === 'true'}
  265. label='允许通过 GitHub 账户登录 & 注册'
  266. name='GitHubOAuthEnabled'
  267. onChange={handleInputChange}
  268. />
  269. <Form.Checkbox
  270. checked={inputs.WeChatAuthEnabled === 'true'}
  271. label='允许通过微信登录 & 注册'
  272. name='WeChatAuthEnabled'
  273. onChange={handleInputChange}
  274. />
  275. </Form.Group>
  276. <Form.Group inline>
  277. <Form.Checkbox
  278. checked={inputs.RegisterEnabled === 'true'}
  279. label='允许新用户注册(此项为否时,新用户将无法以任何方式进行注册)'
  280. name='RegisterEnabled'
  281. onChange={handleInputChange}
  282. />
  283. <Form.Checkbox
  284. checked={inputs.TurnstileCheckEnabled === 'true'}
  285. label='启用 Turnstile 用户校验'
  286. name='TurnstileCheckEnabled'
  287. onChange={handleInputChange}
  288. />
  289. </Form.Group>
  290. <Divider />
  291. <Header as='h3'>
  292. 运营设置
  293. </Header>
  294. <Form.Group widths={4}>
  295. <Form.Input
  296. label='新用户初始配额'
  297. name='QuotaForNewUser'
  298. onChange={handleInputChange}
  299. autoComplete='new-password'
  300. value={inputs.QuotaForNewUser}
  301. type='number'
  302. min='0'
  303. placeholder='例如:100'
  304. />
  305. <Form.Input
  306. label='充值链接'
  307. name='TopUpLink'
  308. onChange={handleInputChange}
  309. autoComplete='new-password'
  310. value={inputs.TopUpLink}
  311. type='link'
  312. placeholder='例如发卡网站的购买链接'
  313. />
  314. <Form.Input
  315. label='额度提醒阈值'
  316. name='QuotaRemindThreshold'
  317. onChange={handleInputChange}
  318. autoComplete='new-password'
  319. value={inputs.QuotaRemindThreshold}
  320. type='number'
  321. min='0'
  322. placeholder='低于此额度时将发送邮件提醒用户'
  323. />
  324. <Form.Input
  325. label='请求预扣费额度'
  326. name='PreConsumedQuota'
  327. onChange={handleInputChange}
  328. autoComplete='new-password'
  329. value={inputs.PreConsumedQuota}
  330. type='number'
  331. min='0'
  332. placeholder='请求结束后多退少补'
  333. />
  334. </Form.Group>
  335. <Form.Group widths={4}>
  336. <Form.Input
  337. label='邀请新用户奖励配额'
  338. name='QuotaForInviter'
  339. onChange={handleInputChange}
  340. autoComplete='new-password'
  341. value={inputs.QuotaForInviter}
  342. type='number'
  343. min='0'
  344. placeholder='例如:100'
  345. />
  346. <Form.Input
  347. label='新用户使用邀请码奖励配额'
  348. name='QuotaForInvitee'
  349. onChange={handleInputChange}
  350. autoComplete='new-password'
  351. value={inputs.QuotaForInvitee}
  352. type='number'
  353. min='0'
  354. placeholder='例如:100'
  355. />
  356. <Form.Input
  357. label='聊天页面链接'
  358. name='ChatLink'
  359. onChange={handleInputChange}
  360. autoComplete='new-password'
  361. value={inputs.ChatLink}
  362. type='link'
  363. placeholder='例如 ChatGPT Next Web 的部署地址'
  364. />
  365. </Form.Group>
  366. <Form.Group widths='equal'>
  367. <Form.TextArea
  368. label='模型倍率'
  369. name='ModelRatio'
  370. onChange={handleInputChange}
  371. style={{ minHeight: 250, fontFamily: 'JetBrains Mono, Consolas' }}
  372. autoComplete='new-password'
  373. value={inputs.ModelRatio}
  374. placeholder='为一个 JSON 文本,键为模型名称,值为倍率'
  375. />
  376. </Form.Group>
  377. <Form.Group widths='equal'>
  378. <Form.TextArea
  379. label='分组倍率'
  380. name='GroupRatio'
  381. onChange={handleInputChange}
  382. style={{ minHeight: 250, fontFamily: 'JetBrains Mono, Consolas' }}
  383. autoComplete='new-password'
  384. value={inputs.GroupRatio}
  385. placeholder='为一个 JSON 文本,键为分组名称,值为倍率'
  386. />
  387. </Form.Group>
  388. <Form.Checkbox
  389. checked={inputs.LogConsumeEnabled === 'true'}
  390. label='启用额度消费日志记录'
  391. name='LogConsumeEnabled'
  392. onChange={handleInputChange}
  393. />
  394. <Form.Button onClick={submitOperationConfig}>保存运营设置</Form.Button>
  395. <Divider />
  396. <Header as='h3'>
  397. 监控设置
  398. </Header>
  399. <Form.Group widths={3}>
  400. <Form.Input
  401. label='最长响应时间'
  402. name='ChannelDisableThreshold'
  403. onChange={handleInputChange}
  404. autoComplete='new-password'
  405. value={inputs.ChannelDisableThreshold}
  406. type='number'
  407. min='0'
  408. placeholder='单位秒,当运行通道全部测试时,超过此时间将自动禁用通道'
  409. />
  410. </Form.Group>
  411. <Form.Group inline>
  412. <Form.Checkbox
  413. checked={inputs.AutomaticDisableChannelEnabled === 'true'}
  414. label='失败时自动禁用通道'
  415. name='AutomaticDisableChannelEnabled'
  416. onChange={handleInputChange}
  417. />
  418. </Form.Group>
  419. <Divider />
  420. <Header as='h3'>
  421. 配置 SMTP
  422. <Header.Subheader>用以支持系统的邮件发送</Header.Subheader>
  423. </Header>
  424. <Form.Group widths={3}>
  425. <Form.Input
  426. label='SMTP 服务器地址'
  427. name='SMTPServer'
  428. onChange={handleInputChange}
  429. autoComplete='new-password'
  430. value={inputs.SMTPServer}
  431. placeholder='例如:smtp.qq.com'
  432. />
  433. <Form.Input
  434. label='SMTP 端口'
  435. name='SMTPPort'
  436. onChange={handleInputChange}
  437. autoComplete='new-password'
  438. value={inputs.SMTPPort}
  439. placeholder='默认: 587'
  440. />
  441. <Form.Input
  442. label='SMTP 账户'
  443. name='SMTPAccount'
  444. onChange={handleInputChange}
  445. autoComplete='new-password'
  446. value={inputs.SMTPAccount}
  447. placeholder='通常是邮箱地址'
  448. />
  449. </Form.Group>
  450. <Form.Group widths={3}>
  451. <Form.Input
  452. label='SMTP 发送者邮箱'
  453. name='SMTPFrom'
  454. onChange={handleInputChange}
  455. autoComplete='new-password'
  456. value={inputs.SMTPFrom}
  457. placeholder='通常和邮箱地址保持一致'
  458. />
  459. <Form.Input
  460. label='SMTP 访问凭证'
  461. name='SMTPToken'
  462. onChange={handleInputChange}
  463. type='password'
  464. autoComplete='new-password'
  465. value={inputs.SMTPToken}
  466. placeholder='敏感信息不会发送到前端显示'
  467. />
  468. </Form.Group>
  469. <Form.Button onClick={submitSMTP}>保存 SMTP 设置</Form.Button>
  470. <Divider />
  471. <Header as='h3'>
  472. 配置 GitHub OAuth App
  473. <Header.Subheader>
  474. 用以支持通过 GitHub 进行登录注册,
  475. <a href='https://github.com/settings/developers' target='_blank'>
  476. 点击此处
  477. </a>
  478. 管理你的 GitHub OAuth App
  479. </Header.Subheader>
  480. </Header>
  481. <Message>
  482. Homepage URL 填 <code>{inputs.ServerAddress}</code>
  483. ,Authorization callback URL 填{' '}
  484. <code>{`${inputs.ServerAddress}/oauth/github`}</code>
  485. </Message>
  486. <Form.Group widths={3}>
  487. <Form.Input
  488. label='GitHub Client ID'
  489. name='GitHubClientId'
  490. onChange={handleInputChange}
  491. autoComplete='new-password'
  492. value={inputs.GitHubClientId}
  493. placeholder='输入你注册的 GitHub OAuth APP 的 ID'
  494. />
  495. <Form.Input
  496. label='GitHub Client Secret'
  497. name='GitHubClientSecret'
  498. onChange={handleInputChange}
  499. type='password'
  500. autoComplete='new-password'
  501. value={inputs.GitHubClientSecret}
  502. placeholder='敏感信息不会发送到前端显示'
  503. />
  504. </Form.Group>
  505. <Form.Button onClick={submitGitHubOAuth}>
  506. 保存 GitHub OAuth 设置
  507. </Form.Button>
  508. <Divider />
  509. <Header as='h3'>
  510. 配置 WeChat Server
  511. <Header.Subheader>
  512. 用以支持通过微信进行登录注册,
  513. <a
  514. href='https://github.com/songquanpeng/wechat-server'
  515. target='_blank'
  516. >
  517. 点击此处
  518. </a>
  519. 了解 WeChat Server
  520. </Header.Subheader>
  521. </Header>
  522. <Form.Group widths={3}>
  523. <Form.Input
  524. label='WeChat Server 服务器地址'
  525. name='WeChatServerAddress'
  526. placeholder='例如:https://yourdomain.com'
  527. onChange={handleInputChange}
  528. autoComplete='new-password'
  529. value={inputs.WeChatServerAddress}
  530. />
  531. <Form.Input
  532. label='WeChat Server 访问凭证'
  533. name='WeChatServerToken'
  534. type='password'
  535. onChange={handleInputChange}
  536. autoComplete='new-password'
  537. value={inputs.WeChatServerToken}
  538. placeholder='敏感信息不会发送到前端显示'
  539. />
  540. <Form.Input
  541. label='微信公众号二维码图片链接'
  542. name='WeChatAccountQRCodeImageURL'
  543. onChange={handleInputChange}
  544. autoComplete='new-password'
  545. value={inputs.WeChatAccountQRCodeImageURL}
  546. placeholder='输入一个图片链接'
  547. />
  548. </Form.Group>
  549. <Form.Button onClick={submitWeChat}>
  550. 保存 WeChat Server 设置
  551. </Form.Button>
  552. <Divider />
  553. <Header as='h3'>
  554. 配置 Turnstile
  555. <Header.Subheader>
  556. 用以支持用户校验,
  557. <a href='https://dash.cloudflare.com/' target='_blank'>
  558. 点击此处
  559. </a>
  560. 管理你的 Turnstile Sites,推荐选择 Invisible Widget Type
  561. </Header.Subheader>
  562. </Header>
  563. <Form.Group widths={3}>
  564. <Form.Input
  565. label='Turnstile Site Key'
  566. name='TurnstileSiteKey'
  567. onChange={handleInputChange}
  568. autoComplete='new-password'
  569. value={inputs.TurnstileSiteKey}
  570. placeholder='输入你注册的 Turnstile Site Key'
  571. />
  572. <Form.Input
  573. label='Turnstile Secret Key'
  574. name='TurnstileSecretKey'
  575. onChange={handleInputChange}
  576. type='password'
  577. autoComplete='new-password'
  578. value={inputs.TurnstileSecretKey}
  579. placeholder='敏感信息不会发送到前端显示'
  580. />
  581. </Form.Group>
  582. <Form.Button onClick={submitTurnstile}>
  583. 保存 Turnstile 设置
  584. </Form.Button>
  585. </Form>
  586. </Grid.Column>
  587. </Grid>
  588. );
  589. };
  590. export default SystemSetting;