You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
reagent_manage/vue/src/view/message/user/MessageStream.vue

196 lines
5.2 KiB

<template>
<div v-loading="config.loading" class="message-stream">
<el-button
6 years ago
v-if="showReadAllBtn>0"
plain
size="mini"
style="margin-bottom: 20px"
@click="readAll"
>
清除所有未读
</el-button>
<empty v-if="tableData.length===0"/>
<template v-else>
<el-timeline>
<el-timeline-item
6 years ago
v-for="msg in tableData"
:key="msg.index"
:timestamp="msg.time"
placement="top"
>
<el-card
6 years ago
v-for="child in msg.children"
:key="child.id"
:class="{'message-stream__item':true,viewed:child.__read}"
@click.native="read(child)"
>
<div class="message-stream__item__title">
<span v-if="!child.__read" class="dot success"/>
6 years ago
<b>{{ transformType(child.type) }}</b>
{{ child.title }}
</div>
<auto-hidden :html="child.content"/>
6 years ago
<div class="message-stream__item__footer"><b>By:</b> {{ child.pname }}</div>
</el-card>
</el-timeline-item>
</el-timeline>
<el-pagination
6 years ago
background
:current-page="searchForm.page"
:page-size="searchForm.pageSize"
:total="searchForm.total"
hide-on-single-page
layout="prev, pager, next"
@current-change="pageChange"
/>
</template>
</div>
</template>
<script>
import tablePageMixin from '@/mixin/tablePageMixin'
6 years ago
import AutoHidden from "./AutoHidden"
import Empty from '@/component/Empty'
6 years ago
import {search, read, readAll} from "@/api/message/user"
6 years ago
export default {
name: "MessageStream",
6 years ago
mixins: [tablePageMixin],
6 years ago
components: {AutoHidden, Empty},
6 years ago
props: {
mode: String,
},
6 years ago
data() {
return {
searchForm: {
pageSize: 30
},
6 years ago
}
},
6 years ago
computed: {
unreadCount() {
return this.$store.state.message.unreadCount
},
6 years ago
showReadAllBtn() {
return this.mode === 'unread' && this.unreadCount > 0
}
},
6 years ago
watch: {
mode() {
this.search()
}
},
methods: {
search() {
if (this.config.loading) return
const unread = this.mode === 'unread'
const currentMode = this.mode
this.tableData = []
this.config.loading = true
search({...this.searchForm, unread})
.then(({list, total}) => {
if (this.mode !== currentMode) return
unread && this.$store.commit('message/unreadCount', total)
this.searchForm.total = total
this.tableData = this.transformList(list)
})
6 years ago
.finally(() => this.config.loading = false)
},
6 years ago
//按照发布时间分组,精确到日
transformList(list) {
const thisYear = new Date().getFullYear()
const map = {}
let index = 0
list.forEach(item => {
let time = new Date(item.ptime),
year = time.getFullYear(),
month = time.getMonth() + 1,
day = time.getDate()
//根据模式判断是未读还是已读
item.__read = this.mode === 'read'
time = (year === thisYear ? '' : `${year}`) + `${month}${day}`
const already = map[time]
if (!already) {
map[time] = {index: index++, time, children: [item]}
}
else already.children.push(item)
})
6 years ago
const result = []
6 years ago
Object.keys(map).forEach(key => result[map[key].index] = map[key])
6 years ago
return result
},
6 years ago
read(msg) {
if (msg.read) return
msg.read = true
this.$store.commit('message/unreadCount', this.unreadCount - 1)
read(msg.id)
},
6 years ago
readAll() {
readAll().then(() => this.search())
},
transformType(type) {
switch (type) {
case 0:
return '提醒'
case 1:
return '公告'
}
},
}
6 years ago
}
</script>
<style lang="scss">
6 years ago
.message-stream {
&__item {
cursor: pointer;
position: relative;
color: #666;
background: #FFF9EA;
& + & {
margin-top: 20px;
}
6 years ago
&.viewed {
cursor: auto;
background: transparent;
6 years ago
&::before {
visibility: hidden;
}
6 years ago
}
6 years ago
&__title {
text-align: left;
6 years ago
.dot {
height: 8px;
width: 8px;
margin-right: 0;
}
6 years ago
}
6 years ago
&__footer {
text-align: right;
}
}
6 years ago
}
</style>