Record.tsx 4.4 KB

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