wujingjing
2024-10-24 98bb96923a7fa57b46dc331890ff051a3cd11253
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<template>
    <div class="w100 h100 flex items-center flex-column box-border pr-1">
        <div class="w100 box-border px-[18px] py-0 flex-0">
            <el-button
                @click="newChatRoomClick"
                icon="ele-Plus"
                color="#1c86ff"
                class="flex items-center box-border mb-3 justify-center add_room cursor-pointer w-[124px] h-[32.88px] rounded-s-md text-white opacity-100"
            >
                新建聊天室
            </el-button>
        </div>
        <div
            class="flex flex-col flex-auto w-[210.98px] rounded-t-lg box-border mb-3 relative opacity-100 shadow-lg shadow-[[#0e0e0f]-500/50"
        >
            <div class="group flex-0 relative w100 h-[34px] bg-[#2b2c30]">
                <el-input clearable v-model="queryParams.title" placeholder="搜索" class="set-input">
                    <template #prefix>
                        <el-icon><search /></el-icon>
                    </template>
                </el-input>
                <div
                    class="absolute hidden top-[100%] w-[84px] z-[1001] left-0 group-hover:block overflow-hidden rounded-md text-sm text-gray-500 bg-[#fff] py-1.5"
                >
                    <div
                        class="w100 relative hover:bg-[#e6f1ff]"
                        v-for="item in Object.keys(dateFilterMap)"
                        :key="item"
                        @click="filterDateClick(Number(item))"
                    >
                        <div
                            class="size-2 absolute left-2 rounded-full bg-[#2a82e4]"
                            :style="{ display: item === activeDateFilter + '' ? 'block' : 'none' }"
                            style="top: calc(50% - 0.25rem)"
                        ></div>
                        <div class="w100 relative h-[28px] flex items-center justify-center cursor-pointer">{{ dateFilterMap[item] }}</div>
                    </div>
                </div>
            </div>
 
            <div class="flex-auto text-[#ccc] flex flex-col items-center mt-6 overflow-y-auto set-scroll" ref="chatRoomRef">
                <div
                    :class="{ 'bg-[#41424a]': item.id === activeRoomId }"
                    class="group flex items-center w-full h-10 rounded-md cursor-pointer px-2 py-2 flex-0"
                    v-for="(item, index) in queryData"
                    :key="index"
                    @click="roomClick(item)"
                >
                    <div class="ywifont ywicon-xiaoxi1 flex-0 mr-2.5"></div>
                    <div class="flex-auto text-ellipsis text-nowrap text-sm group-hover:text-[#0084ff]">{{ item.title }}</div>
                    <div class="text-gray-100 flex items-center space-x-2 ml-1">
                        <!-- <div class="ywifont invisible ywicon-bianji group-hover:visible !text-sm"></div> -->
 
                        <el-popconfirm title="确定删除聊天记录?" @confirm.stop="confirmDeleteChatRoom(item)" width="180">
                            <template #reference>
                                <div class="ywifont invisible ywicon-shanchu3 group-hover:visible"></div>
                            </template>
                        </el-popconfirm>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
 
<script setup lang="ts">
import { Search } from '@element-plus/icons-vue';
import { computed, onMounted, reactive, ref, watch } from 'vue';
import type { ChatRoomItem } from './types';
import { CreateHistoryGroup, DeleteHistoryGroups, GetHistoryGroups } from '/@/api/ai/chat';
import router from '/@/router';
import { activeRoomId, chatRoomList } from '/@/stores/chatRoom';
import { DateFilter, dateFilterMap } from '/@/model/types/date';
import { debounce, getRecentDateRange } from '/@/utils/util';
import moment from 'moment';
import { useSearch } from '/@/hooks/useSearch';
import { gotoRoute } from '/@/utils/route';
 
const chatRoomRef = ref<HTMLDivElement>(null);
const queryParams = ref({
    title: '',
});
 
