| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <template>
- <view class="container">
- <!-- 顶部导航栏 -->
- <view class="header">
- <view class="back-btn" @click="goBack">
- <text class="back-icon">←</text>
- </view>
- <text class="header-title">消息</text>
- <view class="header-right"></view>
- </view>
-
- <!-- 消息列表 -->
- <scroll-view class="scroll-content" scroll-y>
- <view
- class="message-item"
- v-for="(message, index) in messageList"
- :key="index"
- @click="handleMessageClick(message)"
- >
- <image class="message-avatar" :src="message.avatar" mode="aspectFill"></image>
- <view class="message-content">
- <view class="message-header">
- <text class="message-sender">{{ message.sender }}</text>
- <text class="message-time">{{ message.time }}</text>
- </view>
- <view class="message-action">
- <text class="action-icon">{{ message.actionIcon }}</text>
- <text class="action-text">{{ message.actionText }}</text>
- </view>
- <view class="comment-bubble" v-if="message.comment">
- <text class="comment-text">{{ message.comment }}</text>
- </view>
- </view>
- </view>
-
- <!-- 空状态 -->
- <view class="empty-state" v-if="messageList.length === 0">
- <text class="empty-text">暂无消息</text>
- </view>
- </scroll-view>
- </view>
- </template>
- <script>
- import { getMessages, markMessageAsRead } from '@/utils/api.js'
-
- export default {
- data() {
- return {
- messageList: [],
- loading: false,
- page: 1,
- size: 20,
- hasMore: true
- }
- },
- onLoad() {
- this.loadMessages()
- },
- onPullDownRefresh() {
- this.page = 1
- this.messageList = []
- this.hasMore = true
- this.loadMessages().finally(() => {
- uni.stopPullDownRefresh()
- })
- },
- onReachBottom() {
- if (this.hasMore && !this.loading) {
- this.page++
- this.loadMessages()
- }
- },
- methods: {
- async loadMessages() {
- const userInfo = uni.getStorageSync('userInfo')
- if (!userInfo || !userInfo.id) {
- uni.showToast({
- title: '请先登录',
- icon: 'none'
- })
- setTimeout(() => {
- uni.navigateTo({
- url: '/pages/login/login'
- })
- }, 1500)
- return
- }
-
- if (this.loading) return
- this.loading = true
-
- try {
- const res = await getMessages(userInfo.id, null, null, this.page, this.size)
-
- if (res && res.code === 200 && res.data) {
- const newMessages = res.data.list || []
-
- if (this.page === 1) {
- this.messageList = newMessages
- } else {
- this.messageList = this.messageList.concat(newMessages)
- }
-
- // 判断是否还有更多数据
- this.hasMore = newMessages.length >= this.size
-
- // 如果没有默认头像,设置默认头像
- this.messageList.forEach(msg => {
- if (!msg.avatar) {
- msg.avatar = 'https://picsum.photos/seed/default/200/200'
- }
- })
- } else {
- if (this.page === 1) {
- this.messageList = []
- }
- this.hasMore = false
- }
- } catch (error) {
- console.error('加载消息失败:', error)
- if (this.page === 1) {
- this.messageList = []
- }
- if (this.page > 1) {
- this.page--
- }
- } finally {
- this.loading = false
- }
- },
- goBack() {
- uni.navigateBack()
- },
- async handleMessageClick(message) {
- // 标记消息为已读
- if (message.isRead === 0) {
- try {
- await markMessageAsRead(message.id)
- message.isRead = 1
- } catch (error) {
- console.error('标记消息已读失败:', error)
- }
- }
-
- // 根据消息类型跳转到相关页面
- if (message.type === 'like' || message.type === 'reply') {
- // 点赞或回复消息,跳转到书籍详情页(可以从relatedId获取评论,再获取bookId)
- uni.showToast({
- title: '查看评论详情',
- icon: 'none'
- })
- // TODO: 可以根据relatedId查询评论,然后跳转到书籍详情页
- } else if (message.type === 'comment') {
- // 评论消息,跳转到书籍详情页
- uni.showToast({
- title: '查看书籍详情',
- icon: 'none'
- })
- // TODO: 可以根据relatedId跳转到书籍详情页
- }
- }
- }
- }
- </script>
- <style scoped>
- .container {
- width: 100%;
- height: 100vh;
- background-color: #FFFFFF;
- display: flex;
- flex-direction: column;
- padding-top: 30px;
- box-sizing: border-box;
- }
-
- .header {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 20rpx 30rpx;
- background-color: #FFFFFF;
- border-bottom: 1rpx solid #F0F0F0;
- position: relative;
- }
-
- .back-btn {
- width: 60rpx;
- height: 60rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- z-index: 10;
- }
-
- .back-icon {
- font-size: 40rpx;
- color: #333333;
- font-weight: bold;
- }
-
- .header-title {
- position: absolute;
- left: 50%;
- transform: translateX(-50%);
- font-size: 36rpx;
- font-weight: bold;
- color: #333333;
- }
-
- .header-right {
- width: 60rpx;
- }
-
- .scroll-content {
- flex: 1;
- width: 100%;
- padding-bottom: env(safe-area-inset-bottom);
- }
-
- .message-item {
- display: flex;
- padding: 30rpx;
- border-bottom: 1rpx solid #F0F0F0;
- background-color: #FFFFFF;
- }
-
- .message-item:last-child {
- border-bottom: none;
- }
-
- .message-avatar {
- width: 80rpx;
- height: 80rpx;
- border-radius: 50%;
- margin-right: 20rpx;
- flex-shrink: 0;
- background-color: #F5F5F5;
- }
-
- .message-content {
- flex: 1;
- display: flex;
- flex-direction: column;
- min-width: 0;
- }
-
- .message-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 15rpx;
- }
-
- .message-sender {
- font-size: 32rpx;
- font-weight: bold;
- color: #333333;
- }
-
- .message-time {
- font-size: 24rpx;
- color: #999999;
- }
-
- .message-action {
- display: flex;
- align-items: center;
- margin-bottom: 15rpx;
- }
-
- .action-icon {
- font-size: 28rpx;
- margin-right: 10rpx;
- }
-
- .action-text {
- font-size: 28rpx;
- color: #666666;
- }
-
- .comment-bubble {
- background-color: #F5F5F5;
- border-radius: 12rpx;
- padding: 20rpx;
- margin-top: 10rpx;
- }
-
- .comment-text {
- font-size: 28rpx;
- color: #333333;
- line-height: 1.6;
- }
-
- .empty-state {
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 200rpx 0;
- }
-
- .empty-text {
- font-size: 28rpx;
- color: #999999;
- }
- </style>
|