zxl
2025-05-23 5d5b0f7ab0f34019e11901ddcd59cd8b51ea9ff9
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<template>
  <div>
    <el-container class="editor-container">
      <el-header class="no-padding toolbar" height="35px">
        <ul>
          <li v-popover:popoverEmoticon>
            <i class="iconfont icon-icon_im_face" style="font-size: 15px" />
            <p class="tip-title">表情符号</p>
          </li>
          <!-- <li @click="codeBlock.isShow = true">
            <i class="iconfont icon-daima" />
            <p class="tip-title">代码片段</p>
          </li>
          <li @click="recorder = true">
            <i class="el-icon-headset" />
            <p class="tip-title">语音消息</p>
          </li> -->
          <!-- #TODO 发图片功能暂时隐藏 此处会涉及到token过期 -->
          <!-- <li @click="$refs.restFile.click()">
            <i class="el-icon-picture-outline-round" />
            <p class="tip-title">图片</p>
          </li> -->
          <!-- <li @click="$refs.restFile2.click()">
            <i class="el-icon-folder" />
            <p class="tip-title">附件</p>
          </li>
          <li @click="filesManager.isShow = true">
            <i class="el-icon-folder-opened" />
            <p class="tip-title">上传管理</p>
          </li>
          <li v-show="isGroupTalk" @click="vote.isShow = true">
            <i class="el-icon-s-data" />
            <p class="tip-title">发起投票</p>
          </li> -->
 
          <!--          <p class="text-tips no-select">-->
          <!--            <span>按Enter发送 / Shift+Enter 换行</span>-->
          <!--            <el-popover placement="top-end" width="600" trigger="click">-->
          <!--              <div class="editor-books">-->
          <!--                <div class="books-title">编辑说明:</div>-->
          <!--                <p>-->
          <!--                  1.-->
          <!--                  支持上传QQ及微信截图,在QQ或微信中截图后使用Ctrl+v上传图片。-->
          <!--                </p>-->
          <!--                <p>-->
          <!--                  2.-->
          <!--                  支持浏览器及Word文档中的图片复制上传、复制后使用Ctrl+v上传图片。-->
          <!--                </p>-->
          <!--                <p>3. 支持图片拖拽上传。</p>-->
          <!--                <p>4. 支持文件上传 ( 文件小于100M ) 。</p>-->
          <!--                <p>5. 按Enter发送 / Shift+Enter 换行。</p>-->
          <!--                <p>-->
          <!--                  6.-->
          <!--                  注意:当文件正在上传时,请勿关闭网页或离开当前对话框,否则将导致文件停止上传或上传失败。-->
          <!--                </p>-->
          <!--              </div>-->
          <!--              <i class="el-icon-info" slot="reference" />-->
          <!--            </el-popover>-->
          <!--          </p>-->
        </ul>
 
        <el-popover ref="popoverEmoticon" placement="top-start" trigger="click" width="300"
          popper-class="no-padding el-popover-em">
          <MeEditorEmoticon ref="editorEmoticon" @selected="selecteEmoticon" />
        </el-popover>
 
        <form enctype="multipart/form-data" style="display: none" ref="fileFrom">
          <input type="file" ref="restFile" accept="image/*" @change="uploadImageChange" />
          <input type="file" ref="restFile2" @change="uploadFileChange" />
        </form>
      </el-header>
      <el-main class="no-padding textarea">
        <textarea ref="textarea" v-paste="pasteImage" v-drag="dragPasteImage" v-model.trim="editorText" rows="6"
          placeholder="你想要的聊点什么呢 ..." @keydown="keydownEvent($event)" @input="inputEvent($event)" />
      </el-main>
    </el-container>
 
    <!-- 图片查看器 -->
    <MeEditorImageView ref="imageViewer" v-model="imageViewer.isShow" :file="imageViewer.file"
      @confirm="confirmUploadImage" />
 
    <MeEditorRecorder v-if="recorder" @close="recorder = false" />
 
    <!-- 代码块编辑器 -->
    <TalkCodeBlock v-if="codeBlock.isShow" :edit-mode="codeBlock.editMode" @close="codeBlock.isShow = false"
      @confirm="confirmCodeBlock" />
 
    <!-- 文件上传管理器 -->
    <MeEditorFileManage ref="filesManager" v-model="filesManager.isShow" />
 
    <MeEditorVote v-if="vote.isShow" @close="
      () => {
        this.vote.isShow = false;
      }
    " />
  </div>
