article-detail.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 style="height: 200rpx;"></view>
  38. </scroll-view>
  39. <!-- 底部 -->
  40. <view class="detail-footer">
  41. <view class="share-btn" @click="generatePoster">
  42. <text class="share-btn-icon">🖼</text>
  43. </view>
  44. </view>
  45. </template>
  46. <ContentSharePoster :show="showPoster" :title="article && article.title" :coverImage="article && article.coverImage" :qrCodeBase64="posterQrCode" typeLabel="好文推荐" :dimensionCode="posterDimension" :summary="posterSummary" :inviteText="posterInviteText" @close="closePoster" />
  47. <!-- 浠宝答题弹窗 -->
  48. <view class="quiz-mask" v-if="showQuiz" @click="closeQuiz">
  49. <view class="quiz-dialog" @click.stop>
  50. <view class="quiz-mascot">
  51. <text class="mascot-icon">{{ mascotIcon }}</text>
  52. <text class="mascot-name">{{ mascotName }}</text>
  53. </view>
  54. <text class="quiz-intro">阅读完成!考考你三个问题~</text>
  55. <view class="quiz-question" v-for="(q, qi) in quizQuestions" :key="qi">
  56. <text class="q-title">问题{{ qi+1 }}: {{ q.question }}</text>
  57. <view class="q-options">
  58. <text v-for="(opt, oi) in q.options" :key="oi" class="q-option" :class="{ selected: quizAnswers[qi] === String.fromCharCode(65 + oi) }" @click="selectQuizAnswer(qi, oi)">{{ opt }}</text>
  59. </view>
  60. </view>
  61. <button class="quiz-submit" @click="submitQuiz">提交答案</button>
  62. </view>
  63. </view>
  64. <!-- 答题结果 -->
  65. <view class="result-mask" v-if="showResult">
  66. <view class="result-dialog">
  67. <text class="result-icon">{{ resultIcon }}</text>
  68. <text class="result-text">答对 {{ quizResult.correctCount }}/{{ quizResult.totalQuestions }} 题</text>
  69. <text class="result-energy">获得 {{ quizResult.earnedEnergy }} 能量 ⚡</text>
  70. <button class="result-btn" @click="closeResult">知道了</button>
  71. </view>
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. import { getArticleDetail, reportReadingTime, getShareQrCode, completeArticleRead, generateQuiz, submitQuiz } from '@/utils/api.js'
  77. import shareMixin from '../../components/share-mixin.js'
  78. import ContentSharePoster from '@/components/ContentSharePoster.vue'
  79. export default {
  80. mixins: [shareMixin],
  81. components: { ContentSharePoster },
  82. data() {
  83. return {
  84. articleId: '', article: null, loading: true, error: false, errorMsg: '',
  85. readingSeconds: 0, lastReportedSeconds: 0, readingCompleted: false,
  86. isTimerRunning: false, timerHandle: null, syncHandle: null,
  87. showPoster: false, posterQrCode: '',
  88. posterDimension: '', posterSummary: '', posterInviteText: '',
  89. // 答题
  90. showQuiz: false, quizQuestions: [], quizAnswers: [], showResult: false, quizResult: {},
  91. mascotList: [{ icon: '🐶', name: '浠宝' }, { icon: '🐼', name: '福宝' }],
  92. mascotIcon: '🐶', mascotName: '浠宝'
  93. }
  94. },
  95. computed: {},
  96. onLoad(options) {
  97. this.mascotIcon = this.mascotList[Math.floor(Math.random() * 2)].icon
  98. this.mascotName = this.mascotList[Math.floor(Math.random() * 2)].name
  99. if (options && options.id) { this.articleId = options.id; this.loadDetail(options.id) }
  100. else { this.error = true; this.errorMsg = '参数错误'; this.loading = false }
  101. },
  102. onShow() { if (this.article && !this.error) this.startNewReadingSession() },
  103. onHide() { this.pauseReadingTimer() },
  104. onUnload() { this.pauseReadingTimer() },
  105. methods: {
  106. async loadDetail(id) {
  107. this.loading = true; this.error = false
  108. try {
  109. var res = await getArticleDetail({ id: id })
  110. if (res.code === 200 && res.data) {
  111. this.article = res.data
  112. this.setShareInfo('推荐阅读: ' + (res.data.title || ''), '/pages/article-center/article-detail?id=' + id)
  113. this.startReadingTimer()
  114. } else { this.error = true; this.errorMsg = '文章不存在或无权限查看' }
  115. } catch (e) { this.error = true; this.errorMsg = '加载失败' }
  116. finally { this.loading = false }
  117. },
  118. formatDate(d) { return d ? d.slice(0, 10) : '' },
  119. parseDimensions: function(article) {
  120. 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: '富' } }
  121. var raw = article && article.relatedDimensions ? article.relatedDimensions.split(',').map(function(s) { return s.trim().toLowerCase() }) : []
  122. var weights = null
  123. if (article && article.dimensionWeights) try { var w = JSON.parse(article.dimensionWeights); if (w && typeof w === 'object') weights = w } catch (e) {}
  124. if (!weights) {
  125. var n = raw.filter(function(c) { return dimMap[c] }).length
  126. 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++ }) }
  127. }
  128. 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 } })
  129. },
  130. getMainDimension: function() {
  131. var dims = this.article ? this.parseDimensions(this.article) : []
  132. var best = null
  133. var i
  134. for (i = 0; i < dims.length; i++) {
  135. if (!best || dims[i].weight > best.weight) best = dims[i]
  136. }
  137. return best ? best.code : ''
  138. },
  139. onScrollToBottom() {},
  140. startNewReadingSession: function() {
  141. // 每次进入文章详情页都从 0 开始新会话,时长不延续上一次
  142. this.readingSeconds = 0
  143. this.lastReportedSeconds = 0
  144. this.startReadingTimer()
  145. },
  146. startReadingTimer: function() {
  147. if (this.isTimerRunning || this.readingCompleted) return
  148. this.isTimerRunning = true
  149. var self = this
  150. this.timerHandle = setInterval(function() { self.readingSeconds++ }, 1000)
  151. // 每 10 秒自动记录一次该用户已读时长
  152. this.syncHandle = setInterval(function() { self.reportReadingTime() }, 10000)
  153. },
  154. pauseReadingTimer: function() {
  155. if (!this.isTimerRunning) return
  156. this.isTimerRunning = false
  157. if (this.timerHandle) { clearInterval(this.timerHandle); this.timerHandle = null }
  158. if (this.syncHandle) { clearInterval(this.syncHandle); this.syncHandle = null }
  159. this.reportReadingTime()
  160. },
  161. async reportReadingTime() {
  162. var delta = this.readingSeconds - this.lastReportedSeconds
  163. if (delta <= 0) return
  164. this.lastReportedSeconds = this.readingSeconds
  165. var childId = uni.getStorageSync('currentChildId')
  166. if (!childId || !this.article) return
  167. reportReadingTime({ articleId: this.article.id, childId: parseInt(childId), durationSeconds: delta })
  168. // 达到阅读时长自动完成
  169. var targetSeconds = (this.article.readTime || 3) * 60
  170. if (!this.readingCompleted && this.readingSeconds >= targetSeconds) {
  171. this.readingCompleted = true
  172. this.pauseReadingTimer()
  173. try { await completeArticleRead({ articleId: this.article.id, childId: parseInt(childId), durationSeconds: this.readingSeconds }) } catch (e) {}
  174. uni.showToast({ title: '阅读完成 +5能量', icon: 'success' })
  175. // 弹出答题
  176. this.startQuiz()
  177. }
  178. },
  179. formatReadingTime: function(seconds) {
  180. if (seconds < 60) return '已读 ' + seconds + '秒'
  181. var min = Math.floor(seconds / 60); var sec = seconds % 60
  182. return '已读 ' + min + '分' + (sec > 0 ? sec + '秒' : '')
  183. },
  184. async generatePoster() {
  185. if (!this.articleId) return
  186. uni.showLoading({ title: '生成海报中...' })
  187. try {
  188. var res = await getShareQrCode('pages/article-center/article-detail', 'id=' + this.articleId)
  189. if (res && res.data) {
  190. this.posterQrCode = res.data.qrCodeBase64 || ''
  191. this.posterDimension = this.getMainDimension()
  192. this.posterSummary = (this.article && this.article.summary) || ''
  193. this.posterInviteText = '「' + (uni.getStorageSync('nickname') || '好友') + '」邀请你一起阅读这篇文章'
  194. this.showPoster = true
  195. }
  196. } catch (e) { uni.showToast({ title: '生成失败', icon: 'none' }) }
  197. finally { uni.hideLoading() }
  198. },
  199. // 答题
  200. async startQuiz() {
  201. try {
  202. var res = await generateQuiz({ articleId: this.articleId })
  203. if (res.code === 200 && res.data && res.data.length > 0) {
  204. this.quizQuestions = res.data
  205. this.quizAnswers = []
  206. this.showQuiz = true
  207. }
  208. } catch (e) {}
  209. },
  210. async submitQuiz() {
  211. var correct = 0
  212. for (var i = 0; i < this.quizQuestions.length; i++) {
  213. if (this.quizAnswers[i] === this.quizQuestions[i].answer) correct++
  214. }
  215. var childId = uni.getStorageSync('currentChildId')
  216. try {
  217. var res = await submitQuiz({ correctCount: correct, childId: parseInt(childId || 0) })
  218. if (res.code === 200) {
  219. this.quizResult = res.data || { correctCount: correct, totalQuestions: 3, earnedEnergy: correct * 5 }
  220. this.showQuiz = false
  221. this.showResult = true
  222. }
  223. } catch (e) {}
  224. },
  225. closePoster: function() {
  226. this.showPoster = false
  227. },
  228. closeQuiz: function() {
  229. this.showQuiz = false
  230. },
  231. closeResult: function() {
  232. this.showResult = false
  233. },
  234. selectQuizAnswer: function(qi, oi) {
  235. this.quizAnswers[qi] = String.fromCharCode(65 + oi)
  236. }
  237. }
  238. }
  239. </script>
  240. <style scoped>
  241. .detail-container { min-height: 100vh; background: #fff; }
  242. .loading-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  243. .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; }
  244. @keyframes spin { 0% { transform: rotate(0deg); } 360% { transform: rotate(360deg); } }
  245. .loading-text { font-size: 26rpx; color: #999; }
  246. .error-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  247. .error-icon { font-size: 100rpx; margin-bottom: 24rpx; }
  248. .error-text { font-size: 28rpx; color: #999; margin-bottom: 30rpx; }
  249. .retry-btn { width: 240rpx; height: 72rpx; line-height: 72rpx; background: #5B9BD5; color: #fff; font-size: 28rpx; border-radius: 36rpx; text-align: center; border: none; }
  250. .content-scroll { height: calc(100vh - 120rpx); }
  251. .detail-cover { width: 100%; display: block; }
  252. .detail-title { display: block; font-size: 36rpx; font-weight: bold; color: #333; line-height: 1.4; padding: 30rpx 30rpx 0; }
  253. .detail-meta { display: flex; align-items: center; padding: 16rpx 30rpx 0; font-size: 22rpx; color: #999; flex-wrap: wrap; }
  254. .meta-author { color: #5B9BD5; }
  255. .meta-sep { margin: 0 12rpx; color: #ddd; }
  256. .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; }
  257. .reading-time-badge.completed { color: #10B981; background: rgba(16,185,129,0.1); }
  258. .detail-category-row { padding: 16rpx 30rpx 0; }
  259. .detail-category { display: inline-block; font-size: 20rpx; color: #5B9BD5; background: rgba(91,155,213,0.1); padding: 4rpx 16rpx; border-radius: 8rpx; }
  260. .detail-dimensions { padding: 16rpx 30rpx 0; display: flex; gap: 12rpx; }
  261. .dim-bar-item { flex: 1; }
  262. .dim-bar { height: 8rpx; border-radius: 4rpx; }
  263. .dim-label { font-size: 18rpx; color: #999; text-align: center; display: block; margin-top: 4rpx; }
  264. .divider { height: 1rpx; background: #eee; margin: 24rpx 30rpx; }
  265. .detail-body { padding: 0 30rpx; font-size: 28rpx; color: #444; line-height: 1.8; }
  266. .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; }
  267. .share-btn { height: 72rpx; width: 72rpx; background: #f5f5f5; border-radius: 50%; display: flex; align-items: center; justify-content: center; border: none; }
  268. .share-btn::after { border: none; }
  269. .share-btn-icon { font-size: 32rpx; }
  270. /* 答题弹窗 */
  271. .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; }
  272. .quiz-dialog, .result-dialog { background: #fff; border-radius: 24rpx; width: 650rpx; padding: 32rpx; }
  273. .quiz-mascot { display: flex; align-items: center; justify-content: center; gap: 12rpx; margin-bottom: 16rpx; }
  274. .mascot-icon { font-size: 60rpx; }
  275. .mascot-name { font-size: 32rpx; font-weight: 700; color: #F97316; }
  276. .quiz-intro { text-align: center; font-size: 28rpx; color: #666; margin-bottom: 24rpx; }
  277. .quiz-question { margin-bottom: 20rpx; }
  278. .q-title { font-size: 26rpx; font-weight: 600; color: #333; margin-bottom: 12rpx; }
  279. .q-options { display: flex; flex-direction: column; gap: 8rpx; }
  280. .q-option { padding: 14rpx 20rpx; border: 2rpx solid #E5E7EB; border-radius: 12rpx; font-size: 24rpx; color: #333; }
  281. .q-option.selected { border-color: #F97316; background: #FFF7ED; color: #92400E; }
  282. .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; }
  283. .result-dialog { text-align: center; }
  284. .result-icon { font-size: 80rpx; }
  285. .result-text { font-size: 32rpx; font-weight: 700; color: #333; margin: 16rpx 0; }
  286. .result-energy { font-size: 28rpx; color: #F97316; }
  287. </style>