article-detail.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <template>
  2. <view class="detail-container">
  3. <!-- 加载状态 -->
  4. <view v-if="loading" class="loading-wrap">
  5. <view class="loading-spinner"></view>
  6. <text class="loading-text">加载中...</text>
  7. </view>
  8. <!-- 错误状态 -->
  9. <view v-else-if="error" class="error-wrap">
  10. <text class="error-icon">📄</text>
  11. <text class="error-text">{{ errorMsg }}</text>
  12. <button class="retry-btn" @click="loadDetail(articleId)">重新加载</button>
  13. </view>
  14. <!-- 文章内容 -->
  15. <view v-else-if="article" class="detail-content">
  16. <!-- 封面图 -->
  17. <image
  18. v-if="article.coverImage"
  19. class="detail-cover"
  20. :src="article.coverImage"
  21. mode="widthFix"
  22. />
  23. <!-- 文章标题 -->
  24. <text class="detail-title">{{ article.title }}</text>
  25. <!-- 元信息 -->
  26. <view class="detail-meta">
  27. <text class="meta-author">{{ article.author || '浠艾福' }}</text>
  28. <text class="meta-separator">|</text>
  29. <text class="meta-date">{{ getFormattedDate() }}</text>
  30. <text class="meta-separator">|</text>
  31. <text class="meta-read">{{ article.readTime || 3 }} 分钟阅读</text>
  32. </view>
  33. <!-- 分类标签 -->
  34. <view class="detail-category-row">
  35. <text class="detail-category">{{ article.categoryName || '' }}</text>
  36. </view>
  37. <!-- 文章标签 -->
  38. <view v-if="tagList.length" class="detail-tags">
  39. <text v-for="(tag, idx) in tagList" :key="idx" class="tag-item">{{ tag }}</text>
  40. </view>
  41. <!-- 分割线 -->
  42. <view class="divider"></view>
  43. <!-- 文章正文(富文本) -->
  44. <view class="detail-body">
  45. <rich-text :nodes="article.content" />
  46. </view>
  47. <!-- 底部完成按钮 -->
  48. <view class="detail-footer">
  49. <button class="read-btn" @click="onReadComplete">阅读完成</button>
  50. </view>
  51. </view>
  52. <!-- 空状态 -->
  53. <view v-else class="error-wrap">
  54. <text class="error-icon">📄</text>
  55. <text class="error-text">文章不存在</text>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. import { getArticleDetail, recordArticleRead } from '../../utils/api.js'
  61. export default {
  62. data() {
  63. return {
  64. articleId: '',
  65. article: null,
  66. loading: true,
  67. error: false,
  68. errorMsg: '',
  69. tagList: []
  70. }
  71. },
  72. onLoad(options) {
  73. if (options && options.id) {
  74. this.articleId = options.id
  75. this.loadDetail(options.id)
  76. } else {
  77. this.error = true
  78. this.errorMsg = '参数错误'
  79. this.loading = false
  80. }
  81. },
  82. methods: {
  83. async loadDetail(id) {
  84. this.loading = true
  85. this.error = false
  86. try {
  87. const res = await getArticleDetail({ id: id })
  88. if (res.code === 200 && res.data) {
  89. this.article = res.data
  90. // 解析标签 JSON 字符串
  91. if (res.data.tags) {
  92. try {
  93. let parsed = JSON.parse(res.data.tags)
  94. this.tagList = Array.isArray(parsed) ? parsed : []
  95. } catch (e) {
  96. this.tagList = []
  97. }
  98. }
  99. } else {
  100. this.error = true
  101. this.errorMsg = '文章不存在或无权限查看'
  102. }
  103. } catch (e) {
  104. this.error = true
  105. this.errorMsg = '加载失败,请稍后重试'
  106. } finally {
  107. this.loading = false
  108. }
  109. },
  110. getFormattedDate() {
  111. if (this.article && this.article.publishedAt) {
  112. return this.article.publishedAt.slice(0, 10)
  113. }
  114. return ''
  115. },
  116. async onReadComplete() {
  117. try {
  118. await recordArticleRead({ id: this.article.id })
  119. uni.showToast({ title: '阅读记录已保存', icon: 'success' })
  120. } catch (e) {
  121. // 静默处理
  122. }
  123. setTimeout(function() {
  124. uni.navigateBack()
  125. }, 1500)
  126. }
  127. }
  128. }
  129. </script>
  130. <style scoped>
  131. .detail-container {
  132. min-height: 100vh;
  133. background: #fff;
  134. padding-bottom: 120rpx;
  135. }
  136. /* ===== 加载状态 ===== */
  137. .loading-wrap {
  138. display: flex;
  139. flex-direction: column;
  140. align-items: center;
  141. justify-content: center;
  142. padding-top: 300rpx;
  143. }
  144. .loading-spinner {
  145. width: 60rpx;
  146. height: 60rpx;
  147. border: 4rpx solid #e0e0e0;
  148. border-top-color: #5B9BD5;
  149. border-radius: 50%;
  150. animation: spin 0.8s linear infinite;
  151. margin-bottom: 20rpx;
  152. }
  153. @keyframes spin {
  154. 0% { transform: rotate(0deg); }
  155. 100% { transform: rotate(360deg); }
  156. }
  157. .loading-text {
  158. font-size: 26rpx;
  159. color: #999;
  160. }
  161. /* ===== 错误状态 ===== */
  162. .error-wrap {
  163. display: flex;
  164. flex-direction: column;
  165. align-items: center;
  166. padding-top: 300rpx;
  167. }
  168. .error-icon {
  169. font-size: 100rpx;
  170. margin-bottom: 24rpx;
  171. }
  172. .error-text {
  173. font-size: 28rpx;
  174. color: #999;
  175. margin-bottom: 30rpx;
  176. }
  177. .retry-btn {
  178. width: 240rpx;
  179. height: 72rpx;
  180. line-height: 72rpx;
  181. background: #5B9BD5;
  182. color: #fff;
  183. font-size: 28rpx;
  184. border-radius: 36rpx;
  185. text-align: center;
  186. border: none;
  187. }
  188. .retry-btn::after {
  189. border: none;
  190. }
  191. /* ===== 内容区 ===== */
  192. .detail-cover {
  193. width: 100%;
  194. max-height: 500rpx;
  195. display: block;
  196. }
  197. .detail-title {
  198. display: block;
  199. font-size: 36rpx;
  200. font-weight: bold;
  201. color: #333;
  202. line-height: 1.4;
  203. padding: 30rpx 30rpx 0;
  204. }
  205. /* ===== 元信息 ===== */
  206. .detail-meta {
  207. display: flex;
  208. flex-direction: row;
  209. align-items: center;
  210. padding: 16rpx 30rpx 0;
  211. font-size: 22rpx;
  212. color: #999;
  213. }
  214. .meta-author {
  215. color: #5B9BD5;
  216. }
  217. .meta-separator {
  218. margin: 0 12rpx;
  219. color: #ddd;
  220. }
  221. .meta-date,
  222. .meta-read {
  223. color: #999;
  224. }
  225. /* ===== 分类标签 ===== */
  226. .detail-category-row {
  227. padding: 16rpx 30rpx 0;
  228. }
  229. .detail-category {
  230. display: inline-block;
  231. font-size: 20rpx;
  232. color: #5B9BD5;
  233. background: rgba(91, 155, 213, 0.1);
  234. padding: 4rpx 16rpx;
  235. border-radius: 8rpx;
  236. }
  237. /* ===== 标签 ===== */
  238. .detail-tags {
  239. display: flex;
  240. flex-direction: row;
  241. flex-wrap: wrap;
  242. padding: 12rpx 30rpx 0;
  243. gap: 12rpx;
  244. }
  245. .tag-item {
  246. font-size: 20rpx;
  247. color: #8B5CF6;
  248. background: rgba(139, 92, 246, 0.08);
  249. padding: 4rpx 14rpx;
  250. border-radius: 8rpx;
  251. }
  252. /* ===== 分割线 ===== */
  253. .divider {
  254. height: 1rpx;
  255. background: #eee;
  256. margin: 24rpx 30rpx;
  257. }
  258. /* ===== 正文 ===== */
  259. .detail-body {
  260. padding: 0 30rpx;
  261. font-size: 28rpx;
  262. color: #444;
  263. line-height: 1.8;
  264. }
  265. .detail-body rich-text {
  266. word-break: break-word;
  267. }
  268. /* ===== 底部按钮 ===== */
  269. .detail-footer {
  270. padding: 40rpx 60rpx;
  271. }
  272. .read-btn {
  273. width: 100%;
  274. height: 88rpx;
  275. line-height: 88rpx;
  276. background: linear-gradient(135deg, #5B9BD5, #3A7CC4);
  277. color: #fff;
  278. font-size: 32rpx;
  279. font-weight: bold;
  280. border-radius: 44rpx;
  281. text-align: center;
  282. border: none;
  283. }
  284. .read-btn::after {
  285. border: none;
  286. }
  287. </style>