article-detail.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <template>
  2. <view class="detail-container">
  3. <view v-if="loading" class="loading-wrap">
  4. <view class="loading-spinner"></view>
  5. <text class="loading-text">加载中...</text>
  6. </view>
  7. <view v-else-if="error" class="error-wrap">
  8. <text class="error-icon">📄</text>
  9. <text class="error-text">{{ errorMsg }}</text>
  10. <button class="retry-btn" @click="loadDetail(articleId)">重新加载</button>
  11. </view>
  12. <template v-else-if="article">
  13. <scroll-view scroll-y class="content-scroll" @scrolltolower="onScrollToBottom">
  14. <image v-if="article.coverImage" class="detail-cover" :src="article.coverImage" mode="widthFix" />
  15. <text class="detail-title">{{ article.title }}</text>
  16. <view class="detail-meta">
  17. <text class="meta-author">{{ article.author || '浠艾福' }}</text>
  18. <text class="meta-sep">|</text>
  19. <text class="meta-date">{{ formatDate(article.publishedAt) }}</text>
  20. <text class="meta-sep">|</text>
  21. <text class="meta-readtime">{{ article.readTime || 3 }}分钟阅读</text>
  22. <text class="reading-time-badge" v-if="!readingCompleted">{{ formatReadingTime(readingSeconds) }}</text>
  23. <text class="reading-time-badge completed" v-else>✅ 阅读完成</text>
  24. </view>
  25. <view class="detail-category-row">
  26. <text class="detail-category">{{ article.categoryName || '' }}</text>
  27. </view>
  28. <view v-if="article.relatedDimensions" class="detail-dimensions">
  29. <view v-for="dim in parseDimensions(article)" :key="dim.code" class="dim-bar-item">
  30. <view class="dim-bar" :style="{ background: dim.color, width: dim.weight + '%' }"></view>
  31. <text class="dim-label">{{ dim.name }}</text>
  32. </view>
  33. </view>
  34. <view class="divider"></view>
  35. <!-- 正文 -->
  36. <view class="detail-body">
  37. <mp-html :content="article.content" />
  38. </view>
  39. <!-- 评论区 -->
  40. </view>
  41. <view style="height: 200rpx;"></view>
  42. </scroll-view>
  43. <!-- 底部 -->
  44. <view class="detail-footer">
  45. <button class="share-btn" open-type="share" @click="onShareTap">
  46. <text class="share-btn-icon">📤</text>
  47. <text class="share-btn-text">分享</text>
  48. </button>
  49. <view class="share-btn" @click="generatePoster">
  50. <text class="share-btn-icon">🖼</text>
  51. <text class="share-btn-text">海报</text>
  52. </view>
  53. </view>
  54. </template>
  55. <ContentSharePoster :show="showPoster" :title="article && article.title" :coverImage="article && article.coverImage" :qrCodeBase64="posterQrCode" typeLabel="好文推荐" @close="showPoster = false" />
  56. <!-- 浠宝答题弹窗 -->
  57. <view class="quiz-mask" v-if="showQuiz" @click="showQuiz = false">
  58. <view class="quiz-dialog" @click.stop>
  59. <view class="quiz-mascot">
  60. <text class="mascot-icon">{{ mascotIcon }}</text>
  61. <text class="mascot-name">{{ mascotName }}</text>
  62. </view>
  63. <text class="quiz-intro">阅读完成!考考你三个问题~</text>
  64. <view class="quiz-question" v-for="(q, qi) in quizQuestions" :key="qi">
  65. <text class="q-title">问题{{ qi+1 }}: {{ q.question }}</text>
  66. <view class="q-options">
  67. <text v-for="(opt, oi) in q.options" :key="oi" class="q-option" :class="{ selected: quizAnswers[qi] === String.fromCharCode(65 + oi) }" @click="quizAnswers[qi] = String.fromCharCode(65 + oi)">{{ opt }}</text>
  68. </view>
  69. </view>
  70. <button class="quiz-submit" @click="submitQuiz">提交答案</button>
  71. </view>
  72. </view>
  73. <!-- 答题结果 -->
  74. <view class="result-mask" v-if="showResult">
  75. <view class="result-dialog">
  76. <text class="result-icon">{{ resultIcon }}</text>
  77. <text class="result-text">答对 {{ quizResult.correctCount }}/{{ quizResult.totalQuestions }} 题</text>
  78. <text class="result-energy">获得 {{ quizResult.earnedEnergy }} 能量 ⚡</text>
  79. <button class="result-btn" @click="showResult = false">知道了</button>
  80. </view>
  81. </view>
  82. </view>
  83. </template>
  84. <script>
  85. import { getArticleDetail, reportReadingTime, getShareQrCode, completeArticleRead, getArticleComments, createArticleComment, generateQuiz, submitQuiz } from '@/utils/api.js'
  86. import shareMixin from '../../components/share-mixin.js'
  87. import ContentSharePoster from '@/components/ContentSharePoster.vue'
  88. import MpHtml from '@/components/mp-html/mp-html.vue'
  89. export default {
  90. mixins: [shareMixin],
  91. components: { ContentSharePoster, MpHtml },
  92. data() {
  93. return {
  94. articleId: '', article: null, loading: true, error: false, errorMsg: '',
  95. readingSeconds: 0, lastReportedSeconds: 0, readingCompleted: false,
  96. isTimerRunning: false, timerHandle: null, syncHandle: null,
  97. showPoster: false, posterQrCode: '',
  98. // 评论
  99. comments: [], commentText: '', replyTarget: null,
  100. // 答题
  101. showQuiz: false, quizQuestions: [], quizAnswers: [], showResult: false, quizResult: {},
  102. mascotList: [{ icon: '🐶', name: '浠宝' }, { icon: '🐼', name: '福宝' }],
  103. mascotIcon: '🐶', mascotName: '浠宝'
  104. }
  105. },
  106. computed: {
  107. commentPlaceholder: function() {
  108. return this.replyTarget ? '回复 ' + (this.replyTarget.userName || '') + ':' : '写下你的评论...'
  109. }
  110. },
  111. onLoad(options) {
  112. this.mascotIcon = this.mascotList[Math.floor(Math.random() * 2)].icon
  113. this.mascotName = this.mascotList[Math.floor(Math.random() * 2)].name
  114. if (options && options.id) { this.articleId = options.id; this.loadDetail(options.id); this.loadComments() }
  115. else { this.error = true; this.errorMsg = '参数错误'; this.loading = false }
  116. },
  117. onShow() { if (this.article && !this.error) this.startReadingTimer() },
  118. onHide() { this.pauseReadingTimer() },
  119. onUnload() { this.pauseReadingTimer() },
  120. methods: {
  121. async loadDetail(id) {
  122. this.loading = true; this.error = false
  123. try {
  124. var res = await getArticleDetail({ id: id })
  125. if (res.code === 200 && res.data) {
  126. this.article = res.data
  127. this.setShareInfo('推荐阅读: ' + (res.data.title || ''), '/pages/article-center/article-detail?id=' + id)
  128. this.startReadingTimer()
  129. } else { this.error = true; this.errorMsg = '文章不存在或无权限查看' }
  130. } catch (e) { this.error = true; this.errorMsg = '加载失败' }
  131. finally { this.loading = false }
  132. },
  133. async loadComments() {
  134. try { var res = await getArticleComments({ articleId: this.articleId }); if (res.code === 200) this.comments = res.data || [] }
  135. catch (e) {}
  136. },
  137. formatDate(d) { return d ? d.slice(0, 10) : '' },
  138. parseDimensions: function(article) {
  139. var dimMap = { body: { code: 'body', color: '#FF8C42', name: '身' }, mind: { code: 'mind', color: '#FF6B9D', name: '心' }, wisdom: { code: 'wisdom', color: '#6366F1', name: '智' }, action: { code: 'action', color: '#10B981', name: '行' }, wealth: { code: 'wealth', color: '#F59E0B', name: '富' } }
  140. var raw = article && article.relatedDimensions ? article.relatedDimensions.split(',').map(function(s) { return s.trim().toLowerCase() }) : []
  141. var weights = null
  142. if (article && article.dimensionWeights) try { var w = JSON.parse(article.dimensionWeights); if (w && typeof w === 'object') weights = w } catch (e) {}
  143. if (!weights) {
  144. var n = raw.filter(function(c) { return dimMap[c] }).length
  145. if (n > 0) { var base = Math.floor(100 / n); var rem = 100 - base * n; weights = {}; var i = 0; raw.forEach(function(c) { if (!dimMap[c]) return; weights[c] = i < rem ? base + 1 : base; i++ }) }
  146. }
  147. return raw.filter(function(c) { return dimMap[c] }).map(function(c) { return { code: dimMap[c].code, name: dimMap[c].name, color: dimMap[c].color, weight: (weights && weights[c]) ? weights[c] : 20 } })
  148. },
  149. onScrollToBottom() {},
  150. startReadingTimer: function() {
  151. if (this.isTimerRunning || this.readingCompleted) return
  152. this.isTimerRunning = true
  153. var self = this
  154. this.timerHandle = setInterval(function() { self.readingSeconds++ }, 1000)
  155. this.syncHandle = setInterval(function() { self.reportReadingTime() }, 30000)
  156. },
  157. pauseReadingTimer: function() {
  158. if (!this.isTimerRunning) return
  159. this.isTimerRunning = false
  160. if (this.timerHandle) { clearInterval(this.timerHandle); this.timerHandle = null }
  161. if (this.syncHandle) { clearInterval(this.syncHandle); this.syncHandle = null }
  162. this.reportReadingTime()
  163. },
  164. async reportReadingTime() {
  165. var delta = this.readingSeconds - this.lastReportedSeconds
  166. if (delta <= 0) return
  167. this.lastReportedSeconds = this.readingSeconds
  168. var childId = uni.getStorageSync('currentChildId')
  169. if (!childId || !this.article) return
  170. reportReadingTime({ articleId: this.article.id, childId: parseInt(childId), durationSeconds: delta })
  171. // 达到阅读时长自动完成
  172. var targetSeconds = (this.article.readTime || 3) * 60
  173. if (!this.readingCompleted && this.readingSeconds >= targetSeconds) {
  174. this.readingCompleted = true
  175. this.pauseReadingTimer()
  176. try { await completeArticleRead({ articleId: this.article.id, childId: parseInt(childId), durationSeconds: this.readingSeconds }) } catch (e) {}
  177. uni.showToast({ title: '阅读完成 +5能量', icon: 'success' })
  178. // 弹出答题
  179. this.startQuiz()
  180. }
  181. },
  182. formatReadingTime: function(seconds) {
  183. if (seconds < 60) return '已读 ' + seconds + '秒'
  184. var min = Math.floor(seconds / 60); var sec = seconds % 60
  185. return '已读 ' + min + '分' + (sec > 0 ? sec + '秒' : '')
  186. },
  187. async generatePoster() {
  188. if (!this.articleId) return
  189. uni.showLoading({ title: '生成海报中...' })
  190. try {
  191. var res = await getShareQrCode('pages/article-center/article-detail', 'id=' + this.articleId)
  192. if (res && res.data) { this.posterQrCode = res.data.qrCodeBase64 || ''; this.showPoster = true }
  193. } catch (e) { uni.showToast({ title: '生成失败', icon: 'none' }) }
  194. finally { uni.hideLoading() }
  195. },
  196. // 评论
  197. startReply: function(comment) {
  198. this.replyTarget = comment
  199. this.commentText = ''
  200. uni.pageScrollTo({ selector: '.comment-input-row', duration: 300 })
  201. },
  202. async submitComment() {
  203. var text = this.commentText.trim()
  204. if (!text) return
  205. try {
  206. var params = { articleId: this.articleId, content: text }
  207. if (this.replyTarget) { params.parentId = this.replyTarget.id; params.replyToId = this.replyTarget.id }
  208. var res = await createArticleComment(params)
  209. if (res.code === 200) {
  210. uni.showToast({ title: '评论已提交,等待审核', icon: 'success' })
  211. this.commentText = ''; this.replyTarget = null
  212. } else { uni.showToast({ title: res.message || '评论失败', icon: 'none' }) }
  213. } catch (e) { uni.showToast({ title: '网络错误', icon: 'none' }) }
  214. },
  215. // 答题
  216. async startQuiz() {
  217. try {
  218. var res = await generateQuiz({ articleId: this.articleId })
  219. if (res.code === 200 && res.data && res.data.length > 0) {
  220. this.quizQuestions = res.data
  221. this.quizAnswers = []
  222. this.showQuiz = true
  223. }
  224. } catch (e) {}
  225. },
  226. async submitQuiz() {
  227. var correct = 0
  228. for (var i = 0; i < this.quizQuestions.length; i++) {
  229. if (this.quizAnswers[i] === this.quizQuestions[i].answer) correct++
  230. }
  231. var childId = uni.getStorageSync('currentChildId')
  232. try {
  233. var res = await submitQuiz({ correctCount: correct, childId: parseInt(childId || 0) })
  234. if (res.code === 200) {
  235. this.quizResult = res.data || { correctCount: correct, totalQuestions: 3, earnedEnergy: correct * 5 }
  236. this.showQuiz = false
  237. this.showResult = true
  238. }
  239. } catch (e) {}
  240. }
  241. }
  242. }
  243. </script>
  244. <style scoped>
  245. .detail-container { min-height: 100vh; background: #fff; }
  246. .loading-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  247. .loading-spinner { width: 60rpx; height: 60rpx; border: 4rpx solid #e0e0e0; border-top-color: #5B9BD5; border-radius: 50%; animation: spin 0.8s linear infinite; margin-bottom: 20rpx; }
  248. @keyframes spin { 0% { transform: rotate(0deg); } 360% { transform: rotate(360deg); } }
  249. .loading-text { font-size: 26rpx; color: #999; }
  250. .error-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  251. .error-icon { font-size: 100rpx; margin-bottom: 24rpx; }
  252. .error-text { font-size: 28rpx; color: #999; margin-bottom: 30rpx; }
  253. .retry-btn { width: 240rpx; height: 72rpx; line-height: 72rpx; background: #5B9BD5; color: #fff; font-size: 28rpx; border-radius: 36rpx; text-align: center; border: none; }
  254. .content-scroll { height: calc(100vh - 120rpx); }
  255. .detail-cover { width: 100%; display: block; }
  256. .detail-title { display: block; font-size: 36rpx; font-weight: bold; color: #333; line-height: 1.4; padding: 30rpx 30rpx 0; }
  257. .detail-meta { display: flex; align-items: center; padding: 16rpx 30rpx 0; font-size: 22rpx; color: #999; flex-wrap: wrap; }
  258. .meta-author { color: #5B9BD5; }
  259. .meta-sep { margin: 0 12rpx; color: #ddd; }
  260. .reading-time-badge { margin-left: auto; font-size: 20rpx; color: #5B9BD5; background: rgba(91,155,213,0.08); padding: 4rpx 12rpx; border-radius: 20rpx; white-space: nowrap; }
  261. .reading-time-badge.completed { color: #10B981; background: rgba(16,185,129,0.1); }
  262. .detail-category-row { padding: 16rpx 30rpx 0; }
  263. .detail-category { display: inline-block; font-size: 20rpx; color: #5B9BD5; background: rgba(91,155,213,0.1); padding: 4rpx 16rpx; border-radius: 8rpx; }
  264. .detail-dimensions { padding: 16rpx 30rpx 0; display: flex; gap: 12rpx; }
  265. .dim-bar-item { flex: 1; }
  266. .dim-bar { height: 8rpx; border-radius: 4rpx; }
  267. .dim-label { font-size: 18rpx; color: #999; text-align: center; display: block; margin-top: 4rpx; }
  268. .divider { height: 1rpx; background: #eee; margin: 24rpx 30rpx; }
  269. .detail-body { padding: 0 30rpx; font-size: 28rpx; color: #444; line-height: 1.8; }
  270. /* 评论区 */
  271. .comment-section { padding: 30rpx; border-top: 16rpx solid #F5F7FA; }
  272. .comment-header { margin-bottom: 20rpx; }
  273. .comment-title { font-size: 30rpx; font-weight: 700; color: #333; }
  274. .comment-input-row { display: flex; gap: 12rpx; margin-bottom: 24rpx; }
  275. .comment-input { flex: 1; border: 2rpx solid #E5E7EB; border-radius: 12rpx; padding: 16rpx 20rpx; font-size: 26rpx; height: 72rpx; box-sizing: border-box; }
  276. .comment-submit { width: 120rpx; height: 72rpx; line-height: 72rpx; background: #5B9BD5; color: #fff; font-size: 26rpx; border-radius: 12rpx; text-align: center; border: none; }
  277. .comment-empty { text-align: center; padding: 40rpx 0; color: #999; font-size: 26rpx; }
  278. .comment-item { padding: 20rpx 0; border-bottom: 2rpx solid #F5F5F5; }
  279. .comment-user { display: flex; align-items: center; margin-bottom: 8rpx; }
  280. .comment-avatar { width: 40rpx; height: 40rpx; border-radius: 50%; background: #5B9BD5; color: #fff; font-size: 20rpx; text-align: center; line-height: 40rpx; margin-right: 12rpx; }
  281. .comment-name { font-size: 26rpx; color: #333; font-weight: 500; }
  282. .comment-time { font-size: 20rpx; color: #999; margin-left: 12rpx; }
  283. .comment-content { font-size: 26rpx; color: #444; line-height: 1.5; padding-left: 52rpx; }
  284. .comment-actions { padding-left: 52rpx; margin-top: 8rpx; }
  285. .comment-reply-btn { font-size: 22rpx; color: #5B9BD5; }
  286. .reply-list { padding-left: 52rpx; margin-top: 12rpx; background: #FAFAFA; border-radius: 8rpx; padding: 12rpx; }
  287. .reply-item { padding: 8rpx 0; }
  288. .reply-user { display: flex; align-items: center; }
  289. .reply-avatar { width: 32rpx; height: 32rpx; border-radius: 50%; background: #94A3B8; color: #fff; font-size: 16rpx; text-align: center; line-height: 32rpx; margin-right: 8rpx; }
  290. .reply-name { font-size: 24rpx; color: #333; }
  291. .reply-time { font-size: 18rpx; color: #999; margin-left: 8rpx; }
  292. .reply-to { font-size: 18rpx; color: #5B9BD5; margin-left: 8rpx; }
  293. .reply-content { font-size: 24rpx; color: #444; padding-left: 40rpx; margin-top: 4rpx; }
  294. .detail-footer { position: fixed; bottom: 0; left: 0; right: 0; background: #fff; padding: 20rpx 30rpx; display: flex; align-items: center; gap: 16rpx; box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.06); z-index: 10; }
  295. .share-btn { height: 72rpx; line-height: 72rpx; background: #f5f5f5; color: #666; font-size: 24rpx; border-radius: 36rpx; padding: 0 24rpx; display: flex; align-items: center; border: none; }
  296. .share-btn::after { border: none; }
  297. .share-btn-icon { font-size: 28rpx; margin-right: 6rpx; }
  298. .share-btn-text { font-size: 24rpx; }
  299. /* 答题弹窗 */
  300. .quiz-mask, .result-mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 999; display: flex; align-items: center; justify-content: center; }
  301. .quiz-dialog, .result-dialog { background: #fff; border-radius: 24rpx; width: 650rpx; padding: 32rpx; }
  302. .quiz-mascot { display: flex; align-items: center; justify-content: center; gap: 12rpx; margin-bottom: 16rpx; }
  303. .mascot-icon { font-size: 60rpx; }
  304. .mascot-name { font-size: 32rpx; font-weight: 700; color: #F97316; }
  305. .quiz-intro { text-align: center; font-size: 28rpx; color: #666; margin-bottom: 24rpx; }
  306. .quiz-question { margin-bottom: 20rpx; }
  307. .q-title { font-size: 26rpx; font-weight: 600; color: #333; margin-bottom: 12rpx; }
  308. .q-options { display: flex; flex-direction: column; gap: 8rpx; }
  309. .q-option { padding: 14rpx 20rpx; border: 2rpx solid #E5E7EB; border-radius: 12rpx; font-size: 24rpx; color: #333; }
  310. .q-option.selected { border-color: #F97316; background: #FFF7ED; color: #92400E; }
  311. .quiz-submit, .result-btn { width: 100%; padding: 20rpx; background: linear-gradient(135deg, #F97316, #FB923C); color: #fff; font-size: 28rpx; border-radius: 12rpx; border: none; margin-top: 20rpx; }
  312. .result-dialog { text-align: center; }
  313. .result-icon { font-size: 80rpx; }
  314. .result-text { font-size: 32rpx; font-weight: 700; color: #333; margin: 16rpx 0; }
  315. .result-energy { font-size: 28rpx; color: #F97316; }
  316. </style>