|
|
|
@ -18,7 +18,7 @@
|
|
|
|
|
<template v-for="message in messages">
|
|
|
|
|
<text-message :key="message.id" v-if="message.type == 0"
|
|
|
|
|
:avatarUrl="message.avatarUrl" :time="message.time" :authorName="message.authorName"
|
|
|
|
|
:authorType="message.authorType" :content="message.content"
|
|
|
|
|
:authorType="message.authorType" :content="message.content" :repeated="message.repeated"
|
|
|
|
|
></text-message>
|
|
|
|
|
<legacy-paid-message :key="message.id" v-else-if="message.type == 1"
|
|
|
|
|
:avatarUrl="message.avatarUrl" :title="message.title" :content="message.content"
|
|
|
|
@ -51,11 +51,14 @@ export default {
|
|
|
|
|
PaidMessage
|
|
|
|
|
},
|
|
|
|
|
data() {
|
|
|
|
|
let cfg = {...config.DEFAULT_CONFIG}
|
|
|
|
|
cfg.blockKeywords = cfg.blockKeywords.split('\n').filter(val => val)
|
|
|
|
|
cfg.blockUsers = cfg.blockUsers.split('\n').filter(val => val)
|
|
|
|
|
let styleElement = document.createElement('style')
|
|
|
|
|
styleElement.innerText = config.DEFAULT_CONFIG.css
|
|
|
|
|
styleElement.innerText = cfg.css
|
|
|
|
|
document.head.appendChild(styleElement)
|
|
|
|
|
return {
|
|
|
|
|
config: config.DEFAULT_CONFIG,
|
|
|
|
|
config: cfg,
|
|
|
|
|
styleElement,
|
|
|
|
|
websocket: null,
|
|
|
|
|
messages: [],
|
|
|
|
@ -66,18 +69,46 @@ export default {
|
|
|
|
|
// 开发时使用localhost:80
|
|
|
|
|
const url = process.env.NODE_ENV === 'development' ? 'ws://localhost/chat' : `ws://${window.location.host}/chat`
|
|
|
|
|
this.websocket = new WebSocket(url)
|
|
|
|
|
this.websocket.onopen = () => this.websocket.send(JSON.stringify({
|
|
|
|
|
cmd: COMMAND_JOIN_ROOM,
|
|
|
|
|
data: {
|
|
|
|
|
roomId: parseInt(this.$route.params.roomId)
|
|
|
|
|
this.websocket.onopen = this.onWsOpen.bind(this)
|
|
|
|
|
this.websocket.onmessage = this.onWsMessage.bind(this)
|
|
|
|
|
|
|
|
|
|
if (this.$route.query.config_id) {
|
|
|
|
|
try {
|
|
|
|
|
let cfg = await config.getRemoteConfig(this.$route.query.config_id)
|
|
|
|
|
cfg.blockKeywords = cfg.blockKeywords.split('\n').filter(val => val)
|
|
|
|
|
cfg.blockUsers = cfg.blockUsers.split('\n').filter(val => val)
|
|
|
|
|
this.styleElement.innerText = cfg.css
|
|
|
|
|
this.config = cfg
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.$message.error('获取配置失败:' + e)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
this.websocket.onmessage = (event) => {
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
beforeDestroy() {
|
|
|
|
|
document.head.removeChild(this.styleElement)
|
|
|
|
|
this.websocket.close()
|
|
|
|
|
},
|
|
|
|
|
updated() {
|
|
|
|
|
window.scrollTo(0, document.body.scrollHeight)
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
onWsOpen() {
|
|
|
|
|
this.websocket.send(JSON.stringify({
|
|
|
|
|
cmd: COMMAND_JOIN_ROOM,
|
|
|
|
|
data: {
|
|
|
|
|
roomId: parseInt(this.$route.params.roomId)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
},
|
|
|
|
|
onWsMessage(event) {
|
|
|
|
|
let body = JSON.parse(event.data)
|
|
|
|
|
let message = null
|
|
|
|
|
let time, price
|
|
|
|
|
switch(body.cmd) {
|
|
|
|
|
case COMMAND_ADD_TEXT:
|
|
|
|
|
if (!this.filterTextMessage(body.data) || this.mergeSimilar(body.data.content)) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
time = new Date(body.data.timestamp * 1000)
|
|
|
|
|
message = {
|
|
|
|
|
id: this.nextId++,
|
|
|
|
@ -86,7 +117,8 @@ export default {
|
|
|
|
|
time: `${time.getHours()}:${time.getMinutes()}`,
|
|
|
|
|
authorName: body.data.authorName,
|
|
|
|
|
authorType: body.data.authorType,
|
|
|
|
|
content: body.data.content
|
|
|
|
|
content: body.data.content,
|
|
|
|
|
repeated: 1
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
case COMMAND_ADD_GIFT:
|
|
|
|
@ -114,26 +146,49 @@ export default {
|
|
|
|
|
}
|
|
|
|
|
if (message) {
|
|
|
|
|
this.messages.push(message)
|
|
|
|
|
if (this.messages.length > 50)
|
|
|
|
|
this.messages.shift()
|
|
|
|
|
if (this.messages.length > 50) {
|
|
|
|
|
this.messages.splice(0, this.messages.length - 50)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.$route.query.config_id) {
|
|
|
|
|
try {
|
|
|
|
|
this.config = await config.getRemoteConfig(this.$route.query.config_id)
|
|
|
|
|
this.styleElement.innerText = this.config.css
|
|
|
|
|
} catch (e) {
|
|
|
|
|
this.$message.error('获取配置失败:' + e)
|
|
|
|
|
},
|
|
|
|
|
filterTextMessage(data) {
|
|
|
|
|
if (this.config.blockGiftDanmaku && data.isGiftDanmaku) {
|
|
|
|
|
return false
|
|
|
|
|
} else if (this.config.blockLevel > 0 && data.authorLevel < this.config.blockLevel) {
|
|
|
|
|
return false
|
|
|
|
|
} else if (this.config.blockNewbie && data.isNewbie) {
|
|
|
|
|
return false
|
|
|
|
|
} else if (this.config.blockNotMobileVerified && !data.isMobileVerified) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for (let keyword of this.config.blockKeywords) {
|
|
|
|
|
if (data.content.indexOf(keyword) !== -1) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (let user of this.config.blockUsers) {
|
|
|
|
|
if (data.authorName === user) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
},
|
|
|
|
|
mergeSimilar(content) {
|
|
|
|
|
if (!this.config.mergeSimilarDanmaku) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for (let i = this.messages.length - 1; i >= 0 && i >= this.messages.length - 5; i--) {
|
|
|
|
|
let message = this.messages[i]
|
|
|
|
|
if (
|
|
|
|
|
(message.content.indexOf(content) !== -1 || content.indexOf(message.content) !== -1) // 包含对方
|
|
|
|
|
&& Math.abs(message.content.length - content.length) < Math.min(message.content.length, content.length) // 长度差比两者长度都小
|
|
|
|
|
) {
|
|
|
|
|
message.repeated++
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
beforeDestroy() {
|
|
|
|
|
document.head.removeChild(this.styleElement)
|
|
|
|
|
this.websocket.close()
|
|
|
|
|
},
|
|
|
|
|
updated() {
|
|
|
|
|
window.scrollTo(0, document.body.scrollHeight)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|