index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import React, { useContext, useEffect, useState } from 'react';
  2. import { Card, Col, Row } from '@douyinfe/semi-ui';
  3. import { API, showError, showNotice, timestamp2string } from '../../helpers';
  4. import { StatusContext } from '../../context/Status';
  5. import { marked } from 'marked';
  6. import { StyleContext } from '../../context/Style/index.js';
  7. import { useTranslation } from 'react-i18next';
  8. const Home = () => {
  9. const { t } = useTranslation();
  10. const [statusState] = useContext(StatusContext);
  11. const [homePageContentLoaded, setHomePageContentLoaded] = useState(false);
  12. const [homePageContent, setHomePageContent] = useState('');
  13. const [styleState, styleDispatch] = useContext(StyleContext);
  14. const displayNotice = async () => {
  15. const res = await API.get('/api/notice');
  16. const { success, message, data } = res.data;
  17. if (success) {
  18. let oldNotice = localStorage.getItem('notice');
  19. if (data !== oldNotice && data !== '') {
  20. const htmlNotice = marked(data);
  21. showNotice(htmlNotice, true);
  22. localStorage.setItem('notice', data);
  23. }
  24. } else {
  25. showError(message);
  26. }
  27. };
  28. const displayHomePageContent = async () => {
  29. setHomePageContent(localStorage.getItem('home_page_content') || '');
  30. const res = await API.get('/api/home_page_content');
  31. const { success, message, data } = res.data;
  32. if (success) {
  33. let content = data;
  34. if (!data.startsWith('https://')) {
  35. content = marked.parse(data);
  36. }
  37. setHomePageContent(content);
  38. localStorage.setItem('home_page_content', content);
  39. // 如果内容是 URL,则发送主题模式
  40. if (data.startsWith('https://')) {
  41. const iframe = document.querySelector('iframe');
  42. if (iframe) {
  43. const theme = localStorage.getItem('theme-mode') || 'light';
  44. // 测试是否正确传递theme-mode给iframe
  45. // console.log('Sending theme-mode to iframe:', theme);
  46. iframe.onload = () => {
  47. iframe.contentWindow.postMessage({ themeMode: theme }, '*');
  48. };
  49. }
  50. }
  51. } else {
  52. showError(message);
  53. setHomePageContent('加载首页内容失败...');
  54. }
  55. setHomePageContentLoaded(true);
  56. };
  57. const getStartTimeString = () => {
  58. const timestamp = statusState?.status?.start_time;
  59. return statusState.status ? timestamp2string(timestamp) : '';
  60. };
  61. useEffect(() => {
  62. displayNotice().then();
  63. displayHomePageContent().then();
  64. });
  65. return (
  66. <>
  67. {homePageContentLoaded && homePageContent === '' ? (
  68. <>
  69. <Card
  70. bordered={false}
  71. headerLine={false}
  72. title={t('系统状况')}
  73. bodyStyle={{ padding: '10px 20px' }}
  74. >
  75. <Row gutter={16}>
  76. <Col span={12}>
  77. <Card
  78. title={t('系统信息')}
  79. headerExtraContent={
  80. <span
  81. style={{
  82. fontSize: '12px',
  83. color: 'var(--semi-color-text-1)',
  84. }}
  85. >
  86. {t('系统信息总览')}
  87. </span>
  88. }
  89. >
  90. <p>{t('名称')}:{statusState?.status?.system_name}</p>
  91. <p>
  92. {t('版本')}:
  93. {statusState?.status?.version
  94. ? statusState?.status?.version
  95. : 'unknown'}
  96. </p>
  97. <p>
  98. {t('源码')}:
  99. <a
  100. href='https://github.com/Calcium-Ion/new-api'
  101. target='_blank'
  102. rel='noreferrer'
  103. >
  104. https://github.com/Calcium-Ion/new-api
  105. </a>
  106. </p>
  107. <p>
  108. {t('协议')}:
  109. <a
  110. href='https://www.apache.org/licenses/LICENSE-2.0'
  111. target='_blank'
  112. rel='noreferrer'
  113. >
  114. Apache-2.0 License
  115. </a>
  116. </p>
  117. <p>{t('启动时间')}:{getStartTimeString()}</p>
  118. </Card>
  119. </Col>
  120. <Col span={12}>
  121. <Card
  122. title={t('系统配置')}
  123. headerExtraContent={
  124. <span
  125. style={{
  126. fontSize: '12px',
  127. color: 'var(--semi-color-text-1)',
  128. }}
  129. >
  130. {t('系统配置总览')}
  131. </span>
  132. }
  133. >
  134. <p>
  135. {t('邮箱验证')}:
  136. {statusState?.status?.email_verification === true
  137. ? t('已启用')
  138. : t('未启用')}
  139. </p>
  140. <p>
  141. {t('GitHub 身份验证')}:
  142. {statusState?.status?.github_oauth === true
  143. ? t('已启用')
  144. : t('未启用')}
  145. </p>
  146. <p>
  147. {t('微信身份验证')}:
  148. {statusState?.status?.wechat_login === true
  149. ? t('已启用')
  150. : t('未启用')}
  151. </p>
  152. <p>
  153. {t('Turnstile 用户校验')}:
  154. {statusState?.status?.turnstile_check === true
  155. ? t('已启用')
  156. : t('未启用')}
  157. </p>
  158. <p>
  159. {t('Telegram 身份验证')}:
  160. {statusState?.status?.telegram_oauth === true
  161. ? t('已启用')
  162. : t('未启用')}
  163. </p>
  164. <p>
  165. {t('Linux DO 身份验证')}:
  166. {statusState?.status?.linuxdo_oauth === true
  167. ? t('已启用')
  168. : t('未启用')}
  169. </p>
  170. </Card>
  171. </Col>
  172. </Row>
  173. </Card>
  174. </>
  175. ) : (
  176. <>
  177. {homePageContent.startsWith('https://') ? (
  178. <iframe
  179. src={homePageContent}
  180. style={{ width: '100%', height: '100vh', border: 'none' }}
  181. />
  182. ) : (
  183. <div
  184. style={{ fontSize: 'larger' }}
  185. dangerouslySetInnerHTML={{ __html: homePageContent }}
  186. ></div>
  187. )}
  188. </>
  189. )}
  190. </>
  191. );
  192. };
  193. export default Home;