const gotoAnswerPage = (room: ChatRoomItem) => {
    if (room.isInitial) {
        gotoRoute({
            name: 'Home',
            query: {
                id: room.id,
            },
        });
    } else {
        gotoRoute({
            name: 'AskAnswer',
            query: {
                id: room.id,
            },
        });
    }
 
    activeRoomId.value = room.id;
};
 
const newChatRoomClick = async () => {
    const res = await CreateHistoryGroup({
        group_title: '新建对话开始',
    });
 
    const newRoom = {
        id: res.history_group_id,
        isInitial: true,
        title: '新建对话开始',
    };
    if (!chatRoomList.value) {
        chatRoomList.value = [newRoom];
    } else {
        chatRoomList.value.unshift(newRoom);
    }
    gotoAnswerPage(newRoom);
};
 
const roomClick = (room: ChatRoomItem) => {
    gotoAnswerPage(room);
};
 
const confirmDeleteChatRoom = async (room: ChatRoomItem) => {
    const res = await DeleteHistoryGroups({
        history_group_id: room.id,
    });
 
    const foundIndex = chatRoomList.value.findIndex((item) => item === room);
    chatRoomList.value.splice(foundIndex, 1);
    if (chatRoomList.value.length === 0) {
        newChatRoomClick();
        return;
    }
    roomClick(chatRoomList.value[0]);
    chatRoomRef.value.firstElementChild?.scrollIntoView();
};
 
//#region ====================== 日期筛选 ======================
 
const activeDateFilter = ref<DateFilter>(DateFilter.All);
const filterDateClick = (dateFilter: DateFilter) => {
    activeDateFilter.value = dateFilter;
};
 
const filteredChatRoomList = computed(() => {
    if (activeDateFilter.value === DateFilter.All) return chatRoomList.value;
    else {
        let dayCount = null;
        switch (activeDateFilter.value) {
            case DateFilter.AWeek:
                dayCount = 7;
                break;
 
            case DateFilter.AMonth:
                dayCount = 30;
 
                break;
            case DateFilter.ThreeMonth:
                dayCount = 90;
                break;
        }
 
        const [startDay, endDay] = getRecentDateRange(dayCount);
        const data = chatRoomList.value.filter((item) => moment(item.createTime).isBetween(startDay, endDay));
        return data;
    }
});
//#endregion
 
//#region ====================== 搜索聊天室 ======================
const { query, queryData } = useSearch(filteredChatRoomList, queryParams);
 
const debounceQuery = debounce(query);
 
watch(
    () => queryParams.value.title,
    (val) => {
        debounceQuery();
    }
);
//#endregion
 
onMounted(async () => {
    const res = await GetHistoryGroups();
 
    const resData = (res?.groups || []) as any[];
    // 按最晚时间到最早时间
    chatRoomList.value = resData
        ?.toSorted((a, b) => {
            return b.create_time.localeCompare(a.create_time);
        })
        .map((item) => {
            return {
                id: item.group_id,
                title: item.group_title,
                createTime: item.create_time,
                isInitial: Number(item.chat_count) === 0,
            };
        });
    if (!chatRoomList.value || chatRoomList.value.length === 0 ) {
        newChatRoomClick();
    } else {
        roomClick(chatRoomList.value[0]);
    }
});
</script>
<style scoped lang="scss">
.set-input {
    :deep(.el-input__wrapper) {
        width: 100%;
        height: 100%;
        font-size: 12px;
        font-weight: 400;
        letter-spacing: 0;
        color: #e5e5e5;
        border-radius: 6px;
        border: 1px solid transparent;
        box-sizing: border-box;
        line-height: 34px;
        // padding-left: 31px;
        padding-right: 10px;
        background-color: transparent;
        cursor: pointer;
        transition: color 0.2s ease-in-out;
        box-shadow: unset;
    }
    :deep(.el-input__inner) {
        &::placeholder {
            color: white;
        }
        color: white;
    }
}
::-webkit-scrollbar {
    height: 0;
    width: 0;
    color: transparent;
}
</style>