article-detail.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template>
  2. <view class="detail-container">
  3. <!-- loading -->
  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. <template v-else-if="article">
  15. <!-- 文章内容区 -->
  16. <scroll-view scroll-y class="content-scroll" @scrolltolower="onScrollToBottom" v-if="!showQuiz && !showResult">
  17. <!-- 封面 -->
  18. <image v-if="article.coverImage" class="detail-cover" :src="article.coverImage" mode="widthFix" />
  19. <!-- 标题 -->
  20. <text class="detail-title">{{ article.title }}</text>
  21. <!-- 元信息 -->
  22. <view class="detail-meta">
  23. <text class="meta-author">{{ article.author || '浠艾福' }}</text>
  24. <text class="meta-sep">|</text>
  25. <text class="meta-date">{{ formatDate(article.publishedAt) }}</text>
  26. <text class="meta-sep">|</text>
  27. <text class="meta-readtime">{{ article.readTime || 3 }}分钟阅读</text>
  28. </view>
  29. <!-- 分类 -->
  30. <view class="detail-category-row">
  31. <text class="detail-category">{{ article.categoryName || '' }}</text>
  32. </view>
  33. <!-- 五维权重彩条 -->
  34. <view v-if="article.relatedDimensions" class="detail-dimensions">
  35. <view v-for="dim in parseDimensions(article)" :key="dim.code" class="dim-bar-item">
  36. <view class="dim-bar" :style="{ background: dim.color, width: dim.weight + '%' }"></view>
  37. <text class="dim-label">{{ dim.name }}</text>
  38. </view>
  39. </view>
  40. <!-- 分割线 -->
  41. <view class="divider"></view>
  42. <!-- 正文 -->
  43. <view class="detail-body">
  44. <rich-text :nodes="article.content"></rich-text>
  45. </view>
  46. <!-- 底部占位 -->
  47. <view style="height: 160rpx;"></view>
  48. </scroll-view>
  49. <!-- 阅读完成 -->
  50. <view v-if="!showQuiz && !showResult" class="detail-footer">
  51. <button class="share-btn" open-type="share" @click="onShareTap">
  52. <text class="share-btn-icon">📤</text>
  53. <text class="share-btn-text">分享</text>
  54. </button>
  55. <button
  56. class="read-btn"
  57. @click="onReadComplete"
  58. >阅读完成</button>
  59. </view>
  60. <!-- AI 答题界面 -->
  61. <view v-if="showQuiz" class="quiz-container">
  62. <view class="quiz-header">
  63. <text class="quiz-title">阅读小测验</text>
  64. <text class="quiz-desc">回答以下问题,巩固阅读收获</text>
  65. </view>
  66. <view v-if="quizLoading" class="quiz-loading">
  67. <view class="loading-spinner"></view>
  68. <text class="quiz-loading-text">AI 正在根据你的情况出题...</text>
  69. </view>
  70. <view v-else-if="quizError" class="quiz-error">
  71. <text class="quiz-error-text">{{ quizErrorMsg }}</text>
  72. <button class="retry-btn" @click="loadQuiz">重新出题</button>
  73. </view>
  74. <template v-else-if="quizQuestions.length > 0">
  75. <view v-for="(q, idx) in quizQuestions" :key="idx" class="quiz-question">
  76. <text class="q-title">第{{ idx + 1 }}题</text>
  77. <text class="q-text">{{ q.question }}</text>
  78. <view
  79. v-for="(opt, optIdx) in q.options"
  80. :key="optIdx"
  81. :class="['q-option', selectedAnswers[idx] === getOptionLetter(optIdx) ? 'selected' : '']"
  82. @click="selectAnswer(idx, getOptionLetter(optIdx))"
  83. >
  84. <text class="q-option-letter">{{ getOptionLetter(optIdx) }}</text>
  85. <text class="q-option-text">{{ getOptionText(opt) }}</text>
  86. </view>
  87. </view>
  88. <button
  89. class="submit-btn"
  90. :disabled="!canSubmit"
  91. @click="onSubmitQuiz"
  92. >提交答案</button>
  93. </template>
  94. </view>
  95. <!-- 答题结果 -->
  96. <view v-if="showResult" class="result-container">
  97. <view class="result-card">
  98. <text class="result-icon">{{ resultScore === resultTotal ? '🎉' : '💪' }}</text>
  99. <text class="result-title">{{ resultScore === resultTotal ? '全部答对!' : '继续加油!' }}</text>
  100. <text class="result-score">{{ resultScore }} / {{ resultTotal }}</text>
  101. <text v-if="resultPoints > 0" class="result-points">+{{ resultPoints }} 积分</text>
  102. </view>
  103. <button class="back-btn" @click="goBack">返回文章列表</button>
  104. </view>
  105. </template>
  106. </view>
  107. </template>
  108. <script>
  109. import { getArticleDetail, getAiQuestions, submitAnswers, recordArticleRead } from '@/utils/api.js'
  110. import shareMixin from '../../components/share-mixin.js'
  111. export default {
  112. mixins: [shareMixin],
  113. data() {
  114. return {
  115. articleId: '',
  116. article: null,
  117. loading: true,
  118. error: false,
  119. errorMsg: '',
  120. readSeconds: 0,
  121. readTimer: null,
  122. showQuiz: false,
  123. quizLoading: false,
  124. quizError: false,
  125. quizErrorMsg: '',
  126. quizQuestions: [],
  127. quizRecordId: null,
  128. selectedAnswers: [],
  129. showResult: false,
  130. resultScore: 0,
  131. resultTotal: 0,
  132. resultPoints: 0
  133. }
  134. },
  135. computed: {
  136. canSubmit: function() {
  137. if (this.quizQuestions.length === 0) return false
  138. for (var i = 0; i < this.quizQuestions.length; i++) {
  139. if (!this.selectedAnswers[i]) return false
  140. }
  141. return true
  142. }
  143. },
  144. onLoad(options) {
  145. if (options && options.id) {
  146. this.articleId = options.id
  147. this.loadDetail(options.id)
  148. this.startTimer()
  149. } else {
  150. this.error = true
  151. this.errorMsg = '参数错误'
  152. this.loading = false
  153. }
  154. },
  155. onUnload: function() {
  156. this.stopTimer()
  157. },
  158. methods: {
  159. async loadDetail(id) {
  160. this.loading = true
  161. this.error = false
  162. try {
  163. var res = await getArticleDetail({ id: id })
  164. if (res.code === 200 && res.data) {
  165. this.article = res.data
  166. this.setShareInfo('推荐阅读: ' + (res.data.title || ''), '/pages/article-center/article-detail?id=' + id)
  167. } else {
  168. this.error = true
  169. this.errorMsg = '文章不存在或无权限查看'
  170. }
  171. } catch (e) {
  172. this.error = true
  173. this.errorMsg = '加载失败,请稍后重试'
  174. } finally {
  175. this.loading = false
  176. }
  177. },
  178. startTimer: function() {
  179. var self = this
  180. this.readTimer = setInterval(function() {
  181. self.readSeconds++
  182. }, 1000)
  183. },
  184. stopTimer: function() {
  185. if (this.readTimer) {
  186. clearInterval(this.readTimer)
  187. this.readTimer = null
  188. }
  189. },
  190. formatDate(dateStr) {
  191. if (!dateStr) return ''
  192. return dateStr.slice(0, 10)
  193. },
  194. parseDimensions: function(article) {
  195. var dimMap = {
  196. body: { code: 'body', color: '#FF8C42', name: '身' },
  197. mind: { code: 'mind', color: '#FF6B9D', name: '心' },
  198. wisdom: { code: 'wisdom', color: '#6366F1', name: '智' },
  199. action: { code: 'action', color: '#10B981', name: '行' },
  200. wealth: { code: 'wealth', color: '#F59E0B', name: '富' }
  201. }
  202. var raw = article && article.relatedDimensions
  203. ? article.relatedDimensions.split(',').map(function(s) { return s.trim().toLowerCase() })
  204. : []
  205. var weights = null
  206. if (article && article.dimensionWeights) {
  207. try {
  208. var w = JSON.parse(article.dimensionWeights)
  209. if (w && typeof w === 'object') weights = w
  210. } catch (e) {}
  211. }
  212. if (!weights) {
  213. var n = raw.filter(function(c) { return dimMap[c] }).length
  214. if (n > 0) {
  215. var base = Math.floor(100 / n)
  216. var rem = 100 - base * n
  217. weights = {}
  218. var i = 0
  219. raw.forEach(function(c) {
  220. if (!dimMap[c]) return
  221. weights[c] = i < rem ? base + 1 : base
  222. i++
  223. })
  224. }
  225. }
  226. return raw.filter(function(c) { return dimMap[c] }).map(function(c) {
  227. return {
  228. code: dimMap[c].code,
  229. name: dimMap[c].name,
  230. color: dimMap[c].color,
  231. weight: (weights && weights[c]) ? weights[c] : 20
  232. }
  233. })
  234. },
  235. getOptionLetter(idx) {
  236. return String.fromCharCode(65 + idx)
  237. },
  238. getOptionText(opt) {
  239. if (!opt) return ''
  240. // Remove "A. ", "B. " prefix for display
  241. var match = opt.match(/^[A-D][.、]\s*/);
  242. return match ? opt.substring(match[0].length) : opt
  243. },
  244. selectAnswer(qIdx, letter) {
  245. this.selectedAnswers[qIdx] = letter
  246. this.$forceUpdate()
  247. },
  248. onScrollToBottom() {
  249. // 滚动到底部
  250. },
  251. async onReadComplete() {
  252. // Stop timer and record the reading
  253. this.stopTimer()
  254. // Record read with duration (only meaningful if childId present)
  255. var childId = uni.getStorageSync('currentChildId')
  256. var userId = uni.getStorageSync('userId')
  257. if (childId && userId && this.article && this.article.id) {
  258. try {
  259. var readRes = await recordArticleRead({
  260. id: this.article.id,
  261. userId: userId,
  262. childId: childId,
  263. durationSeconds: this.readSeconds
  264. })
  265. if (readRes && readRes.code === 200 && readRes.data) {
  266. var earned = readRes.data.pointsEarned || 0
  267. if (earned > 0) {
  268. uni.showToast({ title: '阅读完成,获得 ' + earned + ' 积分', icon: 'success' })
  269. } else if (readRes.data.alreadyCompleted) {
  270. uni.showToast({ title: '已计过积分啦', icon: 'none' })
  271. } else if (readRes.data.belowThreshold) {
  272. var minSec = readRes.data.minSeconds || 60
  273. uni.showToast({ title: '阅读需满' + minSec + '秒计入积分', icon: 'none' })
  274. }
  275. }
  276. } catch (e) {
  277. // silent fail - reading still counted
  278. }
  279. }
  280. this.showQuiz = true
  281. this.loadQuiz()
  282. },
  283. async loadQuiz() {
  284. this.quizLoading = true
  285. this.quizError = false
  286. try {
  287. var childId = uni.getStorageSync('currentChildId')
  288. var res = await getAiQuestions({
  289. articleId: this.article.id,
  290. childId: childId || undefined
  291. })
  292. if (res.code === 200 && res.data) {
  293. this.quizRecordId = res.data.recordId
  294. var questions = res.data.questions
  295. if (typeof questions === 'string') {
  296. questions = JSON.parse(questions)
  297. }
  298. this.quizQuestions = Array.isArray(questions) ? questions : []
  299. this.selectedAnswers = new Array(this.quizQuestions.length).fill(null)
  300. } else {
  301. this.quizError = true
  302. this.quizErrorMsg = '出题失败,请重试'
  303. }
  304. } catch (e) {
  305. this.quizError = true
  306. this.quizErrorMsg = '网络错误,请重试'
  307. } finally {
  308. this.quizLoading = false
  309. }
  310. },
  311. async onSubmitQuiz() {
  312. if (!this.canSubmit) return
  313. var answers = this.selectedAnswers.map(function(selected, idx) {
  314. return { questionIndex: idx, selected: selected }
  315. })
  316. try {
  317. var childId = uni.getStorageSync('currentChildId')
  318. var res = await submitAnswers({
  319. recordId: this.quizRecordId,
  320. answers: JSON.stringify(answers),
  321. childId: childId || undefined
  322. })
  323. if (res.code === 200 && res.data) {
  324. this.resultScore = res.data.score
  325. this.resultTotal = res.data.totalQuestions
  326. this.resultPoints = res.data.pointsEarned
  327. this.showResult = true
  328. this.showQuiz = false
  329. } else {
  330. uni.showToast({ title: res.message || '提交失败', icon: 'none' })
  331. }
  332. } catch (e) {
  333. uni.showToast({ title: '提交失败', icon: 'none' })
  334. }
  335. },
  336. goBack() {
  337. uni.navigateBack()
  338. }
  339. }
  340. }
  341. </script>
  342. <style scoped>
  343. .detail-container { min-height: 100vh; background: #fff; }
  344. .loading-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  345. .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; }
  346. @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
  347. .loading-text { font-size: 26rpx; color: #999; }
  348. .error-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  349. .error-icon { font-size: 100rpx; margin-bottom: 24rpx; }
  350. .error-text { font-size: 28rpx; color: #999; margin-bottom: 30rpx; }
  351. .retry-btn { width: 240rpx; height: 72rpx; line-height: 72rpx; background: #5B9BD5; color: #fff; font-size: 28rpx; border-radius: 36rpx; text-align: center; border: none; }
  352. .retry-btn::after { border: none; }
  353. .content-scroll { height: calc(100vh - 120rpx); }
  354. .detail-cover { width: 100%; display: block; }
  355. .detail-title { display: block; font-size: 36rpx; font-weight: bold; color: #333; line-height: 1.4; padding: 30rpx 30rpx 0; }
  356. .detail-meta { display: flex; align-items: center; padding: 16rpx 30rpx 0; font-size: 22rpx; color: #999; }
  357. .meta-author { color: #5B9BD5; }
  358. .meta-sep { margin: 0 12rpx; color: #ddd; }
  359. .detail-category-row { padding: 16rpx 30rpx 0; }
  360. .detail-category { display: inline-block; font-size: 20rpx; color: #5B9BD5; background: rgba(91,155,213,0.1); padding: 4rpx 16rpx; border-radius: 8rpx; }
  361. .detail-dimensions { padding: 16rpx 30rpx 0; display: flex; gap: 12rpx; }
  362. .dim-bar-item { flex: 1; }
  363. .dim-bar { height: 8rpx; border-radius: 4rpx; }
  364. .dim-label { font-size: 18rpx; color: #999; text-align: center; display: block; margin-top: 4rpx; }
  365. .divider { height: 1rpx; background: #eee; margin: 24rpx 30rpx; }
  366. .detail-body { padding: 0 30rpx; font-size: 28rpx; color: #444; line-height: 1.8; }
  367. .detail-body rich-text { word-break: break-word; }
  368. /* 底部按钮 */
  369. .detail-footer { position: fixed; bottom: 0; left: 0; right: 0; background: #fff; padding: 20rpx 30rpx; display: flex; align-items: center; box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.06); z-index: 10; }
  370. .read-btn { flex: 1; height: 80rpx; line-height: 80rpx; background: linear-gradient(135deg, #5B9BD5, #3A7CC4); color: #fff; font-size: 30rpx; font-weight: bold; border-radius: 40rpx; text-align: center; border: none; }
  371. .read-btn::after { border: none; }
  372. .share-btn { height: 72rpx; line-height: 72rpx; background: #f5f5f5; color: #666; font-size: 24rpx; border-radius: 36rpx; padding: 0 24rpx; margin-right: 16rpx; display: flex; align-items: center; border: none; flex-shrink: 0; }
  373. .share-btn::after { border: none; }
  374. .share-btn-icon { font-size: 28rpx; margin-right: 6rpx; }
  375. .share-btn-text { font-size: 24rpx; }
  376. /* 答题区 */
  377. .quiz-container { padding: 30rpx; }
  378. .quiz-header { text-align: center; margin-bottom: 40rpx; }
  379. .quiz-title { font-size: 34rpx; font-weight: bold; color: #333; display: block; }
  380. .quiz-desc { font-size: 24rpx; color: #999; margin-top: 10rpx; display: block; }
  381. .quiz-loading { display: flex; flex-direction: column; align-items: center; padding-top: 100rpx; }
  382. .quiz-loading-text { font-size: 26rpx; color: #999; margin-top: 20rpx; }
  383. .quiz-error { display: flex; flex-direction: column; align-items: center; padding-top: 100rpx; }
  384. .quiz-error-text { font-size: 26rpx; color: #999; margin-bottom: 20rpx; }
  385. .quiz-question { margin-bottom: 40rpx; }
  386. .q-title { font-size: 24rpx; color: #5B9BD5; font-weight: bold; display: block; margin-bottom: 12rpx; }
  387. .q-text { font-size: 30rpx; color: #333; line-height: 1.5; display: block; margin-bottom: 20rpx; }
  388. .q-option { display: flex; align-items: center; padding: 20rpx; background: #f5f7fa; border-radius: 12rpx; margin-bottom: 12rpx; border: 2rpx solid transparent; }
  389. .q-option.selected { background: #E8F4FD; border-color: #5B9BD5; }
  390. .q-option-letter { width: 40rpx; height: 40rpx; line-height: 40rpx; text-align: center; background: #ddd; color: #fff; border-radius: 50%; font-size: 22rpx; font-weight: bold; margin-right: 16rpx; flex-shrink: 0; }
  391. .q-option.selected .q-option-letter { background: #5B9BD5; }
  392. .q-option-text { font-size: 26rpx; color: #333; }
  393. .submit-btn { width: 100%; height: 88rpx; line-height: 88rpx; background: linear-gradient(135deg, #F97316, #EA580C); color: #fff; font-size: 32rpx; font-weight: bold; border-radius: 44rpx; text-align: center; border: none; margin-top: 20rpx; }
  394. .submit-btn[disabled] { opacity: 0.4; }
  395. .submit-btn::after { border: none; }
  396. /* 结果页 */
  397. .result-container { display: flex; flex-direction: column; align-items: center; padding: 120rpx 30rpx; }
  398. .result-card { background: #fff; border-radius: 24rpx; padding: 60rpx 80rpx; text-align: center; box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.08); margin-bottom: 60rpx; }
  399. .result-icon { font-size: 100rpx; display: block; margin-bottom: 20rpx; }
  400. .result-title { font-size: 32rpx; font-weight: bold; color: #333; display: block; margin-bottom: 16rpx; }
  401. .result-score { font-size: 48rpx; font-weight: bold; color: #5B9BD5; display: block; margin-bottom: 12rpx; }
  402. .result-points { font-size: 36rpx; color: #F97316; font-weight: bold; display: block; }
  403. .back-btn { width: 60%; height: 80rpx; line-height: 80rpx; background: #5B9BD5; color: #fff; font-size: 30rpx; border-radius: 40rpx; text-align: center; border: none; }
  404. .back-btn::after { border: none; }
  405. </style>