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.

295 lines
7.0 KiB

<template>
<view class="message-page" :class="{ 'wechat-browser': isWeixinBrowser }">
<view class="header-title" :class="{ 'hide-in-wechat': isWeixinBrowser }">消息</view>
<view class="message-list">
<template v-if="noticeList.length > 0">
<view
v-for="(item, idx) in noticeList"
:key="item.id"
class="message-card"
>
<view class="message-card-header">
<view class="tag" :style="{ background: tagColors[idx % tagColors.length] }">
<text class="tag-text">{{ item.type_name }}</text>
</view>
<view class="date">{{ formatChinaDate(item.created_at) }}</view>
</view>
<view class="message-title">{{ item.title }}</view>
<view class="message-content">{{ item.content }}</view>
</view>
</template>
<template v-else>
<view class="empty-box">
<image src="/static/empty.png" class="empty-img" mode="aspectFit" />
<view class="empty-text">暂无消息</view>
</view>
</template>
<!-- 加载更多提示 -->
<view v-if="noticeList.length > 0" class="load-more">
<view v-if="loading" class="loading-text">加载中...</view>
<view v-else-if="!hasMore" class="no-more-text"></view>
</view>
</view>
</view>
</template>
<script>
import { base } from '@/common/util.js'
import { API } from '@/config/index.js';
export default {
name: 'MessagePage',
data() {
return {
noticeList: [],
isWeixinBrowser: false,
page: 1,
lastPage: 1,
loading: false,
hasMore: true,
tagColors: [
'linear-gradient(90deg, #ffb980 0%, #ffc99a 100%)',
'linear-gradient(90deg, #217aff 0%, #3b7cff 100%)',
'linear-gradient(90deg, #ff5c5c 0%, #ff7a7a 100%)',
'linear-gradient(90deg, #22c58b 0%, #2ed9a3 100%)',
'#f39c12', '#8e44ad', '#16a085'
]
}
},
onLoad() {
// #ifdef H5
this.isWeixinBrowser = /MicroMessenger/i.test(navigator.userAgent)
// #endif
},
onShow() {
// 重置分页
this.page = 1;
this.hasMore = true;
this.fetchNotifications(true);
},
// 下拉刷新
onPullDownRefresh() {
this.page = 1;
this.hasMore = true;
this.fetchNotifications(true).finally(() => {
uni.stopPullDownRefresh();
});
},
// 上拉加载更多
onReachBottom() {
if (this.hasMore && !this.loading) {
this.loadMore();
}
},
methods: {
formatChinaDate: base.formatChinaDate,
async fetchNotifications(reset = false) {
const token = uni.getStorageSync('token');
if (!token) return;
// 如果正在加载,直接返回
if (this.loading) return;
this.loading = true;
try {
const res = await new Promise((resolve, reject) => {
uni.request({
url: `${API.NOTIFICATION_LIST}?token=${token}`,
method: 'GET',
data: {
page: this.page,
per_page: 5
},
success: resolve,
fail: reject
});
});
if (res.data && res.data.errcode === 0) {
const newList = Array.isArray(res.data.data.data) ? res.data.data.data : [];
this.lastPage = res.data.data.last_page || 1;
if (reset) {
// 重置列表
this.noticeList = newList;
} else {
// 追加数据
this.noticeList = [...this.noticeList, ...newList];
}
// 判断是否还有更多数据
this.hasMore = this.page < this.lastPage;
console.log('当前页:', this.page, '总页数:', this.lastPage, '是否有更多:', this.hasMore);
} else {
uni.showToast({
title: res.data.errmsg || '获取消息列表失败',
icon: 'none'
});
}
} catch (e) {
console.error('获取消息列表失败:', e);
uni.showToast({
title: '网络错误',
icon: 'none'
});
} finally {
this.loading = false;
}
},
// 加载更多
loadMore() {
if (this.hasMore && !this.loading) {
this.page += 1;
this.fetchNotifications(false);
}
}
}
}
</script>
<style scoped>
.message-page {
background: linear-gradient(180deg, #cbe6ff 0%, #f6faff 100%);
min-height: 100vh;
padding-bottom: 80rpx;
font-family: 'SourceHanSansCN', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}
.wechat-browser {
margin-top: -44rpx;
}
.header-title {
text-align: center;
font-size: 36rpx;
font-weight: bold;
padding-top: 7vh;
letter-spacing: 2rpx;
}
.header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20px 16px 10px 16px;
background: linear-gradient(180deg, #cbe6ff 0%, #f6faff 100%);
}
.back-btn, .more-btn {
font-size: 24px;
color: #333;
}
.title {
font-size: 22px;
font-weight: bold;
color: #222;
}
.message-list {
padding: 10px 0 0 0;
margin-top: 44rpx;
}
.message-card {
background: #fff;
border-radius: 10px;
margin: 0 16px 16px 16px;
box-shadow: 0 2px 8px rgba(0,0,0,0.04);
padding: 18px 18px 12px 18px;
height: 272rpx;
}
.message-card-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
}
.tag {
font-size: 22rpx;
padding: 4rpx 18rpx;
border-radius: 8rpx;
margin-right: 24rpx;
white-space: nowrap;
color: #fff;
display: inline-block;
font-weight: 500;
border: none;
transform: skewX(-20deg);
}
.tag-text {
display: inline-block;
transform: skewX(20deg);
}
.date {
color: #173766;
font-size: 15px;
}
.message-title {
font-size: 16px;
font-weight: 550;
margin-bottom: 12px;
color: #222;
margin-top: 12px;
}
.message-content {
color: #355;
font-size: 14px;
color: #173766;
}
.tabbar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
height: 60px;
background: #fff;
display: flex;
border-top: 1px solid #eaeaea;
z-index: 10;
}
.tab-item {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
color: #888;
font-size: 14px;
}
.tab-item.active {
color: #217aff;
}
.icon {
font-size: 22px;
margin-bottom: 2px;
}
.hide-in-wechat {
display: none !important;
}
.empty-box {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-top: 120rpx;
}
.empty-img {
width: 320rpx;
height: 320rpx;
margin-bottom: 32rpx;
}
.empty-text {
color: #888;
font-size: 28rpx;
}
.load-more {
padding: 30rpx 0;
text-align: center;
}
.loading-text {
color: #888;
font-size: 28rpx;
}
.no-more-text {
color: #999;
font-size: 26rpx;
}
</style>