App.jsx 11 KB

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