article-detail.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. <!-- 定时器显示 -->
  50. <view class="reading-timer" v-if="readingSeconds >= 0">
  51. <text class="timer-icon">⏱</text>
  52. <text class="timer-text">{{ formatTime(readingSeconds) }}</text>
  53. <text class="timer-hint" v-if="readingSeconds < 60">阅读{{ 60 - readingSeconds }}秒可获取积分</text>
  54. <text class="timer-hint timer-ready" v-else>已达标,可领取积分</text>
  55. </view>
  56. <!-- 完成按钮 -->
  57. <button
  58. class="read-btn"
  59. :class="{ 'read-btn-active': readingSeconds >= 60 }"
  60. @click="onReadComplete"
  61. >阅读完成</button>
  62. </view>
  63. </view>
  64. <!-- 空状态 -->
  65. <view v-else class="error-wrap">
  66. <text class="error-icon">📄</text>
  67. <text class="error-text">文章不存在</text>
  68. </view>
  69. </view>
  70. </template>
  71. <script>
  72. import { getArticleDetail, recordArticleRead } from '../../utils/api.js'
  73. export default {
  74. data() {
  75. return {
  76. articleId: '',
  77. article: null,
  78. loading: true,
  79. error: false,
  80. errorMsg: '',
  81. tagList: [],
  82. readingSeconds: 0,
  83. readingTimer: null,
  84. readRecorded: false
  85. }
  86. },
  87. onLoad(options) {
  88. if (options && options.id) {
  89. this.articleId = options.id
  90. this.loadDetail(options.id)
  91. this.startReadingTimer()
  92. } else {
  93. this.error = true
  94. this.errorMsg = '参数错误'
  95. this.loading = false
  96. }
  97. },
  98. onHide() {
  99. this.stopReadingTimer()
  100. },
  101. onUnload() {
  102. this.stopReadingTimer()
  103. },
  104. methods: {
  105. async loadDetail(id) {
  106. this.loading = true
  107. this.error = false
  108. try {
  109. const res = await getArticleDetail({ id: id })
  110. if (res.code === 200 && res.data) {
  111. this.article = res.data
  112. // 解析标签 JSON 字符串
  113. if (res.data.tags) {
  114. try {
  115. let parsed = JSON.parse(res.data.tags)
  116. this.tagList = Array.isArray(parsed) ? parsed : []
  117. } catch (e) {
  118. this.tagList = []
  119. }
  120. }
  121. } else {
  122. this.error = true
  123. this.errorMsg = '文章不存在或无权限查看'
  124. }
  125. } catch (e) {
  126. this.error = true
  127. this.errorMsg = '加载失败,请稍后重试'
  128. } finally {
  129. this.loading = false
  130. }
  131. },
  132. getFormattedDate() {
  133. if (this.article && this.article.publishedAt) {
  134. return this.article.publishedAt.slice(0, 10)
  135. }
  136. return ''
  137. },
  138. startReadingTimer() {
  139. var self = this
  140. this.readingTimer = setInterval(function() {
  141. self.readingSeconds = self.readingSeconds + 1
  142. // Auto-submit when reaching 60 seconds (threshold)
  143. // But don't auto-submit - just indicate readiness
  144. }, 1000)
  145. },
  146. stopReadingTimer() {
  147. if (this.readingTimer) {
  148. clearInterval(this.readingTimer)
  149. this.readingTimer = null
  150. }
  151. },
  152. formatTime(seconds) {
  153. var m = Math.floor(seconds / 60)
  154. var s = seconds % 60
  155. return (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s)
  156. },
  157. async onReadComplete() {
  158. if (this.readRecorded) {
  159. uni.navigateBack()
  160. return
  161. }
  162. this.stopReadingTimer()
  163. try {
  164. var childId = uni.getStorageSync('currentChildId')
  165. var res = await recordArticleRead({
  166. id: this.article.id,
  167. durationSeconds: this.readingSeconds,
  168. childId: childId || undefined
  169. })
  170. this.readRecorded = true
  171. if (res.data && res.data.pointsEarned > 0) {
  172. uni.showToast({ title: '获得' + res.data.pointsEarned + '积分!', icon: 'success' })
  173. } else if (res.data && res.data.belowThreshold) {
  174. uni.showToast({ title: '阅读时间未达标', icon: 'none' })
  175. } else if (res.data && res.data.alreadyCompleted) {
  176. uni.showToast({ title: '已读过此文', icon: 'none' })
  177. } else {
  178. uni.showToast({ title: '阅读记录已保存', icon: 'success' })
  179. }
  180. } catch (e) {
  181. uni.showToast({ title: '记录保存失败', icon: 'none' })
  182. }
  183. var self = this
  184. setTimeout(function() {
  185. uni.navigateBack()
  186. }, 1500)
  187. }
  188. }
  189. }
  190. </script>
  191. <style scoped>
  192. .detail-container {
  193. min-height: 100vh;
  194. background: #fff;
  195. padding-bottom: 120rpx;
  196. }
  197. /* ===== 加载状态 ===== */
  198. .loading-wrap {
  199. display: flex;
  200. flex-direction: column;
  201. align-items: center;
  202. justify-content: center;
  203. padding-top: 300rpx;
  204. }
  205. .loading-spinner {
  206. width: 60rpx;
  207. height: 60rpx;
  208. border: 4rpx solid #e0e0e0;
  209. border-top-color: #5B9BD5;
  210. border-radius: 50%;
  211. animation: spin 0.8s linear infinite;
  212. margin-bottom: 20rpx;
  213. }
  214. @keyframes spin {
  215. 0% { transform: rotate(0deg); }
  216. 100% { transform: rotate(360deg); }
  217. }
  218. .loading-text {
  219. font-size: 26rpx;
  220. color: #999;
  221. }
  222. /* ===== 错误状态 ===== */
  223. .error-wrap {
  224. display: flex;
  225. flex-direction: column;
  226. align-items: center;
  227. padding-top: 300rpx;
  228. }
  229. .error-icon {
  230. font-size: 100rpx;
  231. margin-bottom: 24rpx;
  232. }
  233. .error-text {
  234. font-size: 28rpx;
  235. color: #999;
  236. margin-bottom: 30rpx;
  237. }
  238. .retry-btn {
  239. width: 240rpx;
  240. height: 72rpx;
  241. line-height: 72rpx;
  242. background: #5B9BD5;
  243. color: #fff;
  244. font-size: 28rpx;
  245. border-radius: 36rpx;
  246. text-align: center;
  247. border: none;
  248. }
  249. .retry-btn::after {
  250. border: none;
  251. }
  252. /* ===== 内容区 ===== */
  253. .detail-cover {
  254. width: 100%;
  255. max-height: 500rpx;
  256. display: block;
  257. }
  258. .detail-title {
  259. display: block;
  260. font-size: 36rpx;
  261. font-weight: bold;
  262. color: #333;
  263. line-height: 1.4;
  264. padding: 30rpx 30rpx 0;
  265. }
  266. /* ===== 元信息 ===== */
  267. .detail-meta {
  268. display: flex;
  269. flex-direction: row;
  270. align-items: center;
  271. padding: 16rpx 30rpx 0;
  272. font-size: 22rpx;
  273. color: #999;
  274. }
  275. .meta-author {
  276. color: #5B9BD5;
  277. }
  278. .meta-separator {
  279. margin: 0 12rpx;
  280. color: #ddd;
  281. }
  282. .meta-date,
  283. .meta-read {
  284. color: #999;
  285. }
  286. /* ===== 分类标签 ===== */
  287. .detail-category-row {
  288. padding: 16rpx 30rpx 0;
  289. }
  290. .detail-category {
  291. display: inline-block;
  292. font-size: 20rpx;
  293. color: #5B9BD5;
  294. background: rgba(91, 155, 213, 0.1);
  295. padding: 4rpx 16rpx;
  296. border-radius: 8rpx;
  297. }
  298. /* ===== 标签 ===== */
  299. .detail-tags {
  300. display: flex;
  301. flex-direction: row;
  302. flex-wrap: wrap;
  303. padding: 12rpx 30rpx 0;
  304. gap: 12rpx;
  305. }
  306. .tag-item {
  307. font-size: 20rpx;
  308. color: #8B5CF6;
  309. background: rgba(139, 92, 246, 0.08);
  310. padding: 4rpx 14rpx;
  311. border-radius: 8rpx;
  312. }
  313. /* ===== 分割线 ===== */
  314. .divider {
  315. height: 1rpx;
  316. background: #eee;
  317. margin: 24rpx 30rpx;
  318. }
  319. /* ===== 正文 ===== */
  320. .detail-body {
  321. padding: 0 30rpx;
  322. font-size: 28rpx;
  323. color: #444;
  324. line-height: 1.8;
  325. }
  326. .detail-body rich-text {
  327. word-break: break-word;
  328. }
  329. /* ===== 定时器 ===== */
  330. .reading-timer {
  331. display: flex;
  332. flex-direction: row;
  333. align-items: center;
  334. justify-content: center;
  335. padding: 16rpx 0 20rpx;
  336. }
  337. .timer-icon {
  338. font-size: 32rpx;
  339. margin-right: 8rpx;
  340. }
  341. .timer-text {
  342. font-size: 32rpx;
  343. font-weight: bold;
  344. color: #333;
  345. margin-right: 16rpx;
  346. }
  347. .timer-hint {
  348. font-size: 22rpx;
  349. color: #999;
  350. }
  351. .timer-ready {
  352. color: #22c55e;
  353. font-weight: bold;
  354. }
  355. /* ===== 底部按钮 ===== */
  356. .detail-footer {
  357. padding: 40rpx 60rpx;
  358. }
  359. .read-btn {
  360. width: 100%;
  361. height: 88rpx;
  362. line-height: 88rpx;
  363. background: #ddd;
  364. color: #fff;
  365. font-size: 32rpx;
  366. font-weight: bold;
  367. border-radius: 44rpx;
  368. text-align: center;
  369. border: none;
  370. transition: background 0.3s;
  371. }
  372. .read-btn-active {
  373. background: linear-gradient(135deg, #5B9BD5, #3A7CC4);
  374. }
  375. .read-btn::after {
  376. border: none;
  377. }
  378. </style>