</template>
 
<script>
import MeEditorEmoticon from "./MeEditorEmoticon";
import MeEditorFileManage from "./MeEditorFileManage";
import MeEditorImageView from "./MeEditorImageView";
import MeEditorRecorder from "./MeEditorRecorder";
import MeEditorVote from "./MeEditorVote";
import TalkCodeBlock from "@/components/chat/TalkCodeBlock";
import { getPasteImgs, getDragPasteImg } from "@/utils/editor";
import { findTalk } from "@/utils/talk";
 
import {
  ServeSendTalkCodeBlock,
  ServeSendTalkImage,
  ServeSendEmoticon,
} from "@/api/chat";
 
export default {
  name: "MeEditor",
  components: {
    MeEditorEmoticon,
    MeEditorFileManage,
    MeEditorImageView,
    TalkCodeBlock,
    MeEditorRecorder,
    MeEditorVote,
  },
  computed: {
    talkUser () {
      return this.$store.state.dialogue.index_name;
    },
    isGroupTalk () {
      return this.$store.state.dialogue.talk_type == 2;
    },
  },
  watch: {
    talkUser (n_index_name) {
      this.$refs.filesManager.clear();
      this.editorText = this.getDraftText(n_index_name);
    },
  },
  data () {
    return {
      // 当前编辑的内容
      editorText: "",
 
      // 图片查看器相关信息
      imageViewer: {
        isShow: false,
        file: null,
      },
 
      codeBlock: {
        isShow: false,
        editMode: true,
      },
 
      filesManager: {
        isShow: false,
      },
 
      vote: {
        isShow: false,
      },
 
      // 录音器
      recorder: false,
 
      // 上次发送消息的时间
      sendtime: 0,
 
      // 发送间隔时间(默认1秒)
      interval: 1000,
    };
  },
  methods: {
    // 读取对话编辑草稿信息 并赋值给当前富文本
    getDraftText (index_name) {
      return findTalk(index_name)?.draft_text || "";
    },
 
    //复制粘贴图片回调方法
    pasteImage (e) {
      let files = getPasteImgs(e);
      if (files.length == 0) return;
 
      this.openImageViewer(files[0]);
    },
 
    //拖拽上传图片回调方法
    dragPasteImage (e) {
      let files = getDragPasteImg(e);
      if (files.length == 0) return;
 
      this.openImageViewer(files[0]);
    },
 
    inputEvent (e) {
      this.$emit("keyboard-event", e.target.value);
    },
 
    // 键盘按下监听事件
    keydownEvent (e) {
      if (e.keyCode == 13 && this.editorText == "") {
        e.preventDefault();
      }
 
      // 回车发送消息
      if (e.keyCode == 13 && e.shiftKey == false && this.editorText != "") {
        let currentTime = new Date().getTime();
 
        if (this.sendtime > 0) {
          // 判断 1秒内只能发送一条消息
          if (currentTime - this.sendtime < this.interval) {
            e.preventDefault();
            return false;
          }
        }
 
        this.$emit("send", this.editorText);
        this.editorText = "";
        this.sendtime = currentTime;
        e.preventDefault();
      }
    },
 
    // 选择图片文件后回调方法
    uploadImageChange (e) {
      this.openImageViewer(e.target.files[0]);
      this.$refs.restFile.value = null;
    },
 
    // 选择文件回调事件
    uploadFileChange (e) {
      let maxsize = 100 * 1024 * 1024;
      if (e.target.files.length == 0) {
        return false;
      }
 
      let file = e.target.files[0];
      if (/\.(gif|jpg|jpeg|png|webp|GIF|JPG|PNG|WEBP)$/.test(file.name)) {
        this.openImageViewer(file);
        return;
      }
 
      if (file.size > maxsize) {
        this.$notify.info({
          title: "消息",
          message: "上传文件不能大于100M",
        });
        return;
      }
 
      this.filesManager.isShow = true;
      this.$refs.restFile2.value = null;
      this.$refs.filesManager.upload(file);
    },
 
    // 打开图片查看器
    openImageViewer (file) {
      this.imageViewer.isShow = true;
      this.imageViewer.file = file;
    },
 
    // 代码块编辑器确认完成回调事件
    confirmCodeBlock (data) {
      const { talk_type, receiver_id } = this.$store.state.dialogue;
      ServeSendTalkCodeBlock({
        talk_type,
        receiver_id,
        code: data.code,
        lang: data.language,
      }).then((res) => {
        if (res.code == 200) {
          this.codeBlock.isShow = false;
        } else {
          this.$notify({
            title: "友情提示",
            message: res.message,
            type: "warning",
          });
        }
      });
    },
 
    // 确认上传图片消息回调事件
    confirmUploadImage () {
      let fileData = new FormData();
      fileData.append("file", this.imageViewer.file);
 
      let ref = this.$refs.imageViewer;
 
      ServeSendTalkImage(fileData)
        .then((res) => {
          ref.loading = false;
          if (res.code == 200) {
            ref.closeBox();
          } else {
            this.$notify({
              title: "友情提示",
              message: res.message,
              type: "warning",
            });
          }
        })
        .finally(() => {
          ref.loading = false;
        });
    },
 
    // 选中表情包回调事件
    selecteEmoticon (data) {
      if (data.type == 1) {
        let value = this.editorText;
        let el = this.$refs.textarea;
        let startPos = el.selectionStart;
        let endPos = el.selectionEnd;
        let newValue =
          value.substring(0, startPos) +
          data.value +
          value.substring(endPos, value.length);
 
        this.editorText = newValue;
 
        if (el.setSelectionRange) {
          setTimeout(() => {
            let index = startPos + data.value.length;
            el.setSelectionRange(index, index);
            el.focus();
          }, 0);
        }
      } else {
        const { talk_type, receiver_id } = this.$store.state.dialogue;
        ServeSendEmoticon({
          talk_type,
          receiver_id,
          emoticon_id: data.value,
        });
      }
 
      this.$refs.popoverEmoticon.doClose();
    },
  },
};
</script>
<style scoped lang="less">
.editor-container {
  height: 160px;
  width: 100%;
  background-color: white;
}
 
