绿满眶商城微信小程序-uniapp
zxl
2025-06-24 c146b580fdca424b6bb35b0f8d31ffec3952e642
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<template>
  <view class="detail-page">
    <!-- 加载状态 -->
    <view v-if="loading" class="loading-container">
      <u-loading mode="circle" size="48"></u-loading>
      <text class="loading-text">加载中...</text>
    </view>
    
    <!-- 内容区域 -->
    <template v-else>
      <!-- 标题区 -->
      <view class="header">
        <text class="title">{{ detailData.title }}</text>
        <text class="meta">
          <text class="date">{{ formatDate(detailData.publishDate) }}</text>
        </text>
      </view>
      
      <!-- 富文本内容 -->
      <scroll-view scroll-y class="content-wrapper">
        <rich-text 
          :nodes="formatContent(detailData.content)" 
          class="content"
        ></rich-text>
      </scroll-view>
    </template>
    
    <!-- 错误提示 -->
    <u-toast ref="uToast"></u-toast>
  </view>
</template>
 
<script>
import { detail } from '@/api/news.js'
 
export default {
  data() {
    return {
      detailId: '',
      loading: true,
      detailData: {
        title: '',
        content: '',
        publishDate: ''
      }
    }
  },
  onLoad(options) {
    if (options.id) {
      this.detailId = options.id
      this.loadDetailData()
    }
  },
  methods: {
    async loadDetailData() {
      this.loading = true
      try {
        const res = await detail(this.detailId)
        if (res.statusCode === 200) {
            this.detailData = {
            title: res.data.data.title || '无标题',
            content: res.data.data.content || '',
            publishDate: res.data.data.publishDate || ''
          }
        } else {
          this.showError('加载失败: ' + (res.message || '未知错误'))
        }
      } catch (error) {
        console.error('加载详情出错:', error)
        this.showError('网络请求失败')
      } finally {
        this.loading = false
      }
    },
    
    formatDate(dateString) {
      if (!dateString) return '未知时间'
      
      try {
        const date = new Date(dateString)
        if (isNaN(date.getTime())) return dateString
        
        const year = date.getFullYear()
        const month = (date.getMonth() + 1).toString().padStart(2, '0')
        const day = date.getDate().toString().padStart(2, '0')
        const hours = date.getHours().toString().padStart(2, '0')
        const minutes = date.getMinutes().toString().padStart(2, '0')
        
        return `${year}-${month}-${day} ${hours}:${minutes}`
      } catch (e) {
        console.error('日期格式化错误:', e)
        return dateString
      }
    },
    
    formatContent(content) {
      if (!content) return ''
      
      // 基础处理:确保图片自适应
      const processedContent = content
        .replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:20rpx auto;"')
        .replace(/<table/gi, '<table style="width:100%;border-collapse:collapse;"')
        .replace(/<td/gi, '<td style="border:1px solid #ddd;padding:8px;"')
      
      return processedContent
    },
    
    showError(message) {
      this.$refs.uToast.show({
        type: 'error',
        message: message,
        duration: 2000
      })
    }
  }
}
</script>
 
<style lang="scss" scoped>
.detail-page {
  padding: 30rpx;
  min-height: 100vh;
  background-color: #f8f8f8;
}
 
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 60vh;
  
  .loading-text {
    margin-top: 20rpx;
    font-size: 28rpx;
    color: #999;
  }
}
 
.header {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
  
  .title {
    display: block;
    font-size: 36rpx;
    font-weight: bold;
    color: #333;
    line-height: 1.5;
    margin-bottom: 20rpx;
  }
  
  .meta {
    display: flex;
    justify-content: flex-end;
    font-size: 24rpx;
    color: #999;
    
    .date {
      margin-left: 15rpx;
    }
  }
}
 
.content-wrapper {
  background-color: #fff;
  border-radius: 16rpx;
  padding: 30rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
  height: calc(100vh - 300rpx);
  
  .content {
    font-size: 30rpx;
    line-height: 1.8;
    color: #333;
    
    // 覆盖rich-text内部元素样式
    /deep/ p {
      margin-bottom: 20rpx;
    }
    
    /deep/ h1, /deep/ h2, /deep/ h3 {
      margin: 40rpx 0 20rpx;
      font-weight: bold;
    }
    
    /deep/ ul, /deep/ ol {
      padding-left: 40rpx;
      margin-bottom: 20rpx;
    }
    
    /deep/ li {
      margin-bottom: 10rpx;
    }
    
    /deep/ a {
      color: #007aff;
      word-break: break-all;
    }
    
    /deep/ blockquote {
      border-left: 4rpx solid #ddd;
      padding-left: 20rpx;
      color: #666;
      margin: 20rpx 0;
    }
  }
}
</style>