RatioSetting.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React, { useEffect, useState } from 'react';
  2. import { Card, Spin, Tabs } from '@douyinfe/semi-ui';
  3. import { useTranslation } from 'react-i18next';
  4. import GroupRatioSettings from '../../pages/Setting/Ratio/GroupRatioSettings.js';
  5. import ModelRatioSettings from '../../pages/Setting/Ratio/ModelRatioSettings.js';
  6. import ModelSettingsVisualEditor from '../../pages/Setting/Ratio/ModelSettingsVisualEditor.js';
  7. import ModelRatioNotSetEditor from '../../pages/Setting/Ratio/ModelRationNotSetEditor.js';
  8. import { API, showError } from '../../helpers';
  9. const RatioSetting = () => {
  10. const { t } = useTranslation();
  11. let [inputs, setInputs] = useState({
  12. ModelPrice: '',
  13. ModelRatio: '',
  14. CacheRatio: '',
  15. CompletionRatio: '',
  16. GroupRatio: '',
  17. GroupGroupRatio: '',
  18. AutoGroups: '',
  19. DefaultUseAutoGroup: false,
  20. UserUsableGroups: '',
  21. });
  22. const [loading, setLoading] = useState(false);
  23. const getOptions = async () => {
  24. const res = await API.get('/api/option/');
  25. const { success, message, data } = res.data;
  26. if (success) {
  27. let newInputs = {};
  28. data.forEach((item) => {
  29. if (
  30. item.key === 'ModelRatio' ||
  31. item.key === 'GroupRatio' ||
  32. item.key === 'GroupGroupRatio' ||
  33. item.key === 'AutoGroups' ||
  34. item.key === 'UserUsableGroups' ||
  35. item.key === 'CompletionRatio' ||
  36. item.key === 'ModelPrice' ||
  37. item.key === 'CacheRatio'
  38. ) {
  39. try {
  40. item.value = JSON.stringify(JSON.parse(item.value), null, 2);
  41. } catch (e) {
  42. // 如果后端返回的不是合法 JSON,直接展示
  43. }
  44. }
  45. if (['DefaultUseAutoGroup'].includes(item.key)) {
  46. newInputs[item.key] = item.value === 'true' ? true : false;
  47. } else {
  48. newInputs[item.key] = item.value;
  49. }
  50. });
  51. setInputs(newInputs);
  52. } else {
  53. showError(message);
  54. }
  55. };
  56. const onRefresh = async () => {
  57. try {
  58. setLoading(true);
  59. await getOptions();
  60. } catch (error) {
  61. showError('刷新失败');
  62. } finally {
  63. setLoading(false);
  64. }
  65. };
  66. useEffect(() => {
  67. onRefresh();
  68. // eslint-disable-next-line react-hooks/exhaustive-deps
  69. }, []);
  70. return (
  71. <Spin spinning={loading} size='large'>
  72. {/* 分组倍率设置 */}
  73. <Card style={{ marginTop: '10px' }}>
  74. <GroupRatioSettings options={inputs} refresh={onRefresh} />
  75. </Card>
  76. {/* 模型倍率设置以及可视化编辑器 */}
  77. <Card style={{ marginTop: '10px' }}>
  78. <Tabs type='line'>
  79. <Tabs.TabPane tab={t('模型倍率设置')} itemKey='model'>
  80. <ModelRatioSettings options={inputs} refresh={onRefresh} />
  81. </Tabs.TabPane>
  82. <Tabs.TabPane tab={t('可视化倍率设置')} itemKey='visual'>
  83. <ModelSettingsVisualEditor
  84. options={inputs}
  85. refresh={onRefresh}
  86. />
  87. </Tabs.TabPane>
  88. <Tabs.TabPane tab={t('未设置倍率模型')} itemKey='unset_models'>
  89. <ModelRatioNotSetEditor
  90. options={inputs}
  91. refresh={onRefresh}
  92. />
  93. </Tabs.TabPane>
  94. </Tabs>
  95. </Card>
  96. </Spin>
  97. );
  98. };
  99. export default RatioSetting;