Vue 自定义消息通知组件
阅读前
阅读后
话不多说上代码
<template>
<div class="myCollect pageBg">
<div class="deleteall" @click="readall">
<span>全部已读</span>
<!-- <span class="icons icon-yuedu" />-->
</div>
<div class="container">
<div
:class="`contain-info ${item.read && 'isread'}`"
v-for="item in list"
:key="item.id"
>
<div class="notice">
<div class="iconmessage"><div class="icons icon-xiaoxi"></div></div>
<div class="info">
<div class="h3">
<p>{{ item.origin }}消息通知</p>
</div>
<div class="content">
<div class="left-part">
<span class="right">{{ item.content }}</span>
</div>
</div>
</div>
<div class="closemes" @click="closemessage(item.id)">
<van-icon name="close" />
</div>
</div>
<div class="more" @click="readinfo(item.id, item.url)">
<div class="txt">{{ item.createdTime }}</div>
<div class="toinfo">
<span class="txt">查看详情</span>
<span class="icons icon-r-jiantou" />
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "myCollect",
data() {
return {
content: "",
list: [],
page: {
current: 1,
pageSize: 20,
total: 1
},
loading: false, //加载状态
finished: false, //全部加载完成
immediate: false,
activeNames: []
};
},
components: {},
methods: {
loadData() {
this.list = [];
this.$nextTick(() => {
this.findList();
});
},
showtime(time) {
let date =
typeof time === "number"
? new Date(time)
: new Date((time || "").replace(/-/g, "/"));
let diff = (new Date().getTime() - date.getTime()) / 1000;
let dayDiff = Math.floor(diff / 86400);
let isValidDate =
Object.prototype.toString.call(date) === "[object Date]" &&
!isNaN(date.getTime());
if (!isValidDate) {
window.console.error("not a valid date");
}
const formatDate = function(date) {
let today = new Date(date);
let year = today.getFullYear();
let month = ("0" + (today.getMonth() + 1)).slice(-2);
let day = ("0" + today.getDate()).slice(-2);
let hour = today.getHours();
let minute = today.getMinutes();
let second = today.getSeconds();
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
};
if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 31) {
return formatDate(date);
}
return (
(dayDiff === 0 &&
((diff < 60 && "刚刚") ||
(diff < 120 && "1分钟前") ||
(diff < 3600 && Math.floor(diff / 60) + "分钟前") ||
(diff < 7200 && "1小时前") ||
(diff < 86400 && Math.floor(diff / 3600) + "小时前"))) ||
(dayDiff === 1 && "昨天") ||
(dayDiff < 7 && dayDiff + "天前") ||
(dayDiff < 31 && Math.ceil(dayDiff / 7) + "周前")
);
},
findList() {
//调用接口,查询数据
},
//删除
closemessage(id) {
//调用接口。删除
},
//已读
readinfo(id) {
//调用接口,标记为已读状态
},
//一键已读
readall() {
//调用接口,全部标记为已读状态
},
},
computed: {
},
created() {
this.loadData();
}
};
</script>
<style lang="scss" scoped>
.myCollect {
height: 100vh;
overflow-y: scroll;
}
.myCollect {
width: 100%;
height: 100vh;
display: flex;
flex-direction: column;
}
.van-list {
width: 100%;
}
.isread {
opacity: 0.7;
}
.isread .iconmessage {
background: #999999;
}
.contain-info {
width: 100%;
margin-bottom: 10px;
}
.notice {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
width: calc(100% - 30px);
padding: 0 15px;
background: white;
padding-bottom: 10px;
border-bottom: 0.55px solid #eee;
padding-top: 10px;
}
.more {
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
width: calc(100% - 30px);
padding: 0 15px;
background: white;
}
.txt {
font-size: 12px;
font-family: MicrosoftYaHei;
color: rgba(169, 169, 169, 1);
line-height: 16px;
padding: 20px 0;
}
.van-icon-close {
font-size: 23px;
color: rgba(169, 169, 169, 1);
}
.deleteall {
width: calc(100% - 20px);
padding: 15px 10px;
width: 56px;
height: 19px;
font-size: 14px;
font-family: MicrosoftYaHei;
color: rgba(105, 105, 105, 1);
line-height: 19px;
}
.container {
flex: 1;
width: calc(100% - 20px);
padding: 0 10px;
}
.container__top {
position: fixed;
width: 100%;
margin: auto;
background: #fff;
top: 0;
left: 0;
right: 0;
-webkit-box-shadow: 0 0 1.33333vw 1px #ccc;
box-shadow: 0 0 1.33333vw 1px #ccc;
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 2.66667vw;
z-index: 99;
.time-body {
width: 100%;
display: flex;
flex-flow: row nowrap;
justify-content: space-between;
align-items: center;
.van-icon {
color: #999999;
position: relative;
top: 3px;
font-size: 25px;
}
.con-but {
background-color: #07c160;
line-height: 30px;
width: 90px;
color: #fff;
text-align: center;
border-radius: 12px;
height: 30px;
}
.pv-left {
line-height: 30px;
width: 65%;
}
}
}
.van-collapse-item {
margin-bottom: 10px;
}
/deep/ .van-cell__title {
font-weight: bold;
}
.iconmessage {
width: 44px;
height: 44px;
background: rgba(125, 191, 243, 1);
border-radius: 100%;
margin-top: 7px;
}
.icon-xiaoxi {
text-align: center;
line-height: 45px;
font-size: 26px;
color: white;
}
.info {
display: flex;
flex-flow: column nowrap;
justify-content: center;
align-items: flex-start;
position: relative;
top: 0.66667vw;
width: calc(100% - 24vw);
}
.closemes {
color: #52c486;
font-size: 14px;
text-align: right;
}
.h3 {
font-family: MicrosoftYaHei;
color: rgba(169, 169, 169, 1);
line-height: 5vw;
color: #232323;
font-size: 14px;
font-weight: normal;
padding: 0;
float: left;
text-align: left;
}
.content {
margin-top: 10px;
}
.left-part {
overflow: hidden;
display: flex;
flex-flow: row wrap;
justify-content: start;
align-items: center;
.tag {
padding: 2px 4px;
margin: 2px;
color: #07c160;
border: #07c160 1px solid;
border-radius: 10px;
}
.right {
font-size: 14px;
}
}
.toinfo {
display: flex;
justify-content: center;
align-items: center;
}
</style>