forked from moohng/wchat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
155 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,77 @@ | ||
<template> | ||
<view-box class="chat"> | ||
<x-header slot="header"> | ||
<span slot="default">{{ title }}</span> | ||
</x-header> | ||
<ul slot="default" v-scroll="messages"> | ||
<chat-dialog v-for="(message, index) in messages" | ||
:date="date(message, index)", :message="message"></chat-dialog> | ||
</ul> | ||
<chat-bar slot="bottom"></chat-bar> | ||
</view-box> | ||
<view-box class="chat"> | ||
<x-header slot="header"> | ||
<span slot="default">{{ session.name }}</span> | ||
</x-header> | ||
<ul slot="default" v-scroll> | ||
<chat-dialog v-for="message in session.messages" | ||
:message="message"></chat-dialog> | ||
</ul> | ||
<chat-bar slot="bottom" @send="send"></chat-bar> | ||
</view-box> | ||
</template> | ||
|
||
<script> | ||
import { ViewBox, XHeader } from 'vux' | ||
import ChatDialog from './chat-dialog' | ||
import ChatBar from './chat-bar' | ||
import { mapState } from 'vuex' | ||
export default { | ||
computed: { | ||
...mapState (['sessionList']), | ||
title () { | ||
return this.$route.query.title | ||
}, | ||
index () { | ||
// 遍历会话列表,看会话是否存在 | ||
let _index = false | ||
this.sessionList.forEach((session, index) => { | ||
if (session.title === this.title) { | ||
// 存在 记录索引 | ||
_index = index | ||
return | ||
} | ||
}) | ||
return _index | ||
}, | ||
messages () { | ||
if (this.index !== false) { | ||
return this.sessionList[this.index].messages | ||
} | ||
else { | ||
return [] | ||
} | ||
} | ||
}, | ||
methods: { | ||
date (message, index) { | ||
if (index >= 1) { | ||
// 判断是否显示时间 先将就着用吧 | ||
const time = message.time.substr(3, 2) | ||
const lastTime = this.messages[index - 1].time.substr(3, 2) | ||
return time - lastTime > 0 | ||
} | ||
return false | ||
computed: { | ||
...mapState (['sessionList']), | ||
session () { | ||
const { index, username } = this.$route.query | ||
if (username) { | ||
return { | ||
name: username, | ||
members: [username], | ||
messages: [] | ||
} | ||
}, | ||
directives: { | ||
// 自动滚动 | ||
scroll: { | ||
bind (el) { | ||
setTimeout(() => { | ||
el.parentNode.scrollTop = el.scrollHeight | ||
}, 1) | ||
}, | ||
update (el) { | ||
setTimeout(() => { | ||
el.parentNode.scrollTop = el.scrollHeight | ||
}, 1) | ||
} | ||
} | ||
return this.sessionList[index] | ||
} | ||
}, | ||
methods: { | ||
send (text) { | ||
// 封装消息 | ||
const { id, name, members } = this.session | ||
const username = sessionStorage.getItem('username') | ||
const data = { | ||
session: { id, name, members }, | ||
message: { | ||
from: { username }, | ||
content: { text }, | ||
date: Date.now() | ||
} | ||
}, | ||
components: { | ||
ViewBox, | ||
XHeader, | ||
ChatDialog, | ||
ChatBar | ||
} | ||
// 发送消息 | ||
this.$send(JSON.stringify(data)) | ||
// 保存消息 | ||
this.$store.commit('addMessage', data) | ||
} | ||
}, | ||
directives: { | ||
// 自动滚动 | ||
scroll: { | ||
bind (el) { | ||
setTimeout(() => { | ||
el.parentNode.scrollTop = el.scrollHeight | ||
}, 1) | ||
}, | ||
update (el) { | ||
setTimeout(() => { | ||
el.parentNode.scrollTop = el.scrollHeight | ||
}, 1) | ||
} | ||
} | ||
}, | ||
components: { | ||
ViewBox, | ||
XHeader, | ||
ChatDialog, | ||
ChatBar | ||
} | ||
} | ||
</script> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.