Record.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import * as React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { IconButton } from './button';
  4. import ReturnIcon from "../icons/return.svg";
  5. import Excel from 'js-export-excel';
  6. const RecordApp: React.FC = () => {
  7. const [account, setAccount] = React.useState('');
  8. const [password, setPassword] = React.useState('');
  9. // 点击导出
  10. const onClickExport = async (data: { account: string, password: string }) => {
  11. if (data.account && data.password) {
  12. if (data.account === 'root' && password === 'root@2024') {
  13. const res = await fetch('/api/bigModel');
  14. const body = await res.json();
  15. const list: {
  16. id: string;
  17. type: "system" | "user" | "assistant";
  18. create_time: string;
  19. content: string;
  20. }[] = body.data;
  21. // 标题行
  22. const headerRow = [
  23. { id: 'ID', role: '角色', createTime: '创建时间', content: '内容' },
  24. ];
  25. // 导出数据
  26. const sheetData = [...headerRow, ...list.map(item => ({
  27. id: item.id,
  28. role: item.type,
  29. createTime: item.create_time,
  30. content: item.content,
  31. }))];
  32. // 导出数据到Excel
  33. const option = {
  34. fileName: '聊天记录',
  35. datas: [{
  36. sheetData: sheetData,
  37. sheetName: '聊天记录',
  38. }],
  39. };
  40. // 使用Excel构造函数导出数据
  41. const excel = new Excel(option);
  42. excel.saveExcel();
  43. } else {
  44. alert('账号密码不正确');
  45. }
  46. } else {
  47. alert('请输入账号密码');
  48. }
  49. }
  50. return (
  51. <div>
  52. <div style={{ padding: '14px 20px', borderBottom: '1px solid rgba(0, 0, 0, 0.1)', display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
  53. <div>
  54. <div style={{ fontSize: 20, fontWeight: 'bolder' }}>
  55. 建科招聘聊天记录
  56. </div>
  57. <div style={{ fontSize: 14 }}>
  58. 输入账号密码导出聊天记录
  59. </div>
  60. </div>
  61. <div>
  62. <Link to='/'>
  63. <IconButton
  64. icon={<ReturnIcon />}
  65. bordered
  66. title='返回'
  67. aria='返回'
  68. />
  69. </Link>
  70. </div>
  71. </div>
  72. <div style={{ width: '100%', padding: '20px 0', display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
  73. <input
  74. style={{ width: '50%' }}
  75. type="text"
  76. placeholder="请输入账号"
  77. value={account}
  78. onChange={(e) => {
  79. const value = e.target.value;
  80. setAccount(value);
  81. }}
  82. />
  83. <input
  84. style={{ width: '50%', margin: '20px 0' }}
  85. type="password"
  86. placeholder="请输入密码"
  87. value={password}
  88. onChange={(e) => {
  89. const value = e.target.value;
  90. setPassword(value);
  91. }}
  92. />
  93. <button
  94. style={{
  95. width: '50%',
  96. border: '1px solid rgba(0, 0, 0, 0.1)',
  97. backgroundColor: '#FFFFFF',
  98. borderRadius: '10px',
  99. height: 36,
  100. cursor: 'pointer'
  101. }}
  102. onClick={async () => {
  103. await onClickExport({
  104. account: account,
  105. password: password,
  106. })
  107. }}
  108. >
  109. 导出
  110. </button>
  111. </div>
  112. </div>
  113. );
  114. };
  115. export default RecordApp;