master
2022-02-14 1684d66eaf39f77980b987359f413d2ff5714940
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
<template>
    <div class="cl-chat-input">
        <!-- 工具栏 -->
        <div class="cl-chat-input__opbar">
            <ul>
                <li>
                    <!-- 表情 -->
                    <emoji @select="onEmojiSelect" />
                </li>
                <li>
                    <!-- 图片上传 -->
                    <upload name="image" accept="image/*" @before-upload="append" @success="send">
                        <img src="../static/images/image.png" alt="" />
                    </upload>
                </li>
                <li>
                    <!-- 视频上传 -->
                    <upload name="video" accept="video/*" @before-upload="append" @success="send">
                        <img src="../static/images/video.png" alt="" />
                    </upload>
                </li>
            </ul>
        </div>
 
        <!-- 输入框,发送按钮 -->
        <div class="cl-chat-input__content">
            <el-input
                v-model="text"
                placeholder="请描述您想咨询的问题"
                type="textarea"
                resize="none"
                :rows="5"
                @keyup.enter="onTextSend"
            />
 
            <el-button type="primary" size="mini" :disabled="!text" @click="onTextSend"
                >发送</el-button
            >
        </div>
    </div>
</template>
 
<script lang="ts">
import { defineComponent, inject, nextTick, reactive, ref } from "vue";
import Emoji from "./emoji.vue";
import Upload from "./upload.vue";
import { useCool } from "/@/cool";
 
export default defineComponent({
    components: {
        Emoji,
        Upload
    },
 
    setup() {
        const { store, mitt } = useCool();
 
        // 聊天信息
        const chat = inject<any>("chat");
 
        // 输入值
        const text = ref<string>("");
 
        // 表情
        const emoji = reactive<any>({
            visible: false
        });
 
        // 追加消息
        function append(data: any) {
            store.commit("APPEND_MESSAGE_LIST", data);
            mitt.emit("message.scrollToBottom");
        }
 
        // 发送消息
        function send(data: any, isAppend?: boolean) {
            const { id, userId } = store.getters.session;
 
            // 格式化内容
            data.content = JSON.stringify(data.content);
 
            // 更新消息
            store.commit("UPDATE_SESSION", data);
 
            if (chat.socket) {
                chat.socket.emit(`user@${userId}`, {
                    contentType: data.contentType,
                    type: 0,
                    content: data.content,
                    sessionId: id
                });
            }
 
            if (isAppend) {
                append(data);
            }
        }
 
        // 发送文本内容
        function onTextSend() {
            if (text.value) {
                if (text.value.replace(/\n/g, "") !== "") {
                    const data = {
                        type: 0,
                        contentType: 0,
                        content: {
                            text: text.value
                        }
                    };
 
                    send(data, true);
 
                    nextTick(() => {
                        text.value = "";
                    });
                }
            }
        }
 
        // 图片选择
        function onImageSelect(res: any) {
            send(
                {
                    content: {
                        imageUrl: res.data
                    },
                    type: 0,
                    contentType: 1
                },
                true
            );
        }
 
        // 表情选择
        function onEmojiSelect(url: string) {
            emoji.visible = false;
            send(
                {
                    content: {
                        imageUrl: url
                    },
                    type: 0,
                    contentType: 2
                },
                true
            );
        }
 
        // 视频选择
        function onVideoSelect(url: string) {
            send(
                {
                    content: {
                        videoUrl: url
                    },
                    type: 0,
                    contentType: 4
                },
                true
            );
        }
 
        return {
            text,
            emoji,
            send,
            append,
            onTextSend,
            onImageSelect,
            onEmojiSelect,
            onVideoSelect
        };
    }
});
</script>
 
<style lang="scss">
.cl-chat-input {
    background-color: #fff;
    padding: 10px;
    border-radius: 3px;
 
    &__opbar {
        margin-bottom: 5px;
 
        ul {
            display: flex;
 
            li {
                list-style: none;
                margin-right: 10px;
                cursor: pointer;
 
                &:hover {
                    opacity: 0.7;
                }
 
                img {
                    height: 26px;
                    width: 26px;
                }
            }
        }
    }
 
    &__content {
        position: relative;
 
        .el-button {
            position: absolute;
            right: 15px;
            bottom: 10px;
        }
    }
}
</style>