master
2022-04-07 a28f8de41ab2d99457875db7cdd0cc5bf9325f32
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<template>
    <div
        class="cl-chat-session"
        :class="{
            'is-position': browser.isMini,
            'is-show': sessionVisible
        }"
    >
        <!-- 关键字搜索 -->
        <div class="cl-chat-session__search">
            <el-input
                v-model="keyWord"
                placeholder="搜索"
                prefix-icon="el-icon-search"
                size="small"
                clearable
                @clear="onSearch"
                @keyup.enter="onSearch"
            />
        </div>
 
        <!-- 会话列表 -->
        <ul v-loading="loading" class="cl-chat-session__list scroller1">
            <li
                v-for="(item, index) in list"
                :key="index"
                class="cl-chat-session__item"
                :class="{
                    'is-active': session ? item.id == session.id : false
                }"
                @click="toDetail(item)"
                @contextmenu.stop.prevent="openCM($event, item.id, Number(index))"
            >
                <!-- 头像 -->
                <div class="avatar">
                    <el-badge
                        :value="item.serviceUnreadCount"
                        :hidden="item.serviceUnreadCount === 0"
                        :max="99"
                    >
                        <img :src="item.headimgurl" alt="" />
                    </el-badge>
                </div>
 
                <!-- 昵称,内容 -->
                <div class="det">
                    <p class="name">{{ item.nickname }}</p>
                    <p class="content">{{ item.lastMessage }}</p>
                </div>
            </li>
        </ul>
    </div>
</template>
 
<script lang="ts">
import { computed, defineComponent, onUnmounted, reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { isEmpty } from "/@/cool/utils";
import { ContextMenu } from "@cool-vue/crud";
import { parseContent } from "../utils";
import { useCool } from "/@/cool";
 
export default defineComponent({
    setup() {
        const { store, service, mitt } = useCool();
 
        // 当前会话信息
        const session = computed(() => store.getters.session);
        // 是否显示会话列表
        const sessionVisible = computed(() => store.getters.sessionVisible);
        // 浏览器信息
        const browser = computed(() => store.getters.browser);
 
        // 加载状态
        const loading = ref<boolean>(false);
 
        // 分页信息
        const pagination = reactive<any>({
            page: 1,
            size: 100,
            total: 0
        });
 
        // 关键字筛选
        const keyWord = ref<string>("");
 
        // 刷新列表
        async function refresh(params?: any) {
            loading.value = true;
 
            const res = await service.chat.session
                .page({
                    ...pagination,
                    keyWord: keyWord.value,
                    params,
                    order: "updateTime",
                    sort: "desc"
                })
                .then((res: any) => {
                    store.commit("SET_SESSION_LIST", res.list);
                    Object.assign(pagination, res.pagination);
                    return res;
                })
                .catch((err: string) => {
                    ElMessage.error(err);
                    return Promise.reject(err);
                });
 
            loading.value = false;
            return res;
        }
 
        // 搜索关键字
        function onSearch() {
            refresh({ page: 1 });
        }
 
        // 设置会话
        function setSession(item: any) {
            if (item) {
                store.commit("SET_SESSION", item);
                mitt.emit("message.refresh", { page: 1 });
            }
        }
 
        // 会话详情
        function toDetail(item?: any) {
            if (item) {
                // 点击关闭弹窗
                if (browser.value.isMini) store.commit("CLOSE_SESSION");
 
                // 设置为当前会话
                if (!session.value || session.value.id != item.id) {
                    setSession(item);
                }
            } else {
                store.commit("CLEAR_SESSION");
            }
        }
 
        // 数据列表
        const list = computed(() => {
            return store.getters.sessionList
                .map((e: any) => {
                    const { _text } = parseContent(e);
                    return {
                        ...e,
                        lastMessage: _text
                    };
                })
                .sort((a: any, b: any) => {
                    return a.updateTime < b.updateTime ? 1 : -1;
                });
        });
 
        // 右键菜单
        function openCM(e: any, id: any, index: number) {
            ContextMenu.open(e, {
                list: [
                    {
                        label: "删除",
                        icon: "el-icon-delete",
                        callback: (_: any, done: Function) => {
                            service.chat.session.delete({
                                ids: id
                            });
 
                            list.value.splice(index, 1);
 
                            if (id == session.value.id) {
                                toDetail();
                            }
 
                            done();
                        }
                    }
                ]
            });
        }
 
        // PC 端下首次请求读取第一个消息
        refresh().then((res: any) => {
            if (!isEmpty(res.list) && !browser.value.isMini) {
                setSession(res.list[0]);
            }
        });
 
        // 事件监听
        mitt.on("session.refresh", refresh);
 
        // 销毁
        onUnmounted(function () {
            mitt.off("session.refresh", refresh);
        });
 
        return {
            session,
            sessionVisible,
            browser,
            list,
            loading,
            pagination,
            keyWord,
            openCM,
            refresh,
            onSearch,
            toDetail
        };
    }
});
</script>
 
<style lang="scss" scoped>
.cl-chat-session {
    height: 100%;
    width: 0;
    transition: width 0.2s ease-in-out;
    border-radius: 5px;
    background-color: #fff;
    overflow: hidden;
 
    &.is-show {
        width: 250px;
        max-width: 100%;
        margin-right: 5px;
    }
 
    &.is-position {
        position: absolute;
        left: 5px;
        top: 51px;
        height: calc(100% - 56px);
        z-index: 3000;
 
        &.is-show {
            width: calc(100% - 10px);
        }
    }
 
    &__search {
        padding: 10px;
    }
 
    &__list {
        height: calc(100% - 52px);
        overflow: auto;
 
        li {
            display: flex;
            list-style: none;
            padding: 10px;
            border-left: 5px solid #fff;
 
            .avatar {
                margin-right: 12px;
 
                img {
                    display: block;
                    height: 40px;
                    width: 40px;
                    border-radius: 3px;
                    background-color: #eee;
                }
 
                .el-badge {
                    &__content {
                        height: 14px;
                        line-height: 14px;
                        padding: 0 4px;
                        background-color: #fa5151;
                        border: 0;
                    }
                }
            }
 
            .det {
                flex: 1;
                .name {
                    font-size: 13px;
                    margin-top: 1px;
                }
 
                .content {
                    font-size: 12px;
                    margin-top: 5px;
                    color: #666;
                }
 
                .name,
                .content {
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 1;
                }
            }
 
            &.is-active {
                background-color: #eee;
                border-color: $color-primary;
            }
 
            &:hover {
                background-color: #eee;
                cursor: pointer;
            }
        }
    }
 
    &__empty {
        text-align: center;
        margin-top: 10px;
    }
}
</style>