SettingsHeaderNavModules.jsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, useContext } from 'react';
  16. import { Button, Card, Col, Form, Row, Switch, Typography } from '@douyinfe/semi-ui';
  17. import { API, showError, showSuccess } from '../../../helpers';
  18. import { useTranslation } from 'react-i18next';
  19. import { StatusContext } from '../../../context/Status';
  20. const { Text } = Typography;
  21. export default function SettingsHeaderNavModules(props) {
  22. const { t } = useTranslation();
  23. const [loading, setLoading] = useState(false);
  24. const [statusState, statusDispatch] = useContext(StatusContext);
  25. // 顶栏模块管理状态
  26. const [headerNavModules, setHeaderNavModules] = useState({
  27. home: true,
  28. console: true,
  29. pricing: {
  30. enabled: true,
  31. requireAuth: false // 默认不需要登录鉴权
  32. },
  33. docs: true,
  34. about: true,
  35. });
  36. // 处理顶栏模块配置变更
  37. function handleHeaderNavModuleChange(moduleKey) {
  38. return (checked) => {
  39. const newModules = { ...headerNavModules };
  40. if (moduleKey === 'pricing') {
  41. // 对于pricing模块,只更新enabled属性
  42. newModules[moduleKey] = {
  43. ...newModules[moduleKey],
  44. enabled: checked
  45. };
  46. } else {
  47. newModules[moduleKey] = checked;
  48. }
  49. setHeaderNavModules(newModules);
  50. };
  51. }
  52. // 处理模型广场权限控制变更
  53. function handlePricingAuthChange(checked) {
  54. const newModules = { ...headerNavModules };
  55. newModules.pricing = {
  56. ...newModules.pricing,
  57. requireAuth: checked
  58. };
  59. setHeaderNavModules(newModules);
  60. }
  61. // 重置顶栏模块为默认配置
  62. function resetHeaderNavModules() {
  63. const defaultModules = {
  64. home: true,
  65. console: true,
  66. pricing: {
  67. enabled: true,
  68. requireAuth: false
  69. },
  70. docs: true,
  71. about: true,
  72. };
  73. setHeaderNavModules(defaultModules);
  74. showSuccess(t('已重置为默认配置'));
  75. }
  76. // 保存配置
  77. async function onSubmit() {
  78. setLoading(true);
  79. try {
  80. const res = await API.put('/api/option/', {
  81. key: 'HeaderNavModules',
  82. value: JSON.stringify(headerNavModules),
  83. });
  84. const { success, message } = res.data;
  85. if (success) {
  86. showSuccess(t('保存成功'));
  87. // 立即更新StatusContext中的状态
  88. statusDispatch({
  89. type: 'set',
  90. payload: {
  91. ...statusState.status,
  92. HeaderNavModules: JSON.stringify(headerNavModules)
  93. }
  94. });
  95. // 刷新父组件状态
  96. if (props.refresh) {
  97. await props.refresh();
  98. }
  99. } else {
  100. showError(message);
  101. }
  102. } catch (error) {
  103. showError(t('保存失败,请重试'));
  104. } finally {
  105. setLoading(false);
  106. }
  107. }
  108. useEffect(() => {
  109. // 从 props.options 中获取配置
  110. if (props.options && props.options.HeaderNavModules) {
  111. try {
  112. const modules = JSON.parse(props.options.HeaderNavModules);
  113. // 处理向后兼容性:如果pricing是boolean,转换为对象格式
  114. if (typeof modules.pricing === 'boolean') {
  115. modules.pricing = {
  116. enabled: modules.pricing,
  117. requireAuth: false // 默认不需要登录鉴权
  118. };
  119. }
  120. setHeaderNavModules(modules);
  121. } catch (error) {
  122. // 使用默认配置
  123. const defaultModules = {
  124. home: true,
  125. console: true,
  126. pricing: {
  127. enabled: true,
  128. requireAuth: false
  129. },
  130. docs: true,
  131. about: true,
  132. };
  133. setHeaderNavModules(defaultModules);
  134. }
  135. }
  136. }, [props.options]);
  137. // 模块配置数据
  138. const moduleConfigs = [
  139. {
  140. key: 'home',
  141. title: t('首页'),
  142. description: t('用户主页,展示系统信息')
  143. },
  144. {
  145. key: 'console',
  146. title: t('控制台'),
  147. description: t('用户控制面板,管理账户')
  148. },
  149. {
  150. key: 'pricing',
  151. title: t('模型广场'),
  152. description: t('模型定价,需要登录访问'),
  153. hasSubConfig: true // 标识该模块有子配置
  154. },
  155. {
  156. key: 'docs',
  157. title: t('文档'),
  158. description: t('系统文档和帮助信息')
  159. },
  160. {
  161. key: 'about',
  162. title: t('关于'),
  163. description: t('关于系统的详细信息')
  164. }
  165. ];
  166. return (
  167. <Card>
  168. <Form.Section text={t('顶栏管理')} extraText={t('控制顶栏模块显示状态,全局生效')}>
  169. <Row gutter={[16, 16]} style={{ marginBottom: '24px' }}>
  170. {moduleConfigs.map((module) => (
  171. <Col key={module.key} xs={24} sm={12} md={6} lg={6} xl={6}>
  172. <Card
  173. style={{
  174. borderRadius: '8px',
  175. border: '1px solid var(--semi-color-border)',
  176. transition: 'all 0.2s ease',
  177. background: 'var(--semi-color-bg-1)',
  178. minHeight: '80px'
  179. }}
  180. bodyStyle={{ padding: '16px' }}
  181. hoverable
  182. >
  183. <div style={{
  184. display: 'flex',
  185. justifyContent: 'space-between',
  186. alignItems: 'center',
  187. height: '100%'
  188. }}>
  189. <div style={{ flex: 1, textAlign: 'left' }}>
  190. <div style={{
  191. fontWeight: '600',
  192. fontSize: '14px',
  193. color: 'var(--semi-color-text-0)',
  194. marginBottom: '4px'
  195. }}>
  196. {module.title}
  197. </div>
  198. <Text
  199. type="secondary"
  200. size="small"
  201. style={{
  202. fontSize: '12px',
  203. color: 'var(--semi-color-text-2)',
  204. lineHeight: '1.4',
  205. display: 'block'
  206. }}
  207. >
  208. {module.description}
  209. </Text>
  210. </div>
  211. <div style={{ marginLeft: '16px' }}>
  212. <Switch
  213. checked={module.key === 'pricing' ? headerNavModules[module.key]?.enabled : headerNavModules[module.key]}
  214. onChange={handleHeaderNavModuleChange(module.key)}
  215. size="default"
  216. />
  217. </div>
  218. </div>
  219. {/* 为模型广场添加权限控制子开关 */}
  220. {module.key === 'pricing' && (module.key === 'pricing' ? headerNavModules[module.key]?.enabled : headerNavModules[module.key]) && (
  221. <div style={{
  222. borderTop: '1px solid var(--semi-color-border)',
  223. marginTop: '12px',
  224. paddingTop: '12px'
  225. }}>
  226. <div style={{
  227. display: 'flex',
  228. justifyContent: 'space-between',
  229. alignItems: 'center'
  230. }}>
  231. <div style={{ flex: 1, textAlign: 'left' }}>
  232. <div style={{
  233. fontWeight: '500',
  234. fontSize: '12px',
  235. color: 'var(--semi-color-text-1)',
  236. marginBottom: '2px'
  237. }}>
  238. {t('需要登录访问')}
  239. </div>
  240. <Text
  241. type="secondary"
  242. size="small"
  243. style={{
  244. fontSize: '11px',
  245. color: 'var(--semi-color-text-2)',
  246. lineHeight: '1.4',
  247. display: 'block'
  248. }}
  249. >
  250. {t('开启后未登录用户无法访问模型广场')}
  251. </Text>
  252. </div>
  253. <div style={{ marginLeft: '16px' }}>
  254. <Switch
  255. checked={headerNavModules.pricing?.requireAuth || false}
  256. onChange={handlePricingAuthChange}
  257. size="small"
  258. />
  259. </div>
  260. </div>
  261. </div>
  262. )}
  263. </Card>
  264. </Col>
  265. ))}
  266. </Row>
  267. <div style={{
  268. display: 'flex',
  269. gap: '12px',
  270. justifyContent: 'flex-start',
  271. alignItems: 'center',
  272. paddingTop: '8px',
  273. borderTop: '1px solid var(--semi-color-border)'
  274. }}>
  275. <Button
  276. size='default'
  277. type='tertiary'
  278. onClick={resetHeaderNavModules}
  279. style={{
  280. borderRadius: '6px',
  281. fontWeight: '500'
  282. }}
  283. >
  284. {t('重置为默认')}
  285. </Button>
  286. <Button
  287. size='default'
  288. type='primary'
  289. onClick={onSubmit}
  290. loading={loading}
  291. style={{
  292. borderRadius: '6px',
  293. fontWeight: '500',
  294. minWidth: '100px'
  295. }}
  296. >
  297. {t('保存设置')}
  298. </Button>
  299. </div>
  300. </Form.Section>
  301. </Card>
  302. );
  303. }