index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, i18n } = 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. iframe.contentWindow.postMessage({ lang: i18n.language }, '*');
  49. };
  50. }
  51. }
  52. } else {
  53. showError(message);
  54. setHomePageContent('加载首页内容失败...');
  55. }
  56. setHomePageContentLoaded(true);
  57. };
  58. const getStartTimeString = () => {
  59. const timestamp = statusState?.status?.start_time;
  60. return statusState.status ? timestamp2string(timestamp) : '';
  61. };
  62. useEffect(() => {
  63. displayNotice().then();
  64. displayHomePageContent().then();
  65. });
  66. return (
  67. <>
  68. {homePageContentLoaded && homePageContent === '' ? (
  69. <>
  70. <Card
  71. bordered={false}
  72. headerLine={false}
  73. title={t('系统状况')}
  74. bodyStyle={{ padding: '10px 20px' }}
  75. >
  76. <Row gutter={16}>
  77. <Col span={12}>
  78. <Card
  79. title={t('系统信息')}
  80. headerExtraContent={
  81. <span
  82. style={{
  83. fontSize: '12px',
  84. color: 'var(--semi-color-text-1)',
  85. }}
  86. >
  87. {t('系统信息总览')}
  88. </span>
  89. }
  90. >
  91. <p>{t('名称')}:{statusState?.status?.system_name}</p>
  92. <p>
  93. {t('版本')}:
  94. {statusState?.status?.version
  95. ? statusState?.status?.version
  96. : 'unknown'}
  97. </p>
  98. <p>
  99. {t('源码')}:
  100. <a
  101. href='https://github.com/Calcium-Ion/new-api'
  102. target='_blank'
  103. rel='noreferrer'
  104. >
  105. https://github.com/Calcium-Ion/new-api
  106. </a>
  107. </p>
  108. <p>
  109. {t('协议')}:
  110. <a
  111. href='https://www.apache.org/licenses/LICENSE-2.0'
  112. target='_blank'
  113. rel='noreferrer'
  114. >
  115. Apache-2.0 License
  116. </a>
  117. </p>
  118. <p>{t('启动时间')}:{getStartTimeString()}</p>
  119. </Card>
  120. </Col>
  121. <Col span={12}>
  122. <Card
  123. title={t('系统配置')}
  124. headerExtraContent={
  125. <span
  126. style={{
  127. fontSize: '12px',
  128. color: 'var(--semi-color-text-1)',
  129. }}
  130. >
  131. {t('系统配置总览')}
  132. </span>
  133. }
  134. >
  135. <p>
  136. {t('邮箱验证')}:
  137. {statusState?.status?.email_verification === true
  138. ? t('已启用')
  139. : t('未启用')}
  140. </p>
  141. <p>
  142. {t('GitHub 身份验证')}:
  143. {statusState?.status?.github_oauth === true
  144. ? t('已启用')
  145. : t('未启用')}
  146. </p>
  147. <p>
  148. {t('微信身份验证')}:
  149. {statusState?.status?.wechat_login === true
  150. ? t('已启用')
  151. : t('未启用')}
  152. </p>
  153. <p>
  154. {t('Turnstile 用户校验')}:
  155. {statusState?.status?.turnstile_check === true
  156. ? t('已启用')
  157. : t('未启用')}
  158. </p>
  159. <p>
  160. {t('Telegram 身份验证')}:
  161. {statusState?.status?.telegram_oauth === true
  162. ? t('已启用')
  163. : t('未启用')}
  164. </p>
  165. <p>
  166. {t('Linux DO 身份验证')}:
  167. {statusState?.status?.linuxdo_oauth === true
  168. ? t('已启用')
  169. : t('未启用')}
  170. </p>
  171. </Card>
  172. </Col>
  173. </Row>
  174. </Card>
  175. </>
  176. ) : (
  177. <>
  178. {homePageContent.startsWith('https://') ? (
  179. <iframe
  180. src={homePageContent}
  181. style={{ width: '100%', height: '100vh', border: 'none' }}
  182. />
  183. ) : (
  184. <div
  185. style={{ fontSize: 'larger' }}
  186. dangerouslySetInnerHTML={{ __html: homePageContent }}
  187. ></div>
  188. )}
  189. </>
  190. )}
  191. </>
  192. );
  193. };
  194. export default Home;