SettingsSidebarModulesAdmin.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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, { useState, useEffect, useContext } from 'react';
  16. import { useTranslation } from 'react-i18next';
  17. import { Card, Form, Button, Switch, Row, Col, Typography } from '@douyinfe/semi-ui';
  18. import { API, showSuccess, showError } from '../../../helpers';
  19. import { StatusContext } from '../../../context/Status';
  20. const { Text } = Typography;
  21. export default function SettingsSidebarModulesAdmin(props) {
  22. const { t } = useTranslation();
  23. const [loading, setLoading] = useState(false);
  24. const [statusState, statusDispatch] = useContext(StatusContext);
  25. // 左侧边栏模块管理状态(管理员全局控制)
  26. const [sidebarModulesAdmin, setSidebarModulesAdmin] = useState({
  27. chat: {
  28. enabled: true,
  29. playground: true,
  30. chat: true
  31. },
  32. console: {
  33. enabled: true,
  34. detail: true,
  35. token: true,
  36. log: true,
  37. midjourney: true,
  38. task: true
  39. },
  40. personal: {
  41. enabled: true,
  42. topup: true,
  43. personal: true
  44. },
  45. admin: {
  46. enabled: true,
  47. channel: true,
  48. models: true,
  49. redemption: true,
  50. user: true,
  51. setting: true
  52. }
  53. });
  54. // 处理区域级别开关变更
  55. function handleSectionChange(sectionKey) {
  56. return (checked) => {
  57. const newModules = {
  58. ...sidebarModulesAdmin,
  59. [sectionKey]: {
  60. ...sidebarModulesAdmin[sectionKey],
  61. enabled: checked
  62. }
  63. };
  64. setSidebarModulesAdmin(newModules);
  65. };
  66. }
  67. // 处理功能级别开关变更
  68. function handleModuleChange(sectionKey, moduleKey) {
  69. return (checked) => {
  70. const newModules = {
  71. ...sidebarModulesAdmin,
  72. [sectionKey]: {
  73. ...sidebarModulesAdmin[sectionKey],
  74. [moduleKey]: checked
  75. }
  76. };
  77. setSidebarModulesAdmin(newModules);
  78. };
  79. }
  80. // 重置为默认配置
  81. function resetSidebarModules() {
  82. const defaultModules = {
  83. chat: {
  84. enabled: true,
  85. playground: true,
  86. chat: true
  87. },
  88. console: {
  89. enabled: true,
  90. detail: true,
  91. token: true,
  92. log: true,
  93. midjourney: true,
  94. task: true
  95. },
  96. personal: {
  97. enabled: true,
  98. topup: true,
  99. personal: true
  100. },
  101. admin: {
  102. enabled: true,
  103. channel: true,
  104. models: true,
  105. redemption: true,
  106. user: true,
  107. setting: true
  108. }
  109. };
  110. setSidebarModulesAdmin(defaultModules);
  111. showSuccess(t('已重置为默认配置'));
  112. }
  113. // 保存配置
  114. async function onSubmit() {
  115. setLoading(true);
  116. try {
  117. const res = await API.put('/api/option/', {
  118. key: 'SidebarModulesAdmin',
  119. value: JSON.stringify(sidebarModulesAdmin),
  120. });
  121. const { success, message } = res.data;
  122. if (success) {
  123. showSuccess(t('保存成功'));
  124. // 立即更新StatusContext中的状态
  125. statusDispatch({
  126. type: 'set',
  127. payload: {
  128. ...statusState.status,
  129. SidebarModulesAdmin: JSON.stringify(sidebarModulesAdmin)
  130. }
  131. });
  132. // 刷新父组件状态
  133. if (props.refresh) {
  134. await props.refresh();
  135. }
  136. } else {
  137. showError(message);
  138. }
  139. } catch (error) {
  140. showError(t('保存失败,请重试'));
  141. } finally {
  142. setLoading(false);
  143. }
  144. }
  145. useEffect(() => {
  146. // 从 props.options 中获取配置
  147. if (props.options && props.options.SidebarModulesAdmin) {
  148. try {
  149. const modules = JSON.parse(props.options.SidebarModulesAdmin);
  150. setSidebarModulesAdmin(modules);
  151. } catch (error) {
  152. // 使用默认配置
  153. const defaultModules = {
  154. chat: { enabled: true, playground: true, chat: true },
  155. console: { enabled: true, detail: true, token: true, log: true, midjourney: true, task: true },
  156. personal: { enabled: true, topup: true, personal: true },
  157. admin: { enabled: true, channel: true, models: true, redemption: true, user: true, setting: true }
  158. };
  159. setSidebarModulesAdmin(defaultModules);
  160. }
  161. }
  162. }, [props.options]);
  163. // 区域配置数据
  164. const sectionConfigs = [
  165. {
  166. key: 'chat',
  167. title: t('聊天区域'),
  168. description: t('操练场和聊天功能'),
  169. modules: [
  170. { key: 'playground', title: t('操练场'), description: t('AI模型测试环境') },
  171. { key: 'chat', title: t('聊天'), description: t('聊天会话管理') }
  172. ]
  173. },
  174. {
  175. key: 'console',
  176. title: t('控制台区域'),
  177. description: t('数据管理和日志查看'),
  178. modules: [
  179. { key: 'detail', title: t('数据看板'), description: t('系统数据统计') },
  180. { key: 'token', title: t('令牌管理'), description: t('API令牌管理') },
  181. { key: 'log', title: t('使用日志'), description: t('API使用记录') },
  182. { key: 'midjourney', title: t('绘图日志'), description: t('绘图任务记录') },
  183. { key: 'task', title: t('任务日志'), description: t('系统任务记录') }
  184. ]
  185. },
  186. {
  187. key: 'personal',
  188. title: t('个人中心区域'),
  189. description: t('用户个人功能'),
  190. modules: [
  191. { key: 'topup', title: t('钱包管理'), description: t('余额充值管理') },
  192. { key: 'personal', title: t('个人设置'), description: t('个人信息设置') }
  193. ]
  194. },
  195. {
  196. key: 'admin',
  197. title: t('管理员区域'),
  198. description: t('系统管理功能'),
  199. modules: [
  200. { key: 'channel', title: t('渠道管理'), description: t('API渠道配置') },
  201. { key: 'models', title: t('模型管理'), description: t('AI模型配置') },
  202. { key: 'redemption', title: t('兑换码管理'), description: t('兑换码生成管理') },
  203. { key: 'user', title: t('用户管理'), description: t('用户账户管理') },
  204. { key: 'setting', title: t('系统设置'), description: t('系统参数配置') }
  205. ]
  206. }
  207. ];
  208. return (
  209. <Card>
  210. <Form.Section text={t('侧边栏管理(全局控制)')} extraText={t('全局控制侧边栏区域和功能显示,管理员隐藏的功能用户无法启用')}>
  211. {sectionConfigs.map((section) => (
  212. <div key={section.key} style={{ marginBottom: '32px' }}>
  213. {/* 区域标题和总开关 */}
  214. <div style={{
  215. display: 'flex',
  216. justifyContent: 'space-between',
  217. alignItems: 'center',
  218. marginBottom: '16px',
  219. padding: '12px 16px',
  220. backgroundColor: 'var(--semi-color-fill-0)',
  221. borderRadius: '8px',
  222. border: '1px solid var(--semi-color-border)'
  223. }}>
  224. <div>
  225. <div style={{
  226. fontWeight: '600',
  227. fontSize: '16px',
  228. color: 'var(--semi-color-text-0)',
  229. marginBottom: '4px'
  230. }}>
  231. {section.title}
  232. </div>
  233. <Text
  234. type="secondary"
  235. size="small"
  236. style={{
  237. fontSize: '12px',
  238. color: 'var(--semi-color-text-2)',
  239. lineHeight: '1.4'
  240. }}
  241. >
  242. {section.description}
  243. </Text>
  244. </div>
  245. <Switch
  246. checked={sidebarModulesAdmin[section.key]?.enabled}
  247. onChange={handleSectionChange(section.key)}
  248. size="default"
  249. />
  250. </div>
  251. {/* 功能模块网格 */}
  252. <Row gutter={[16, 16]}>
  253. {section.modules.map((module) => (
  254. <Col key={module.key} xs={24} sm={12} md={8} lg={6} xl={6}>
  255. <Card
  256. bodyStyle={{ padding: '16px' }}
  257. hoverable
  258. style={{
  259. opacity: sidebarModulesAdmin[section.key]?.enabled ? 1 : 0.5,
  260. transition: 'opacity 0.2s'
  261. }}
  262. >
  263. <div style={{
  264. display: 'flex',
  265. justifyContent: 'space-between',
  266. alignItems: 'center',
  267. height: '100%'
  268. }}>
  269. <div style={{ flex: 1, textAlign: 'left' }}>
  270. <div style={{
  271. fontWeight: '600',
  272. fontSize: '14px',
  273. color: 'var(--semi-color-text-0)',
  274. marginBottom: '4px'
  275. }}>
  276. {module.title}
  277. </div>
  278. <Text
  279. type="secondary"
  280. size="small"
  281. style={{
  282. fontSize: '12px',
  283. color: 'var(--semi-color-text-2)',
  284. lineHeight: '1.4',
  285. display: 'block'
  286. }}
  287. >
  288. {module.description}
  289. </Text>
  290. </div>
  291. <div style={{ marginLeft: '16px' }}>
  292. <Switch
  293. checked={sidebarModulesAdmin[section.key]?.[module.key]}
  294. onChange={handleModuleChange(section.key, module.key)}
  295. size="default"
  296. disabled={!sidebarModulesAdmin[section.key]?.enabled}
  297. />
  298. </div>
  299. </div>
  300. </Card>
  301. </Col>
  302. ))}
  303. </Row>
  304. </div>
  305. ))}
  306. <div style={{
  307. display: 'flex',
  308. gap: '12px',
  309. justifyContent: 'flex-start',
  310. alignItems: 'center',
  311. paddingTop: '8px',
  312. borderTop: '1px solid var(--semi-color-border)'
  313. }}>
  314. <Button
  315. size='default'
  316. type='tertiary'
  317. onClick={resetSidebarModules}
  318. style={{
  319. borderRadius: '6px',
  320. fontWeight: '500'
  321. }}
  322. >
  323. {t('重置为默认')}
  324. </Button>
  325. <Button
  326. size='default'
  327. type='primary'
  328. onClick={onSubmit}
  329. loading={loading}
  330. style={{
  331. borderRadius: '6px',
  332. fontWeight: '500',
  333. minWidth: '100px'
  334. }}
  335. >
  336. {t('保存设置')}
  337. </Button>
  338. </div>
  339. </Form.Section>
  340. </Card>
  341. );
  342. }