聊天室自动管理用户信息

This commit is contained in:
satori 2022-02-07 01:46:11 +08:00
parent c1a7eb4b7c
commit 31fdf5cc28
2 changed files with 23 additions and 6 deletions

View File

@ -2,9 +2,9 @@
.dialogue-list .dialogue-list
.post(v-for="item in data", :key="item._id") .post(v-for="item in data", :key="item._id")
.left .left
.avatar img.avatar(:src="item.user.avatar")
.right .right
.name Last .name {{ item.user.name }}
.message {{ item.data }} .message {{ item.data }}
</template> </template>

View File

@ -17,7 +17,6 @@
button.submit(@click="submit") 发表 button.submit(@click="submit") 发表
</template> </template>
<style lang="sass"> <style lang="sass">
.chat-index .chat-index
textarea.dialogue textarea.dialogue
@ -36,7 +35,6 @@
flex: 1 flex: 1
</style> </style>
<script> <script>
import socket from "@/assets/js/socket.js"; import socket from "@/assets/js/socket.js";
@ -44,14 +42,17 @@ export default {
asyncData({ $axios }) { asyncData({ $axios }) {
return $axios("/api/chat").then((res) => { return $axios("/api/chat").then((res) => {
return { return {
userstore: new Map(),
chatlist: res.data, chatlist: res.data,
chat: { data: "" }, chat: { data: "" },
chatactive: [{ _id: "2333", data: "cacd" }], chatactive: [],
}; };
}); });
}, },
mounted() { mounted() {
socket.controller.set("chat", (data) => { socket.controller.set("chat", async (data) => {
// user
data.user = await this.loaduser(data.uid);
this.chatactive.push(data); this.chatactive.push(data);
console.log(data); console.log(data);
}); });
@ -59,6 +60,22 @@ export default {
this.keyCodeForEvent(); this.keyCodeForEvent();
}, },
methods: { methods: {
async loaduser(uid) {
if (!uid || uid === "0") {
return { _id: "0", name: "游客", avatar: "" };
}
let user = this.userstore.get(uid);
if (!user) {
return await this.$axios.get("/api/user/" + uid).then((res) => {
if (res.status === 200) {
this.userstore.set(uid, res.data);
return res.data;
}
return { _id: "0", name: "游客", avatar: "" };
});
}
return user;
},
create() { create() {
let data = { name: "FM DEMO", data: "23333" }; let data = { name: "FM DEMO", data: "23333" };
this.$axios.post("/api/chat", data).then((res) => { this.$axios.post("/api/chat", data).then((res) => {