| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- <template>
- <div class="conversation-history-container">
- <div class="conversation-history-left"></div>
- <div class="conversation-history-main">
- <HistoryName />
- <HistorySearchInput @search="handleHistorySearch">
- <HistoryTypeDropdown @typeSelected="handleTypeSelected" :list_status="api_status" />
- </HistorySearchInput>
- <HistoryList
- v-if="list_history.length !== 0"
- :list="list_history"
- :can_loadmore="can_loadmore"
- :agent_icon="agent_icon"
- @loadmore="handleListLoadMore"
- @active="handleListItemActive"
- />
- <template v-if="api_status === 1 || api_status === 2">
- <div class="infomations-container">
- <a-spin :size="32" />
- </div>
- </template>
- <template v-if="api_status === 3 && list_history.length === 0">
- <div class="infomations-container">
- <a-empty> {{ $t('conversation.history_empty') }} </a-empty>
- </div>
- </template>
- <template v-if="api_status === 4">
- <div class="infomations-container">
- <a-result status="warning" :title="api_message"> </a-result>
- </div>
- </template>
- <template v-if="api_status === 5">
- <div class="infomations-container">
- <a-result status="error" :title="api_message"> </a-result>
- </div>
- </template>
- <div class="conversation-history-placeholder"></div>
- </div>
- <div class="conversation-history-right"></div>
- </div>
- </template>
- <script setup>
- import { ref, onMounted } from 'vue';
- import HistoryName from '../../modules/conversation-history/HistoryName.vue';
- import HistorySearchInput from '../../modules/conversation-history/HistorySearchInput.vue';
- import HistoryList from '../../modules/conversation-history/HistoryList.vue';
- import HistoryTypeDropdown from '../../modules/conversation-history/HistoryTypeDropdown.vue';
- import { apiConversationHistoryList } from '../../modules/conversation-history/api-conversation-history';
- import { getIconIntelligent } from '../../modules/common/icon-map';
- import { getTodayZeroTimestamp, ONE_DAY, ONE_MONTH } from '../../utils/datetime';
- const emit = defineEmits(['historyActive']);
- const page_no = ref(1); // 当前页码
- const page_count = ref(50); // 每页显示的数量
- const search_text = ref(''); // 搜索字符串
- const agent_id = ref('');
- // const agent_item = ref(null);
- const can_loadmore = ref(true); // 是否可以加载更多
- // api请求的状态
- const api_status = ref(1); // api状态 1 请求未启动 2 请求进心中 3 请求已成功 4 请求失败(网络成功,但是code不等于200) 5 请求出错
- const api_message = ref(''); // 请求消息 与 api_status 对应的 message 消息内容
- // 历史记录列表数据
- const list_history = ref([]); // 历史记录的列表数据
- const count_today = ref(0); // 记录今天的数据量
- const count_month = ref(0); // 记录本月的数据量
- const count_earlier = ref(0); // 记录更早的数据量
- // 智能体的图标
- const agent_icon = ref('');
- const today_zero = getTodayZeroTimestamp(); // 今天零点
- const yesterday_zero = today_zero - ONE_DAY; // 昨天零点
- const last_month_zero = yesterday_zero - ONE_MONTH; // 上个月(30天之前的)零点
- /**
- * 将数据进行分类
- * 依据时间戳,将列表数据分成三种类型:今天、本月、更早
- * 今天:从今天零点开始到现在
- * 本月:从今天零点之前,往前推30天
- * 更早:在30天之前的数据
- * @param res_list
- */
- function dropListByType(res_list) {
- res_list.forEach((item, index) => {
- let label_item = null; // label项目(今天、本月、更早以前)
- const content_item = item; // 数据项目
- // const create_time = content_item.updated_time; // 会话创建时间
- // const create_time_str = content_item.create_date; // "2024-12-18 11:45:23"
- /**
- * 在后端返回的数据中
- * 有些情况下,存在 create_date 字段
- * 另外一些情况下,存在 updated_time字段
- */
- const createTime = new Date(content_item.create_date);
- const createStamp = createTime.getTime();
- let create_stamp = null;
- if (isNaN(createStamp)) {
- // 非法的
- // 使用updated_time
- const updateTime = new Date(content_item.updated_time);
- const updateStamp = updateTime.getTime();
- if (isNaN(updateStamp)) {
- // 非法的: 使用此刻的数据,意味着所有的数据都是今天的
- create_stamp = Date.now();
- } else {
- create_stamp = updateStamp;
- }
- } else {
- create_stamp = createStamp;
- }
- content_item.create_time = create_stamp;
- if (create_stamp >= today_zero) {
- // 今天的数据
- if (count_today.value === 0) {
- label_item = {
- client_field_content_type: 'label',
- client_field_timestamp_type: 'today',
- id: 'today'
- };
- }
- // 非第0个数据
- content_item.client_field_timestamp_type = 'today'; // 客户端自定义字段: 时间类型
- content_item.client_field_content_type = 'content'; // 内容类别
- count_today.value = count_today.value + 1;
- } else if (create_stamp < today_zero && create_stamp > last_month_zero) {
- // 本月的数据
- if (count_month.value === 0) {
- label_item = {
- client_field_content_type: 'label',
- client_field_timestamp_type: 'month',
- id: 'month'
- };
- }
- content_item.client_field_timestamp_type = 'month'; // 客户端自定义字段: 时间类型
- content_item.client_field_content_type = 'content'; // 内容类别
- count_month.value = count_month.value + 1;
- } else {
- // 更早以前的数据
- if (count_earlier.value === 0) {
- label_item = {
- client_field_content_type: 'label',
- client_field_timestamp_type: 'earlier',
- id: 'earlier'
- };
- }
- content_item.client_field_timestamp_type = 'earlier'; // 客户端自定义字段: 时间类型
- content_item.client_field_content_type = 'content'; // 内容类别
- count_earlier.value = count_earlier.value + 1;
- }
- if (label_item) {
- list_history.value.push(label_item);
- }
- list_history.value.push(content_item);
- });
- }
- /**
- * 处理请求参数
- */
- const formatListOptions = () => {
- return {
- search: search_text.value,
- page: page_no.value,
- per_page: page_count.value,
- agent_id: agent_id.value
- };
- };
- /**
- * 调用api请求
- */
- const handleApiRequest = () => {
- // const cur_agent = agent_item.value;
- // if (cur_agent.menuId === 1) {
- // // 报表合并: 报表合并需要使用领一个历史记录
- // }
- // 处理查询参数
- const opts = formatListOptions();
- // 进入加载状态
- api_status.value = 2;
- api_message.value = '';
- apiConversationHistoryList(opts)
- .then((res) => {
- if (res.code / 1 === 200) {
- const res_data = res.data;
- // 成功
- const res_list = res_data.rows; // 历史记录列表
- if (res_list.length < page_count.value) {
- // 数据已经加载完成,不能加载更多了
- can_loadmore.value = false;
- } else {
- can_loadmore.value = true;
- }
- dropListByType(res_list);
- // api请求成功
- api_status.value = 3;
- api_message.value = 'success';
- } else {
- // api请求成功,但是业务失败
- api_status.value = 4;
- api_message.value = res.msg;
- }
- })
- .catch((err) => {
- // api请求出错
- api_status.value = 5;
- api_message.value = err.message;
- });
- };
- /**
- * 请求更多数据
- */
- const handleListLoadMore = () => {
- if (api_status.value === 2) {
- return;
- }
- if (!can_loadmore.value) {
- // 没有更多了
- return;
- }
- page_no.value = page_no.value + 1; // 页码加1
- // 调用api
- handleApiRequest();
- };
- /**
- * 点击某个历史记录触发的事件
- * @param history_id
- */
- const handleListItemActive = (history_id) => {
- emit('historyActive', {
- agent_id: agent_id.value, // agent_id值
- target_id: history_id
- });
- };
- /**
- * 开始搜索
- * @param value
- */
- const handleHistorySearch = (value) => {
- if (api_status.value === 2) {
- return;
- }
- search_text.value = value; // 搜索字符串
- page_no.value = 1; // 页码归位
- list_history.value = []; // 清空数据
- // 调用api
- handleApiRequest();
- };
- /**
- * 选定具体的类型,此时应该执行历史记录获取的请求
- * 当类型选定后,获取此类型对应的历史记录数据
- * @param type_item
- */
- const handleTypeSelected = (type_item) => {
- if (api_status.value === 2) {
- return;
- }
- if (!type_item) {
- return;
- }
- // agent_item.value = type_item;
- // 统计数据归位
- count_today.value = 0;
- count_month.value = 0;
- count_earlier.value = 0;
- // 设置智能体的图标
- agent_icon.value = getIconIntelligent(type_item.icon);
- // 记录Agent ID
- agent_id.value = type_item.id;
- page_no.value = 1; // 页码归位
- list_history.value = []; // 清空数据
- // 调用api
- handleApiRequest(type_item);
- };
- onMounted(() => {
- // // 调用api
- // handleApiRequest();
- });
- </script>
- <style lang="css" scoped>
- .conversation-history-container {
- display: flex;
- justify-content: center;
- margin-left: auto;
- margin-right: auto;
- }
- .conversation-history-left {
- width: 25%;
- flex-shrink: 0;
- }
- .conversation-history-right {
- width: 25%;
- flex-shrink: 0;
- }
- .conversation-history-main {
- /* width: 60%; */
- flex-grow: 1;
- height: 100%;
- margin-left: auto;
- margin-right: auto;
- display: flex;
- flex-direction: column;
- }
- /* 底部占位元素 */
- .conversation-history-placeholder {
- height: 50px;
- }
- .conversation-history-main .infomations-container {
- display: flex;
- justify-content: center;
- align-items: center;
- min-height: 200px;
- }
- </style>
|