index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. const Home = () => {
  7. const [statusState] = useContext(StatusContext);
  8. const [homePageContentLoaded, setHomePageContentLoaded] = useState(false);
  9. const [homePageContent, setHomePageContent] = useState('');
  10. const displayNotice = async () => {
  11. const res = await API.get('/api/notice');
  12. const { success, message, data } = res.data;
  13. if (success) {
  14. let oldNotice = localStorage.getItem('notice');
  15. if (data !== oldNotice && data !== '') {
  16. const htmlNotice = marked(data);
  17. showNotice(htmlNotice, true);
  18. localStorage.setItem('notice', data);
  19. }
  20. } else {
  21. showError(message);
  22. }
  23. };
  24. const displayHomePageContent = async () => {
  25. setHomePageContent(localStorage.getItem('home_page_content') || '');
  26. const res = await API.get('/api/home_page_content');
  27. const { success, message, data } = res.data;
  28. if (success) {
  29. let content = data;
  30. if (!data.startsWith('https://')) {
  31. content = marked.parse(data);
  32. }
  33. setHomePageContent(content);
  34. localStorage.setItem('home_page_content', content);
  35. } else {
  36. showError(message);
  37. setHomePageContent('加载首页内容失败...');
  38. }
  39. setHomePageContentLoaded(true);
  40. };
  41. const getStartTimeString = () => {
  42. const timestamp = statusState?.status?.start_time;
  43. return statusState.status ? timestamp2string(timestamp) : '';
  44. };
  45. useEffect(() => {
  46. displayNotice().then();
  47. displayHomePageContent().then();
  48. }, []);
  49. return (
  50. <>
  51. {
  52. homePageContentLoaded && homePageContent === '' ?
  53. <>
  54. <Card
  55. bordered={false}
  56. headerLine={false}
  57. title='系统状况'
  58. bodyStyle={{ padding: '10px 20px' }}
  59. >
  60. <Row gutter={16}>
  61. <Col span={12}>
  62. <Card
  63. title='系统信息'
  64. headerExtraContent={<span
  65. style={{ fontSize: '12px', color: 'var(--semi-color-text-1)' }}>系统信息总览</span>}>
  66. <p>名称:{statusState?.status?.system_name}</p>
  67. <p>版本:{statusState?.status?.version ? statusState?.status?.version : 'unknown'}</p>
  68. <p>
  69. 源码:
  70. <a
  71. href='https://github.com/songquanpeng/one-api'
  72. target='_blank' rel='noreferrer'
  73. >
  74. https://github.com/songquanpeng/one-api
  75. </a>
  76. </p>
  77. <p>启动时间:{getStartTimeString()}</p>
  78. </Card>
  79. </Col>
  80. <Col span={12}>
  81. <Card
  82. title='系统配置'
  83. headerExtraContent={<span
  84. style={{ fontSize: '12px', color: 'var(--semi-color-text-1)' }}>系统配置总览</span>}>
  85. <p>
  86. 邮箱验证:
  87. {statusState?.status?.email_verification === true ? '已启用' : '未启用'}
  88. </p>
  89. <p>
  90. GitHub 身份验证:
  91. {statusState?.status?.github_oauth === true ? '已启用' : '未启用'}
  92. </p>
  93. <p>
  94. 微信身份验证:
  95. {statusState?.status?.wechat_login === true ? '已启用' : '未启用'}
  96. </p>
  97. <p>
  98. Turnstile 用户校验:
  99. {statusState?.status?.turnstile_check === true ? '已启用' : '未启用'}
  100. </p>
  101. <p>
  102. Telegram 身份验证:
  103. {statusState?.status?.telegram_oauth === true
  104. ? '已启用' : '未启用'}
  105. </p>
  106. </Card>
  107. </Col>
  108. </Row>
  109. </Card>
  110. </>
  111. : <>
  112. {
  113. homePageContent.startsWith('https://') ?
  114. <iframe src={homePageContent} style={{ width: '100%', height: '100vh', border: 'none' }} /> :
  115. <div style={{ fontSize: 'larger' }} dangerouslySetInnerHTML={{ __html: homePageContent }}></div>
  116. }
  117. </>
  118. }
  119. </>
  120. );
  121. };
  122. export default Home;