Record.tsx 4.5 KB

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