.editor-container .toolbar {
  line-height: 35px;
  border-bottom: 1px solid #f5f0f0;
  border-top: 1px solid #f5f0f0;
}
 
.editor-container .toolbar li {
  list-style: none;
  float: left;
  width: 35px;
  margin-left: 3px;
  cursor: pointer;
  text-align: center;
  line-height: 35px;
  position: relative;
  color: #8d8d8d;
}
 
.editor-container .toolbar li .tip-title {
  display: none;
  position: absolute;
  top: 38px;
  left: 0px;
  height: 26px;
  line-height: 26px;
  background-color: rgba(31, 35, 41, 0.9);
  color: white;
  min-width: 30px;
  font-size: 10px;
  padding: 0 5px;
  border-radius: 2px;
  white-space: pre;
  text-align: center;
  user-select: none;
  z-index: 1;
}
 
.editor-container .toolbar li:hover .tip-title {
  display: block;
}
 
.editor-container .toolbar li:hover {
  background-color: #f7f5f5;
}
 
.editor-container .toolbar .text-tips {
  float: right;
  margin-right: 15px;
  font-size: 12px;
  color: #ccc;
}
 
.editor-container .toolbar .text-tips i {
  font-size: 14px;
  cursor: pointer;
  margin-left: 5px;
  color: rgb(255, 181, 111);
}
 
.editor-container .textarea {
  overflow: hidden;
  position: relative;
}
 
textarea {
  width: calc(100% - 10px);
  width: -moz-calc(100% - 10px);
  width: -webkit-calc(100% - 10px);
  height: calc(100% - 10px);
  height: -moz-calc(100% - 10px);
  height: -webkit-calc(100% - 10px);
  border: 0 none;
  outline: none;
  resize: none;
  font-size: 15px;
  overflow-y: auto;
  color: #464545;
  padding: 5px;
  position: relative;
}
 
textarea::-webkit-scrollbar {
  width: 4px;
  height: 1px;
}
 
textarea::-webkit-scrollbar-thumb {
  background: #d5cfcf;
}
 
textarea::-webkit-scrollbar-track {
  background: #ededed;
}
 
textarea::-webkit-input-placeholder {
  color: #dccdcd;
  font-size: 12px;
  font-weight: 400;
}
 
/* 编辑器文档说明 --- start */
.editor-books .books-title {
  font-size: 16px;
  height: 30px;
  line-height: 22px;
  margin-top: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid #cbcbcb;
  color: #726f6f;
  font-weight: 400;
  margin-left: 11px;
}
 
.editor-books p {
  text-indent: 10px;
  font-size: 12px;
  height: 30px;
  line-height: 30px;
  color: #7f7c7c;
}
 
/* 编辑器文档说明 --- end */
</style>