GroupRatioSettings.jsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 { Button, Col, Form, Row, Spin } from '@douyinfe/semi-ui';
  17. import {
  18. compareObjects,
  19. API,
  20. showError,
  21. showSuccess,
  22. showWarning,
  23. verifyJSON,
  24. } from '../../../helpers';
  25. import { useTranslation } from 'react-i18next';
  26. export default function GroupRatioSettings(props) {
  27. const { t } = useTranslation();
  28. const [loading, setLoading] = useState(false);
  29. const [inputs, setInputs] = useState({
  30. GroupRatio: '',
  31. UserUsableGroups: '',
  32. GroupGroupRatio: '',
  33. 'group_ratio_setting.group_special_usable_group': '',
  34. AutoGroups: '',
  35. DefaultUseAutoGroup: false,
  36. });
  37. const refForm = useRef();
  38. const [inputsRow, setInputsRow] = useState(inputs);
  39. async function onSubmit() {
  40. try {
  41. await refForm.current
  42. .validate()
  43. .then(() => {
  44. const updateArray = compareObjects(inputs, inputsRow);
  45. if (!updateArray.length)
  46. return showWarning(t('你似乎并没有修改什么'));
  47. const requestQueue = updateArray.map((item) => {
  48. const value =
  49. typeof inputs[item.key] === 'boolean'
  50. ? String(inputs[item.key])
  51. : inputs[item.key];
  52. return API.put('/api/option/', { key: item.key, value });
  53. });
  54. setLoading(true);
  55. Promise.all(requestQueue)
  56. .then((res) => {
  57. if (res.includes(undefined)) {
  58. return showError(
  59. requestQueue.length > 1
  60. ? t('部分保存失败,请重试')
  61. : t('保存失败'),
  62. );
  63. }
  64. for (let i = 0; i < res.length; i++) {
  65. if (!res[i].data.success) {
  66. return showError(res[i].data.message);
  67. }
  68. }
  69. showSuccess(t('保存成功'));
  70. props.refresh();
  71. })
  72. .catch((error) => {
  73. console.error('Unexpected error:', error);
  74. showError(t('保存失败,请重试'));
  75. })
  76. .finally(() => {
  77. setLoading(false);
  78. });
  79. })
  80. .catch(() => {
  81. showError(t('请检查输入'));
  82. });
  83. } catch (error) {
  84. showError(t('请检查输入'));
  85. console.error(error);
  86. }
  87. }
  88. useEffect(() => {
  89. const currentInputs = {};
  90. for (let key in props.options) {
  91. if (Object.keys(inputs).includes(key)) {
  92. currentInputs[key] = props.options[key];
  93. }
  94. }
  95. setInputs(currentInputs);
  96. setInputsRow(structuredClone(currentInputs));
  97. refForm.current.setValues(currentInputs);
  98. }, [props.options]);
  99. return (
  100. <Spin spinning={loading}>
  101. <Form
  102. values={inputs}
  103. getFormApi={(formAPI) => (refForm.current = formAPI)}
  104. style={{ marginBottom: 15 }}
  105. >
  106. <Row gutter={16}>
  107. <Col xs={24} sm={16}>
  108. <Form.TextArea
  109. label={t('分组倍率')}
  110. placeholder={t('为一个 JSON 文本,键为分组名称,值为倍率')}
  111. extraText={t(
  112. '分组倍率设置,可以在此处新增分组或修改现有分组的倍率,格式为 JSON 字符串,例如:{"vip": 0.5, "test": 1},表示 vip 分组的倍率为 0.5,test 分组的倍率为 1',
  113. )}
  114. field={'GroupRatio'}
  115. autosize={{ minRows: 6, maxRows: 12 }}
  116. trigger='blur'
  117. stopValidateWithError
  118. rules={[
  119. {
  120. validator: (rule, value) => verifyJSON(value),
  121. message: t('不是合法的 JSON 字符串'),
  122. },
  123. ]}
  124. onChange={(value) => setInputs({ ...inputs, GroupRatio: value })}
  125. />
  126. </Col>
  127. </Row>
  128. <Row gutter={16}>
  129. <Col xs={24} sm={16}>
  130. <Form.TextArea
  131. label={t('用户可选分组')}
  132. placeholder={t('为一个 JSON 文本,键为分组名称,值为分组描述')}
  133. extraText={t(
  134. '用户新建令牌时可选的分组,格式为 JSON 字符串,例如:{"vip": "VIP 用户", "test": "测试"},表示用户可以选择 vip 分组和 test 分组',
  135. )}
  136. field={'UserUsableGroups'}
  137. autosize={{ minRows: 6, maxRows: 12 }}
  138. trigger='blur'
  139. stopValidateWithError
  140. rules={[
  141. {
  142. validator: (rule, value) => verifyJSON(value),
  143. message: t('不是合法的 JSON 字符串'),
  144. },
  145. ]}
  146. onChange={(value) =>
  147. setInputs({ ...inputs, UserUsableGroups: value })
  148. }
  149. />
  150. </Col>
  151. </Row>
  152. <Row gutter={16}>
  153. <Col xs={24} sm={16}>
  154. <Form.TextArea
  155. label={t('分组特殊倍率')}
  156. placeholder={t('为一个 JSON 文本')}
  157. extraText={t(
  158. '键为分组名称,值为另一个 JSON 对象,键为分组名称,值为该分组的用户的特殊分组倍率,例如:{"vip": {"default": 0.5, "test": 1}},表示 vip 分组的用户在使用default分组的令牌时倍率为0.5,使用test分组时倍率为1',
  159. )}
  160. field={'GroupGroupRatio'}
  161. autosize={{ minRows: 6, maxRows: 12 }}
  162. trigger='blur'
  163. stopValidateWithError
  164. rules={[
  165. {
  166. validator: (rule, value) => verifyJSON(value),
  167. message: t('不是合法的 JSON 字符串'),
  168. },
  169. ]}
  170. onChange={(value) =>
  171. setInputs({ ...inputs, GroupGroupRatio: value })
  172. }
  173. />
  174. </Col>
  175. </Row>
  176. <Row gutter={16}>
  177. <Col xs={24} sm={16}>
  178. <Form.TextArea
  179. label={t('分组特殊可用分组')}
  180. placeholder={t('为一个 JSON 文本')}
  181. extraText={t(
  182. '键为用户分组名称,值为操作映射对象。内层键以"+:"开头表示添加指定分组(键值为分组名称,值为描述),以"-:"开头表示移除指定分组(键值为分组名称),不带前缀的键直接添加该分组。例如:{"vip": {"+:premium": "高级分组", "special": "特殊分组", "-:default": "默认分组"}},表示 vip 分组的用户可以使用 premium 和 special 分组,同时移除 default 分组的访问权限',
  183. )}
  184. field={'group_ratio_setting.group_special_usable_group'}
  185. autosize={{ minRows: 6, maxRows: 12 }}
  186. trigger='blur'
  187. stopValidateWithError
  188. rules={[
  189. {
  190. validator: (rule, value) => verifyJSON(value),
  191. message: t('不是合法的 JSON 字符串'),
  192. },
  193. ]}
  194. onChange={(value) =>
  195. setInputs({
  196. ...inputs,
  197. 'group_ratio_setting.group_special_usable_group': value,
  198. })
  199. }
  200. />
  201. </Col>
  202. </Row>
  203. <Row gutter={16}>
  204. <Col xs={24} sm={16}>
  205. <Form.TextArea
  206. label={t('自动分组auto,从第一个开始选择')}
  207. placeholder={t('为一个 JSON 文本')}
  208. field={'AutoGroups'}
  209. autosize={{ minRows: 6, maxRows: 12 }}
  210. trigger='blur'
  211. stopValidateWithError
  212. rules={[
  213. {
  214. validator: (rule, value) => {
  215. if (!value || value.trim() === '') {
  216. return true; // Allow empty values
  217. }
  218. // First check if it's valid JSON
  219. try {
  220. const parsed = JSON.parse(value);
  221. // Check if it's an array
  222. if (!Array.isArray(parsed)) {
  223. return false;
  224. }
  225. // Check if every element is a string
  226. return parsed.every((item) => typeof item === 'string');
  227. } catch (error) {
  228. return false;
  229. }
  230. },
  231. message: t('必须是有效的 JSON 字符串数组,例如:["g1","g2"]'),
  232. },
  233. ]}
  234. onChange={(value) => setInputs({ ...inputs, AutoGroups: value })}
  235. />
  236. </Col>
  237. </Row>
  238. <Row gutter={16}>
  239. <Col span={16}>
  240. <Form.Switch
  241. label={t(
  242. '创建令牌默认选择auto分组,初始令牌也将设为auto(否则留空,为用户默认分组)',
  243. )}
  244. field={'DefaultUseAutoGroup'}
  245. onChange={(value) =>
  246. setInputs({ ...inputs, DefaultUseAutoGroup: value })
  247. }
  248. />
  249. </Col>
  250. </Row>
  251. </Form>
  252. <Button onClick={onSubmit}>{t('保存分组倍率设置')}</Button>
  253. </Spin>
  254. );
  255. }