|
@@ -2,9 +2,10 @@
|
|
|
<div>
|
|
|
<div class="chat-wrap" v-if="selected">
|
|
|
<div class="chat-title">
|
|
|
- {{ selected.name || selected.roomname }}
|
|
|
+ {{ selected.username || selected.name }}
|
|
|
</div>
|
|
|
<div class="record-warp">
|
|
|
+ <!-- 私聊 -->
|
|
|
<el-scrollbar
|
|
|
v-if="tab === 'friends'"
|
|
|
style="height: 95%"
|
|
@@ -16,7 +17,7 @@
|
|
|
:key="i"
|
|
|
>
|
|
|
<!-- receive msg record -->
|
|
|
- <div class="record-item" v-if="item.id !== self.id">
|
|
|
+ <div class="record-item" v-if="item.toid === socket.id">
|
|
|
<div class="avatar">
|
|
|
<el-avatar
|
|
|
shape="square"
|
|
@@ -30,6 +31,7 @@
|
|
|
v-if="_isUrl(item.msg)"
|
|
|
:href="item.msg"
|
|
|
target="_blank"
|
|
|
+ style="margin-left: 10px; margin-top: 15px;"
|
|
|
>
|
|
|
{{ item.msg }}
|
|
|
</a>
|
|
@@ -38,6 +40,7 @@
|
|
|
class="message-content-text receive-record-item"
|
|
|
v-if="item.type === 'text'"
|
|
|
v-html="_parseHtml(item.msg)"
|
|
|
+ style="margin-left: 10px; margin-top: 15px;"
|
|
|
></div>
|
|
|
<!-- 图片 -->
|
|
|
<div
|
|
@@ -68,7 +71,7 @@
|
|
|
</div>
|
|
|
|
|
|
<!-- self send msg record -->
|
|
|
- <div v-if="item.id === self.id" class="record-item send-record">
|
|
|
+ <div v-if="item.toid !== socket.id" class="record-item send-record">
|
|
|
<!-- 链接 -->
|
|
|
<a
|
|
|
class="message-content-text"
|
|
@@ -128,6 +131,8 @@
|
|
|
</div>
|
|
|
</div>
|
|
|
</el-scrollbar>
|
|
|
+
|
|
|
+ <!-- 群聊 -->
|
|
|
<el-scrollbar
|
|
|
v-if="tab === 'groups'"
|
|
|
style="height: 95%"
|
|
@@ -139,7 +144,7 @@
|
|
|
:key="i"
|
|
|
>
|
|
|
<!-- receive msg record -->
|
|
|
- <div class="record-item" v-if="item.id !== self.id">
|
|
|
+ <div class="record-item" v-if="item.id !== socket.id">
|
|
|
<div class="avatar">
|
|
|
<el-avatar
|
|
|
shape="square"
|
|
@@ -147,8 +152,8 @@
|
|
|
></el-avatar>
|
|
|
</div>
|
|
|
<div class="group-record-item">
|
|
|
- <div class="nickname">
|
|
|
- {{ item.nickname }}
|
|
|
+ <div class="username">
|
|
|
+ {{ item.username }}
|
|
|
</div>
|
|
|
<!-- 链接 -->
|
|
|
<a
|
|
@@ -204,7 +209,7 @@
|
|
|
</div>
|
|
|
|
|
|
<!-- self send msg record -->
|
|
|
- <div v-if="item.id === self.id" class="record-item send-record">
|
|
|
+ <div v-if="item.id === socket.id" class="record-item send-record">
|
|
|
<!-- 链接 -->
|
|
|
<a
|
|
|
class="message-content-text"
|
|
@@ -322,10 +327,11 @@
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
-import { addCookie, getCookie } from "@/utils/setCookie.js";
|
|
|
-import { isUrl, parseHtml, processReturn } from "@/utils/common";
|
|
|
+import { isUrl, parseHtml } from "@/utils/common";
|
|
|
import Emoji from "./Emoji.vue";
|
|
|
+import { mapState, mapActions } from "vuex";
|
|
|
let emojiData = require('./../assets/emoji/emoji.json')
|
|
|
+import request from '@/api/require'
|
|
|
export default {
|
|
|
name: "ChatFrame",
|
|
|
props: ["selected", "tab"],
|
|
@@ -339,29 +345,58 @@ export default {
|
|
|
inputRange: '', // 光标
|
|
|
emojiPath: new Map(), // emoji表情地址map对象,
|
|
|
msg: "",
|
|
|
- friend_record: [],
|
|
|
friend_record_list: [],
|
|
|
- room_record: {},
|
|
|
room_record_list: [],
|
|
|
- self: null,
|
|
|
messageType: "",
|
|
|
uploadProgress: false,
|
|
|
};
|
|
|
},
|
|
|
- computed: {},
|
|
|
+ computed: {
|
|
|
+ ...mapState({
|
|
|
+ self: state => state.app.user
|
|
|
+ }),
|
|
|
+ ...mapState({
|
|
|
+ socket: state => state.chat.socket
|
|
|
+ }),
|
|
|
+ ...mapState({
|
|
|
+ friendRecord: state => state.chat.friendRecord
|
|
|
+ }),
|
|
|
+ ...mapState({
|
|
|
+ roomRecord: state => state.chat.roomRecord
|
|
|
+ }),
|
|
|
+ },
|
|
|
watch: {
|
|
|
- selected(n, o) {
|
|
|
- if (this.friend_record[this.selected.id]) {
|
|
|
- this.friend_record_list = this.friend_record[this.selected.id];
|
|
|
- }
|
|
|
- if (this.room_record[this.selected.id]) {
|
|
|
- this.room_record_list = this.room_record[this.selected.id];
|
|
|
+ selected(newVal, o) {
|
|
|
+ if (this.tab === 'friends') {
|
|
|
+ console.log('================================================')
|
|
|
+ console.log(`this.friendRecord`, this.friendRecord)
|
|
|
+ console.log(`newVal`, newVal)
|
|
|
+ console.log(`this.friendRecord[newVal.id]`, this.friendRecord[newVal.id])
|
|
|
+ console.log(`this.friendRecord`, this.friendRecord)
|
|
|
+
|
|
|
+ if (this.friendRecord[newVal.id]) {
|
|
|
+ this.friend_record_list = this.friendRecord[newVal.id];
|
|
|
+ } else {
|
|
|
+ this.$store.commit('chat/set_friend_record', [newVal.id])
|
|
|
+ this.friend_record_list = this.friendRecord[newVal.id];
|
|
|
+ console.log(`this.friendRecord`, this.friendRecord)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (this.roomRecord[newVal.id]) {
|
|
|
+ this.room_record_list = this.roomRecord[newVal.id];
|
|
|
+ } else {
|
|
|
+ this.$store.commit('chat/set_room_record', newVal.id)
|
|
|
+ this.room_record_list = this.roomRecord[newVal.id];
|
|
|
+ console.log(`this.room_record_list[newVal.id]`, this.room_record_list)
|
|
|
+ }
|
|
|
}
|
|
|
this.$nextTick(() => {
|
|
|
+ document.getElementById('charInput').innerText = ''
|
|
|
+ this.msg = ''
|
|
|
this.$refs["myScrollbar"].wrap.scrollTop =
|
|
|
this.$refs["myScrollbar"].wrap.scrollHeight;
|
|
|
});
|
|
|
- },
|
|
|
+ }
|
|
|
},
|
|
|
methods: {
|
|
|
handleSuccess(res, file) {
|
|
@@ -369,8 +404,6 @@ export default {
|
|
|
this.imageUrl = URL.createObjectURL(file.raw);
|
|
|
},
|
|
|
beforeUpload(file) {
|
|
|
- console.log(`file`, file);
|
|
|
- console.log(`file.type`, file.type);
|
|
|
this.uploadProgress = true;
|
|
|
this.fileData = file;
|
|
|
// 调用函数分割文件 我这里是分割成不超过20M的文件快
|
|
@@ -381,7 +414,14 @@ export default {
|
|
|
// })
|
|
|
let fd = new FormData();
|
|
|
fd.append("filename", file);
|
|
|
- this.$http.post("/api/upload/", fd).then(
|
|
|
+ // upload_file(fd).then(
|
|
|
+ request.upload_file(file).then(res => {
|
|
|
+ console.log(`res5555`, res)
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ console.log(`err`, err)
|
|
|
+ })
|
|
|
+ this.$http.post(this.baseUrl + "api/upload/", fd).then(
|
|
|
(res) => {
|
|
|
if (
|
|
|
file.type === "image/jpeg" ||
|
|
@@ -418,10 +458,8 @@ export default {
|
|
|
* @params text
|
|
|
*/
|
|
|
_parseHtml(text) {
|
|
|
- console.log(`text----fffff`, text)
|
|
|
const regex2 = /!!\[(.+?)\]!!/g;
|
|
|
const codeList = text.match(regex2)
|
|
|
- console.log('text.match(regex2)',text.match(regex2))
|
|
|
let html = text
|
|
|
if (codeList) {
|
|
|
codeList.map((item) => {
|
|
@@ -433,12 +471,6 @@ export default {
|
|
|
"' alt='' >")
|
|
|
})
|
|
|
}
|
|
|
- // let html =
|
|
|
- // "<img style='width: 20px; height: 20px;vertical-align: sub;' src='" +
|
|
|
- // this.getIconPic(code) +
|
|
|
- // "' unicode = '" +
|
|
|
- // code +
|
|
|
- // "' alt='' >";
|
|
|
return parseHtml(html);
|
|
|
},
|
|
|
|
|
@@ -665,64 +697,38 @@ export default {
|
|
|
} else { // 输入框信息
|
|
|
this.msg = this.formatInputCon().replace(/<br>/g, '\r\n')
|
|
|
console.log(`this.msg`, this.msg)
|
|
|
- console.log(this.msg)
|
|
|
}
|
|
|
if (this.msg.trim() === "") {
|
|
|
this.msg = "";
|
|
|
return;
|
|
|
}
|
|
|
- if (!this.self) {
|
|
|
- this.self = JSON.parse(localStorage.getItem("self"));
|
|
|
- }
|
|
|
- console.log(`this.self`, this.self);
|
|
|
if (!this.messageType) {
|
|
|
this.messageType = "text";
|
|
|
}
|
|
|
const msgInfo = {
|
|
|
...this.self,
|
|
|
+ id: this.socket.id,
|
|
|
msg: this.msg,
|
|
|
type: this.messageType,
|
|
|
};
|
|
|
- console.log(`msgInfo`, msgInfo);
|
|
|
- console.log(`this.selected.id`, this.selected);
|
|
|
- // if (!this.selected) {
|
|
|
- // this.$message.error("请先选择聊天对象");
|
|
|
- // return;
|
|
|
- // }
|
|
|
- // 输入enter后置空
|
|
|
- console.log(`document.getElementById('charInput')`, document.getElementById('charInput'))
|
|
|
- document.getElementById('charInput').innerText = ''
|
|
|
- console.log(`document.getElementById('charInput')`, document.getElementById('charInput'))
|
|
|
- this.msg = "";
|
|
|
- this.messageType = "";
|
|
|
- console.log('===========this.tab=========')
|
|
|
- console.log(this.tab)
|
|
|
if (this.tab === "friends") {
|
|
|
msgInfo.toid = this.selected.id;
|
|
|
- this.$socket.emit("private message", this.selected.id, msgInfo);
|
|
|
- if (this.friend_record[this.selected.id]) {
|
|
|
- this.friend_record[this.selected.id].push(msgInfo);
|
|
|
- this.friend_record_list = this.friend_record[this.selected.id];
|
|
|
- } else {
|
|
|
- this.friend_record[this.selected.id] = [];
|
|
|
- this.friend_record[this.selected.id].push(msgInfo);
|
|
|
- this.friend_record_list = this.friend_record[this.selected.id];
|
|
|
- }
|
|
|
- // addCookie("friend_record", JSON.stringify(this.friend_record));
|
|
|
+ this.socket.emit("private message", this.selected.id, msgInfo);
|
|
|
+ const msgData = [this.selected.id, msgInfo]
|
|
|
+ this.$store.commit('chat/set_friend_record', msgData)
|
|
|
+ this.friend_record_list = this.friendRecord[this.selected.id];
|
|
|
} else {
|
|
|
- msgInfo.roomid = this.selected.id;
|
|
|
- this.$socket.emit("chat-room", this.selected.id, msgInfo);
|
|
|
- if (this.room_record[this.selected.id]) {
|
|
|
- this.room_record[this.selected.id].push(msgInfo);
|
|
|
- this.room_record_list = this.room_record[this.selected.id];
|
|
|
- } else {
|
|
|
- this.room_record[this.selected.id] = [];
|
|
|
- this.room_record[this.selected.id].push(msgInfo);
|
|
|
- this.room_record_list = this.room_record[this.selected.id];
|
|
|
- }
|
|
|
- // addCookie("room_record", JSON.stringify(this.room_record));
|
|
|
+ console.log('=======ROOM CHAT========')
|
|
|
+ msgInfo.roomId = this.selected.id;
|
|
|
+ this.socket.emit("chat-room", this.selected.id, msgInfo);
|
|
|
+ this.$store.commit('chat/set_room_record', msgInfo)
|
|
|
+ this.room_record_list = this.roomRecord[this.selected.id];
|
|
|
}
|
|
|
this.$nextTick(() => {
|
|
|
+ // 输入enter后置空
|
|
|
+ document.getElementById('charInput').innerText = ''
|
|
|
+ this.msg = "";
|
|
|
+ this.messageType = "";
|
|
|
// 滚动到底部
|
|
|
this.$refs["myScrollbar"].wrap.scrollTop =
|
|
|
this.$refs["myScrollbar"].wrap.scrollHeight;
|
|
@@ -735,92 +741,18 @@ export default {
|
|
|
},
|
|
|
},
|
|
|
created() {
|
|
|
- const self_str = localStorage.getItem("self");
|
|
|
- const room_record_str = getCookie("room_record");
|
|
|
- const friend_record_str = getCookie("friend_record");
|
|
|
- if (self_str) {
|
|
|
- this.self = JSON.parse(self_str);
|
|
|
- }
|
|
|
- if (room_record_str) {
|
|
|
- this.room_record = JSON.parse(room_record_str);
|
|
|
- }
|
|
|
- if (friend_record_str) {
|
|
|
- this.friend_record = JSON.parse(friend_record_str);
|
|
|
- }
|
|
|
+ // const room_record_str = getCookie("room_record");
|
|
|
+ // const friend_record_str = getCookie("friend_record");
|
|
|
+ // if (room_record_str) {
|
|
|
+ // this.room_record = JSON.parse(room_record_str);
|
|
|
+ // }
|
|
|
+ // if (friend_record_str) {
|
|
|
+ // this.friend_record = JSON.parse(friend_record_str);
|
|
|
+ // }
|
|
|
},
|
|
|
mounted () {
|
|
|
this.initEmojiPic()
|
|
|
},
|
|
|
- sockets: {
|
|
|
- connect() {
|
|
|
- console.log("===连接成功=========");
|
|
|
- },
|
|
|
- // 发送消息
|
|
|
- toServer(data) {
|
|
|
- this.$socket.emit("toServer", data);
|
|
|
- },
|
|
|
- // 接收消息
|
|
|
- toClient(data) {
|
|
|
- console.log(`toclient data`, data);
|
|
|
- },
|
|
|
- // 断开链接回调
|
|
|
- disconnect() {
|
|
|
- console.log("disconnect:", "已经断开 socket 链接");
|
|
|
- },
|
|
|
- // 重新连接
|
|
|
- reconnect() {
|
|
|
- // 自动执行,直到链接成功
|
|
|
- this.$socket.emit("connect");
|
|
|
- },
|
|
|
- receiveMsg(data) {
|
|
|
- console.log(`receiveMsg`, data);
|
|
|
- },
|
|
|
- chat(data) {
|
|
|
- console.log(`private message=================`, data);
|
|
|
- if (this.friend_record[data[0]]) {
|
|
|
- this.friend_record[data[0]].push(data[1]);
|
|
|
- } else {
|
|
|
- this.friend_record[data[0]] = [];
|
|
|
- this.friend_record[data[0]].push(data[1]);
|
|
|
- }
|
|
|
- if (this.friend_record[this.selected.id]) {
|
|
|
- this.friend_record_list = this.friend_record[this.selected.id];
|
|
|
- this.$nextTick(() => {
|
|
|
- this.$refs["myScrollbar"].wrap.scrollTop =
|
|
|
- this.$refs["myScrollbar"].wrap.scrollHeight;
|
|
|
- });
|
|
|
- }
|
|
|
- console.log(`this.friend_record`, this.friend_record)
|
|
|
- console.log(`this.selected.id`, this.selected.id)
|
|
|
- console.log(`this.friend_record_list`, this.friend_record_list)
|
|
|
- // addCookie("friend_record", JSON.stringify(this.friend_record));
|
|
|
- },
|
|
|
- room_msg(data) {
|
|
|
- console.log("======room msg===");
|
|
|
- console.log(`data`, data);
|
|
|
- if (this.room_record[data.roomid]) {
|
|
|
- this.room_record[data.roomid].push(data);
|
|
|
- } else {
|
|
|
- this.room_record[data.roomid] = [];
|
|
|
- this.room_record[data.roomid].push(data);
|
|
|
- }
|
|
|
- if (this.room_record[this.selected.id]) {
|
|
|
- this.room_record_list = this.room_record[this.selected.id];
|
|
|
- this.$nextTick(() => {
|
|
|
- this.$refs["myScrollbar"].wrap.scrollTop =
|
|
|
- this.$refs["myScrollbar"].wrap.scrollHeight;
|
|
|
- });
|
|
|
- }
|
|
|
- // addCookie("room_record", JSON.stringify(this.room_record));
|
|
|
- },
|
|
|
- user_list(userlist) {
|
|
|
- console.log(`this.self`, this.self);
|
|
|
- console.log(`userlist`, userlist);
|
|
|
- },
|
|
|
- quit(data) {
|
|
|
- console.log(`quit == quit`, data);
|
|
|
- },
|
|
|
- },
|
|
|
};
|
|
|
</script>
|
|
|
|
|
@@ -853,7 +785,7 @@ export default {
|
|
|
padding: 8px 12px;
|
|
|
border-radius: 5px;
|
|
|
}
|
|
|
-.nickname {
|
|
|
+.username {
|
|
|
padding: 4px;
|
|
|
margin-bottom: 2px;
|
|
|
font-size: 14px;
|
|
@@ -898,8 +830,7 @@ export default {
|
|
|
max-height: 225px;
|
|
|
}
|
|
|
.receive-record-item {
|
|
|
- margin-left: 10px;
|
|
|
- margin-top: 15px;
|
|
|
+ margin: 2px;
|
|
|
background: #fff
|
|
|
}
|
|
|
.input-box {
|