article-detail.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.relatedDimensions)" :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
  52. class="read-btn"
  53. @click="onReadComplete"
  54. >阅读完成</button>
  55. </view>
  56. <!-- AI 答题界面 -->
  57. <view v-if="showQuiz" class="quiz-container">
  58. <view class="quiz-header">
  59. <text class="quiz-title">阅读小测验</text>
  60. <text class="quiz-desc">回答以下问题,巩固阅读收获</text>
  61. </view>
  62. <view v-if="quizLoading" class="quiz-loading">
  63. <view class="loading-spinner"></view>
  64. <text class="quiz-loading-text">AI 正在根据你的情况出题...</text>
  65. </view>
  66. <view v-else-if="quizError" class="quiz-error">
  67. <text class="quiz-error-text">{{ quizErrorMsg }}</text>
  68. <button class="retry-btn" @click="loadQuiz">重新出题</button>
  69. </view>
  70. <template v-else-if="quizQuestions.length > 0">
  71. <view v-for="(q, idx) in quizQuestions" :key="idx" class="quiz-question">
  72. <text class="q-title">第{{ idx + 1 }}题</text>
  73. <text class="q-text">{{ q.question }}</text>
  74. <view
  75. v-for="(opt, optIdx) in q.options"
  76. :key="optIdx"
  77. :class="['q-option', selectedAnswers[idx] === getOptionLetter(optIdx) ? 'selected' : '']"
  78. @click="selectAnswer(idx, getOptionLetter(optIdx))"
  79. >
  80. <text class="q-option-letter">{{ getOptionLetter(optIdx) }}</text>
  81. <text class="q-option-text">{{ getOptionText(opt) }}</text>
  82. </view>
  83. </view>
  84. <button
  85. class="submit-btn"
  86. :disabled="!canSubmit"
  87. @click="onSubmitQuiz"
  88. >提交答案</button>
  89. </template>
  90. </view>
  91. <!-- 答题结果 -->
  92. <view v-if="showResult" class="result-container">
  93. <view class="result-card">
  94. <text class="result-icon">{{ resultScore === resultTotal ? '🎉' : '💪' }}</text>
  95. <text class="result-title">{{ resultScore === resultTotal ? '全部答对!' : '继续加油!' }}</text>
  96. <text class="result-score">{{ resultScore }} / {{ resultTotal }}</text>
  97. <text v-if="resultPoints > 0" class="result-points">+{{ resultPoints }} 积分</text>
  98. </view>
  99. <button class="back-btn" @click="goBack">返回文章列表</button>
  100. </view>
  101. </template>
  102. </view>
  103. </template>
  104. <script>
  105. import { getArticleDetail, getAiQuestions, submitAnswers } from '@/utils/api.js'
  106. export default {
  107. data() {
  108. return {
  109. articleId: '',
  110. article: null,
  111. loading: true,
  112. error: false,
  113. errorMsg: '',
  114. // timer removed
  115. showQuiz: false,
  116. quizLoading: false,
  117. quizError: false,
  118. quizErrorMsg: '',
  119. quizQuestions: [],
  120. quizRecordId: null,
  121. selectedAnswers: [],
  122. showResult: false,
  123. resultScore: 0,
  124. resultTotal: 0,
  125. resultPoints: 0
  126. }
  127. },
  128. computed: {
  129. canSubmit: function() {
  130. if (this.quizQuestions.length === 0) return false
  131. for (var i = 0; i < this.quizQuestions.length; i++) {
  132. if (!this.selectedAnswers[i]) return false
  133. }
  134. return true
  135. }
  136. },
  137. onLoad(options) {
  138. if (options && options.id) {
  139. this.articleId = options.id
  140. this.loadDetail(options.id)
  141. } else {
  142. this.error = true
  143. this.errorMsg = '参数错误'
  144. this.loading = false
  145. }
  146. },
  147. onUnload() {
  148. },
  149. methods: {
  150. async loadDetail(id) {
  151. this.loading = true
  152. this.error = false
  153. try {
  154. var res = await getArticleDetail({ id: id })
  155. if (res.code === 200 && res.data) {
  156. this.article = res.data
  157. } else {
  158. this.error = true
  159. this.errorMsg = '文章不存在或无权限查看'
  160. }
  161. } catch (e) {
  162. this.error = true
  163. this.errorMsg = '加载失败,请稍后重试'
  164. } finally {
  165. this.loading = false
  166. }
  167. },
  168. // timer methods removed
  169. formatDate(dateStr) {
  170. if (!dateStr) return ''
  171. return dateStr.slice(0, 10)
  172. },
  173. parseDimensions(str) {
  174. if (!str) return []
  175. var dimMap = {
  176. body: { code: 'body', color: '#FF8C42', name: '身', weight: 20 },
  177. mind: { code: 'mind', color: '#6366F1', name: '智', weight: 20 },
  178. wisdom: { code: 'wisdom', color: '#FF6B9D', name: '心', weight: 20 },
  179. action: { code: 'action', color: '#10B981', name: '行', weight: 20 },
  180. wealth: { code: 'wealth', color: '#F59E0B', name: '富', weight: 20 }
  181. }
  182. var codes = str.split(',').map(function(s) { return s.trim().toLowerCase() })
  183. return codes.filter(function(c) { return dimMap[c] }).map(function(c) { return dimMap[c] })
  184. },
  185. getOptionLetter(idx) {
  186. return String.fromCharCode(65 + idx)
  187. },
  188. getOptionText(opt) {
  189. if (!opt) return ''
  190. // Remove "A. ", "B. " prefix for display
  191. var match = opt.match(/^[A-D][.、]\s*/);
  192. return match ? opt.substring(match[0].length) : opt
  193. },
  194. selectAnswer(qIdx, letter) {
  195. this.selectedAnswers[qIdx] = letter
  196. this.$forceUpdate()
  197. },
  198. onScrollToBottom() {
  199. // 滚动到底部
  200. },
  201. async onReadComplete() {
  202. // 计时器已移除,直接进入答题
  203. this.showQuiz = true
  204. this.loadQuiz()
  205. },
  206. async loadQuiz() {
  207. this.quizLoading = true
  208. this.quizError = false
  209. try {
  210. var childId = uni.getStorageSync('currentChildId')
  211. var res = await getAiQuestions({
  212. articleId: this.article.id,
  213. childId: childId || undefined
  214. })
  215. if (res.code === 200 && res.data) {
  216. this.quizRecordId = res.data.recordId
  217. var questions = res.data.questions
  218. if (typeof questions === 'string') {
  219. questions = JSON.parse(questions)
  220. }
  221. this.quizQuestions = Array.isArray(questions) ? questions : []
  222. this.selectedAnswers = new Array(this.quizQuestions.length).fill(null)
  223. } else {
  224. this.quizError = true
  225. this.quizErrorMsg = '出题失败,请重试'
  226. }
  227. } catch (e) {
  228. this.quizError = true
  229. this.quizErrorMsg = '网络错误,请重试'
  230. } finally {
  231. this.quizLoading = false
  232. }
  233. },
  234. async onSubmitQuiz() {
  235. if (!this.canSubmit) return
  236. var answers = this.selectedAnswers.map(function(selected, idx) {
  237. return { questionIndex: idx, selected: selected }
  238. })
  239. try {
  240. var childId = uni.getStorageSync('currentChildId')
  241. var res = await submitAnswers({
  242. recordId: this.quizRecordId,
  243. answers: JSON.stringify(answers),
  244. childId: childId || undefined
  245. })
  246. if (res.code === 200 && res.data) {
  247. this.resultScore = res.data.score
  248. this.resultTotal = res.data.totalQuestions
  249. this.resultPoints = res.data.pointsEarned
  250. this.showResult = true
  251. this.showQuiz = false
  252. } else {
  253. uni.showToast({ title: res.message || '提交失败', icon: 'none' })
  254. }
  255. } catch (e) {
  256. uni.showToast({ title: '提交失败', icon: 'none' })
  257. }
  258. },
  259. goBack() {
  260. uni.navigateBack()
  261. }
  262. }
  263. }
  264. </script>
  265. <style scoped>
  266. .detail-container { min-height: 100vh; background: #fff; }
  267. .loading-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  268. .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; }
  269. @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
  270. .loading-text { font-size: 26rpx; color: #999; }
  271. .error-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  272. .error-icon { font-size: 100rpx; margin-bottom: 24rpx; }
  273. .error-text { font-size: 28rpx; color: #999; margin-bottom: 30rpx; }
  274. .retry-btn { width: 240rpx; height: 72rpx; line-height: 72rpx; background: #5B9BD5; color: #fff; font-size: 28rpx; border-radius: 36rpx; text-align: center; border: none; }
  275. .retry-btn::after { border: none; }
  276. .content-scroll { height: calc(100vh - 120rpx); }
  277. .detail-cover { width: 100%; display: block; }
  278. .detail-title { display: block; font-size: 36rpx; font-weight: bold; color: #333; line-height: 1.4; padding: 30rpx 30rpx 0; }
  279. .detail-meta { display: flex; align-items: center; padding: 16rpx 30rpx 0; font-size: 22rpx; color: #999; }
  280. .meta-author { color: #5B9BD5; }
  281. .meta-sep { margin: 0 12rpx; color: #ddd; }
  282. .detail-category-row { padding: 16rpx 30rpx 0; }
  283. .detail-category { display: inline-block; font-size: 20rpx; color: #5B9BD5; background: rgba(91,155,213,0.1); padding: 4rpx 16rpx; border-radius: 8rpx; }
  284. .detail-dimensions { padding: 16rpx 30rpx 0; display: flex; gap: 12rpx; }
  285. .dim-bar-item { flex: 1; }
  286. .dim-bar { height: 8rpx; border-radius: 4rpx; }
  287. .dim-label { font-size: 18rpx; color: #999; text-align: center; display: block; margin-top: 4rpx; }
  288. .divider { height: 1rpx; background: #eee; margin: 24rpx 30rpx; }
  289. .detail-body { padding: 0 30rpx; font-size: 28rpx; color: #444; line-height: 1.8; }
  290. .detail-body rich-text { word-break: break-word; }
  291. /* 底部按钮 */
  292. .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; }
  293. .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; }
  294. .read-btn::after { border: none; }
  295. /* 答题区 */
  296. .quiz-container { padding: 30rpx; }
  297. .quiz-header { text-align: center; margin-bottom: 40rpx; }
  298. .quiz-title { font-size: 34rpx; font-weight: bold; color: #333; display: block; }
  299. .quiz-desc { font-size: 24rpx; color: #999; margin-top: 10rpx; display: block; }
  300. .quiz-loading { display: flex; flex-direction: column; align-items: center; padding-top: 100rpx; }
  301. .quiz-loading-text { font-size: 26rpx; color: #999; margin-top: 20rpx; }
  302. .quiz-error { display: flex; flex-direction: column; align-items: center; padding-top: 100rpx; }
  303. .quiz-error-text { font-size: 26rpx; color: #999; margin-bottom: 20rpx; }
  304. .quiz-question { margin-bottom: 40rpx; }
  305. .q-title { font-size: 24rpx; color: #5B9BD5; font-weight: bold; display: block; margin-bottom: 12rpx; }
  306. .q-text { font-size: 30rpx; color: #333; line-height: 1.5; display: block; margin-bottom: 20rpx; }
  307. .q-option { display: flex; align-items: center; padding: 20rpx; background: #f5f7fa; border-radius: 12rpx; margin-bottom: 12rpx; border: 2rpx solid transparent; }
  308. .q-option.selected { background: #E8F4FD; border-color: #5B9BD5; }
  309. .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; }
  310. .q-option.selected .q-option-letter { background: #5B9BD5; }
  311. .q-option-text { font-size: 26rpx; color: #333; }
  312. .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; }
  313. .submit-btn[disabled] { opacity: 0.4; }
  314. .submit-btn::after { border: none; }
  315. /* 结果页 */
  316. .result-container { display: flex; flex-direction: column; align-items: center; padding: 120rpx 30rpx; }
  317. .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; }
  318. .result-icon { font-size: 100rpx; display: block; margin-bottom: 20rpx; }
  319. .result-title { font-size: 32rpx; font-weight: bold; color: #333; display: block; margin-bottom: 16rpx; }
  320. .result-score { font-size: 48rpx; font-weight: bold; color: #5B9BD5; display: block; margin-bottom: 12rpx; }
  321. .result-points { font-size: 36rpx; color: #F97316; font-weight: bold; display: block; }
  322. .back-btn { width: 60%; height: 80rpx; line-height: 80rpx; background: #5B9BD5; color: #fff; font-size: 30rpx; border-radius: 40rpx; text-align: center; border: none; }
  323. .back-btn::after { border: none; }
  324. </style>