App.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/oidc'
  185. element={
  186. <Suspense fallback={<Loading></Loading>}>
  187. <OAuth2Callback type='oidc'></OAuth2Callback>
  188. </Suspense>
  189. }
  190. />
  191. <Route
  192. path='/oauth/linuxdo'
  193. element={
  194. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  195. <OAuth2Callback type='linuxdo'></OAuth2Callback>
  196. </Suspense>
  197. }
  198. />
  199. <Route
  200. path='/console/setting'
  201. element={
  202. <AdminRoute>
  203. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  204. <Setting />
  205. </Suspense>
  206. </AdminRoute>
  207. }
  208. />
  209. <Route
  210. path='/console/personal'
  211. element={
  212. <PrivateRoute>
  213. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  214. <PersonalSetting />
  215. </Suspense>
  216. </PrivateRoute>
  217. }
  218. />
  219. <Route
  220. path='/console/topup'
  221. element={
  222. <PrivateRoute>
  223. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  224. <TopUp />
  225. </Suspense>
  226. </PrivateRoute>
  227. }
  228. />
  229. <Route
  230. path='/console/log'
  231. element={
  232. <PrivateRoute>
  233. <Log />
  234. </PrivateRoute>
  235. }
  236. />
  237. <Route
  238. path='/console'
  239. element={
  240. <PrivateRoute>
  241. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  242. <Dashboard />
  243. </Suspense>
  244. </PrivateRoute>
  245. }
  246. />
  247. <Route
  248. path='/console/midjourney'
  249. element={
  250. <PrivateRoute>
  251. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  252. <Midjourney />
  253. </Suspense>
  254. </PrivateRoute>
  255. }
  256. />
  257. <Route
  258. path='/console/task'
  259. element={
  260. <PrivateRoute>
  261. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  262. <Task />
  263. </Suspense>
  264. </PrivateRoute>
  265. }
  266. />
  267. <Route
  268. path='/pricing'
  269. element={
  270. pricingRequireAuth ? (
  271. <PrivateRoute>
  272. <Suspense
  273. fallback={<Loading></Loading>}
  274. key={location.pathname}
  275. >
  276. <Pricing />
  277. </Suspense>
  278. </PrivateRoute>
  279. ) : (
  280. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  281. <Pricing />
  282. </Suspense>
  283. )
  284. }
  285. />
  286. <Route
  287. path='/about'
  288. element={
  289. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  290. <About />
  291. </Suspense>
  292. }
  293. />
  294. <Route
  295. path='/user-agreement'
  296. element={
  297. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  298. <UserAgreement />
  299. </Suspense>
  300. }
  301. />
  302. <Route
  303. path='/privacy-policy'
  304. element={
  305. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  306. <PrivacyPolicy />
  307. </Suspense>
  308. }
  309. />
  310. <Route
  311. path='/console/chat/:id?'
  312. element={
  313. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  314. <Chat />
  315. </Suspense>
  316. }
  317. />
  318. {/* 方便使用chat2link直接跳转聊天... */}
  319. <Route
  320. path='/chat2link'
  321. element={
  322. <PrivateRoute>
  323. <Suspense fallback={<Loading></Loading>} key={location.pathname}>
  324. <Chat2Link />
  325. </Suspense>
  326. </PrivateRoute>
  327. }
  328. />
  329. <Route path='*' element={<NotFound />} />
  330. </Routes>
  331. </SetupCheck>
  332. );
  333. }
  334. export default App;