You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

405 lines
12 KiB

  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, { lazy, Suspense, useContext, useMemo } from 'react';
  16. import { Route, Routes, useLocation, useParams } from 'react-router-dom';
  17. import Loading from './components/common/ui/Loading';
  18. import User from './pages/User';
  19. import { AuthRedirect, PrivateRoute, AdminRoute } from './helpers';
  20. import RegisterForm from './components/auth/RegisterForm';
  21. import LoginForm from './components/auth/LoginForm';
  22. import NotFound from './pages/NotFound';
  23. import Forbidden from './pages/Forbidden';
  24. import Setting from './pages/Setting';
  25. import { StatusContext } from './context/Status';
  26. import PasswordResetForm from './components/auth/PasswordResetForm';
  27. import PasswordResetConfirm from './components/auth/PasswordResetConfirm';
  28. import Channel from './pages/Channel';
  29. import Token from './pages/Token';
  30. import Redemption from './pages/Redemption';
  31. import TopUp from './pages/TopUp';
  32. import Log from './pages/Log';
  33. import Chat from './pages/Chat';
  34. import Chat2Link from './pages/Chat2Link';
  35. import Midjourney from './pages/Midjourney';
  36. import Pricing from './pages/Pricing';
  37. import Task from './pages/Task';
  38. import ModelPage from './pages/Model';
  39. import ModelDeploymentPage from './pages/ModelDeployment';
  40. import Playground from './pages/Playground';
  41. import Subscription from './pages/Subscription';
  42. import OAuth2Callback from './components/auth/OAuth2Callback';
  43. import PersonalSetting from './components/settings/PersonalSetting';
  44. import Setup from './pages/Setup';
  45. import SetupCheck from './components/layout/SetupCheck';
  46. const Home = lazy(() => import('./pages/Home'));
  47. const Dashboard = lazy(() => import('./pages/Dashboard'));
  48. const About = lazy(() => import('./pages/About'));
  49. const UserAgreement = lazy(() => import('./pages/UserAgreement'));
  50. const PrivacyPolicy = lazy(() => import('./pages/PrivacyPolicy'));
  51. const Terms = lazy(() => import('./pages/Terms'));
  52. const UsagePolicy = lazy(() => import('./pages/UsagePolicy'));
  53. function DynamicOAuth2Callback() {
  54. const { provider } = useParams();
  55. return <OAuth2Callback type={provider} />;
  56. }
  57. function App() {
  58. const location = useLocation();
  59. const [statusState] = useContext(StatusContext);
  60. // 获取模型广场权限配置
  61. const pricingRequireAuth = useMemo(() => {
  62. const headerNavModulesConfig = statusState?.status?.HeaderNavModules;
  63. if (headerNavModulesConfig) {
  64. try {
  65. const modules = JSON.parse(headerNavModulesConfig);
  66. // 处理向后兼容性:如果pricing是boolean,默认不需要登录
  67. if (typeof modules.pricing === 'boolean') {
  68. return false; // 默认不需要登录鉴权
  69. }
  70. // 如果是对象格式,使用requireAuth配置
  71. return modules.pricing?.requireAuth === true;
  72. } catch (error) {
  73. console.error('解析顶栏模块配置失败:', error);
  74. return false; // 默认不需要登录
  75. }
  76. }
  77. return false; // 默认不需要登录
  78. }, [statusState?.status?.HeaderNavModules]);
  79. return (
  80. <SetupCheck>
  81. <Routes>
  82. <Route
  83. path='/'
  84. element={
  85. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  86. <Home />
  87. </Suspense>
  88. }
  89. />
  90. <Route
  91. path='/setup'
  92. element={
  93. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  94. <Setup />
  95. </Suspense>
  96. }
  97. />
  98. <Route path='/forbidden' element={<Forbidden />} />
  99. <Route
  100. path='/console/models'
  101. element={
  102. <AdminRoute>
  103. <ModelPage />
  104. </AdminRoute>
  105. }
  106. />
  107. <Route
  108. path='/console/deployment'
  109. element={
  110. <AdminRoute>
  111. <ModelDeploymentPage />
  112. </AdminRoute>
  113. }
  114. />
  115. <Route
  116. path='/console/subscription'
  117. element={
  118. <AdminRoute>
  119. <Subscription />
  120. </AdminRoute>
  121. }
  122. />
  123. <Route
  124. path='/console/channel'
  125. element={
  126. <AdminRoute>
  127. <Channel />
  128. </AdminRoute>
  129. }
  130. />
  131. <Route
  132. path='/console/token'
  133. element={
  134. <PrivateRoute>
  135. <Token />
  136. </PrivateRoute>
  137. }
  138. />
  139. <Route
  140. path='/console/playground'
  141. element={
  142. <PrivateRoute>
  143. <Playground />
  144. </PrivateRoute>
  145. }
  146. />
  147. <Route
  148. path='/console/redemption'
  149. element={
  150. <AdminRoute>
  151. <Redemption />
  152. </AdminRoute>
  153. }
  154. />
  155. <Route
  156. path='/console/user'
  157. element={
  158. <AdminRoute>
  159. <User />
  160. </AdminRoute>
  161. }
  162. />
  163. <Route
  164. path='/user/reset'
  165. element={
  166. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  167. <PasswordResetConfirm />
  168. </Suspense>
  169. }
  170. />
  171. <Route
  172. path='/login'
  173. element={
  174. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  175. <AuthRedirect>
  176. <LoginForm />
  177. </AuthRedirect>
  178. </Suspense>
  179. }
  180. />
  181. <Route
  182. path='/register'
  183. element={
  184. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  185. <AuthRedirect>
  186. <RegisterForm />
  187. </AuthRedirect>
  188. </Suspense>
  189. }
  190. />
  191. <Route
  192. path='/reset'
  193. element={
  194. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  195. <PasswordResetForm />
  196. </Suspense>
  197. }
  198. />
  199. <Route
  200. path='/oauth/github'
  201. element={
  202. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  203. <OAuth2Callback type='github'></OAuth2Callback>
  204. </Suspense>
  205. }
  206. />
  207. <Route
  208. path='/oauth/discord'
  209. element={
  210. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  211. <OAuth2Callback type='discord'></OAuth2Callback>
  212. </Suspense>
  213. }
  214. />
  215. <Route
  216. path='/oauth/oidc'
  217. element={
  218. <Suspense fallback={<Loading></Loading>}>
  219. <OAuth2Callback type='oidc'></OAuth2Callback>
  220. </Suspense>
  221. }
  222. />
  223. <Route
  224. path='/oauth/linuxdo'
  225. element={
  226. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  227. <OAuth2Callback type='linuxdo'></OAuth2Callback>
  228. </Suspense>
  229. }
  230. />
  231. <Route
  232. path='/oauth/:provider'
  233. element={
  234. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  235. <DynamicOAuth2Callback />
  236. </Suspense>
  237. }
  238. />
  239. <Route
  240. path='/console/setting'
  241. element={
  242. <AdminRoute>
  243. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  244. <Setting />
  245. </Suspense>
  246. </AdminRoute>
  247. }
  248. />
  249. <Route
  250. path='/console/personal'
  251. element={
  252. <PrivateRoute>
  253. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  254. <PersonalSetting />
  255. </Suspense>
  256. </PrivateRoute>
  257. }
  258. />
  259. <Route
  260. path='/console/topup'
  261. element={
  262. <PrivateRoute>
  263. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  264. <TopUp />
  265. </Suspense>
  266. </PrivateRoute>
  267. }
  268. />
  269. <Route
  270. path='/console/log'
  271. element={
  272. <PrivateRoute>
  273. <Log />
  274. </PrivateRoute>
  275. }
  276. />
  277. <Route
  278. path='/console'
  279. element={
  280. <PrivateRoute>
  281. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  282. <Dashboard />
  283. </Suspense>
  284. </PrivateRoute>
  285. }
  286. />
  287. <Route
  288. path='/console/midjourney'
  289. element={
  290. <PrivateRoute>
  291. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  292. <Midjourney />
  293. </Suspense>
  294. </PrivateRoute>
  295. }
  296. />
  297. <Route
  298. path='/console/task'
  299. element={
  300. <PrivateRoute>
  301. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  302. <Task />
  303. </Suspense>
  304. </PrivateRoute>
  305. }
  306. />
  307. <Route
  308. path='/pricing'
  309. element={
  310. pricingRequireAuth ? (
  311. <PrivateRoute>
  312. <Suspense
  313. fallback={<Loading></Loading>}
  314. key={location.pathname}
  315. >
  316. <Pricing />
  317. </Suspense>
  318. </PrivateRoute>
  319. ) : (
  320. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  321. <Pricing />
  322. </Suspense>
  323. )
  324. }
  325. />
  326. <Route
  327. path='/about'
  328. element={
  329. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  330. <About />
  331. </Suspense>
  332. }
  333. />
  334. <Route
  335. path='/user-agreement'
  336. element={
  337. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  338. <UserAgreement />
  339. </Suspense>
  340. }
  341. />
  342. <Route
  343. path='/privacy-policy'
  344. element={
  345. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  346. <PrivacyPolicy />
  347. </Suspense>
  348. }
  349. />
  350. <Route
  351. path='/user-agreement'
  352. element={
  353. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  354. <Terms />
  355. </Suspense>
  356. }
  357. />
  358. <Route
  359. path='/privacy-policy'
  360. element={
  361. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  362. <UsagePolicy />
  363. </Suspense>
  364. }
  365. />
  366. <Route
  367. path='/console/chat/:id?'
  368. element={
  369. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  370. <Chat />
  371. </Suspense>
  372. }
  373. />
  374. {/* 方便使用chat2link直接跳转聊天... */}
  375. <Route
  376. path='/chat2link'
  377. element={
  378. <PrivateRoute>
  379. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  380. <Chat2Link />
  381. </Suspense>
  382. </PrivateRoute>
  383. }
  384. />
  385. <Route path='*' element={<NotFound />} />
  386. </Routes>
  387. </SetupCheck>
  388. );
  389. }
  390. export default App;