App.jsx 11 KB

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