Record.tsx 4.5 KB

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