App.jsx 9.5 KB

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