dcc
2024-06-13 3616db170333df7d668c97323344335b52c4153c
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
<template>
  <div class="main-right">
    <div class="huihua">
      <!-- 头部 -->
      <div class="hui-top">{{ t("message.home.dibu18") }}</div>
      <!-- 聊天内容 -->
      <div class="hui-center" ref="scroll">
        <div class="hui-li" v-for="(item, index) in msgList" :key="index">
          <div class="list-tiem">{{ item.createtime }}</div>
          <p
            class="hui-n"
            :class="[
              item.send_receive === 'receive' ? 'bg-ta' : '',
              item.send_receive === 'receive' ? 'left-is-ta' : 'right-is-ta',
            ]"
            v-if="item.type === 'text'"
          >
            {{ item.content }}
          </p>
          <img
            :class="[
              item.send_receive === 'receive' ? 'bg-ta' : '',
              item.send_receive === 'receive' ? 'left-is-ta' : 'right-is-ta',
            ]"
            class="content-img"
            v-else
            :src="item.content"
          />
        </div>
      </div>
      <!-- 发送聊天信息 -->
      <div class="hui-bottom">
        <img
          src="@/assets/images/c2c/orderSuccess/Group1862.png"
          alt=""
          @click="handleSendImg"
        />
        <!-- 图片 -->
        <input
          ref="uploadRef"
          type="file"
          hidden
          @change="onImageFileChanged"
          accept="image/png, image/gif, image/jpeg, image/bmp, image/x-icon"
        />
        <el-input v-model="message" :placeholder="t('message.c2c.tip152')" />
        <img
          @click="handleSend('text', message)"
          src="@/assets/images/c2c/orderSuccess/Group1859.png"
          alt=""
        />
      </div>
    </div>
  </div>
</template>
 
<script setup>
import Axios from "@/api/c2c.js";
import { _uploadImage } from "@/api/chat.js";
import { useI18n } from "vue-i18n";
import { onMounted,onUnmounted } from "vue";
 
const { t } = useI18n();
 
const msgList = ref([]);
const lastMsgId = ref();
const uploadRef = ref(null);
const message = ref();
const props = defineProps({
  detailInfo: {
    type: Object,
    default: {},
  },
});
 
onMounted(() => {
  fetchChatList();
});
 
const fetchChatList = (message_id = "") => {
  let obj = {
    messageId: message_id,
    orderNo: props.detailInfo?.orderNo || props.detailInfo?.order_no,
  };
  // 获取消息列表
  Axios.getOtcOnlinechat(obj).then((res) => {
    let data = res.data;
    scrollToBottom();
    if (!lastMsgId.value) {
      lastMsgId.value = data.length && data[data.length - 1]["id"];
    }
    if (data.length) {
      if (message_id) {
        // 加载更多
        lastMsgId.value = data[data.length - 1]["id"];
        msgList.value = [...data.reverse(), ...msgList.value];
      } else {
        msgList.value = [...msgList.value, ...data.reverse()];
        let hash = {};
        msgList.value = msgList.value.reduce(function (preVal, curVal) {
          hash[curVal.id]
            ? " "
            : (hash[curVal.id] = true && preVal.push(curVal));
          return preVal;
        }, []);
      }
    }
  });
};
 
const timeOutTimer1 = ref(null);
const scrollToBottom = () => {
  const doms = document.getElementsByClassName("hui-center")[0];
  timeOutTimer1.value = setTimeout(() => {
    // this.$nextTick(() => {
    doms.scrollTop = doms.scrollHeight;
    // });
  }, 1000);
};
// 选中图片
const onImageFileChanged = (e) => {
  const file = e.target.files[0];
  afterRead(file);
};
// 文件上传
const afterRead = (file) => {
  _uploadImage(file).then((data) => {
    handleSend("img", data.httpUrl);
  });
};
 
const handleSendImg = () => {
  uploadRef.value.click();
};
const handleSend = (type = "text", content = "") => {
  // 发送消息, img 也当消息text
  if (!content) {
    this.$message.error(t("message.c2c.tip142"));
    return;
  }
  Axios.sendOtcOnlinechat({
    orderNo: props.detailInfo.orderNo,
    type,
    content,
  }).then((data) => {
    message.value = "";
    scrollToBottom();
    fetchChatList();
  });
};
 
 // 清除定时器
onUnmounted(()=>{
  if(timeOutTimer1.value){
    clearTimeout(timeOutTimer1.value)
    timeOutTimer1.value = null
  }
})
</script>