article-detail.vue 20 KB

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