App.jsx 10 KB

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