article-detail.vue 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <template>
  2. <view class="detail-container" :style="{ '--font-scale': fontScale }">
  3. <!-- 登录浮层(全局组件,页面内弹出底部登录) -->
  4. <login-sheet ref="loginSheet" @success="onSheetLoginSuccess" />
  5. <view v-if="loading" class="loading-wrap">
  6. <view class="loading-spinner"></view>
  7. <text class="loading-text">加载中...</text>
  8. </view>
  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. <scroll-view scroll-y class="content-scroll" @scrolltolower="onScrollToBottom">
  16. <image v-if="article.coverImage" class="detail-cover" :src="article.coverImage" mode="widthFix" />
  17. <text class="detail-title">{{ article.title }}</text>
  18. <view class="detail-meta">
  19. <text class="meta-author">{{ article.author || '浠艾福' }}</text>
  20. <text class="meta-sep">|</text>
  21. <text class="meta-date">{{ formatDate(article.publishedAt) }}</text>
  22. <text class="meta-sep">|</text>
  23. <text class="meta-readtime">{{ article.readTime || 3 }}分钟阅读</text>
  24. <text class="reading-time-badge" v-if="isLogin && !readingCompleted">{{ formatReadingTime(readingSeconds) }}</text>
  25. <text class="reading-time-badge completed" v-else-if="isLogin && readingCompleted">✅ 阅读完成</text>
  26. <text class="meta-sep" v-if="isLogin">|</text>
  27. <text class="meta-readcount">{{ article.viewCount || 0 }}人阅读</text>
  28. </view>
  29. <view class="detail-category-row">
  30. <text class="detail-category">{{ article.categoryName || '' }}</text>
  31. </view>
  32. <view v-if="article.relatedDimensions" class="detail-dimensions">
  33. <view v-for="dim in parseDimensions(article)" :key="dim.code" class="dim-bar-item">
  34. <view class="dim-bar" :style="{ background: dim.color, width: dim.weight + '%' }"></view>
  35. <text class="dim-label">{{ dim.name }}</text>
  36. </view>
  37. </view>
  38. <view class="detail-summary" v-if="article.summary">
  39. <text class="detail-summary-text">{{ article.summary }}</text>
  40. </view>
  41. <view class="divider"></view>
  42. <view class="detail-body"><mp-html :content="article.content" :domain="config.API_BASE_URL" :tag-style="contentTagStyle" /></view>
  43. <!-- 关联商品推荐(文章标签 ↔ 商品标签匹配,登录/匿名均可返回) -->
  44. <view v-if="relatedProducts.length > 0" class="related-section">
  45. <view class="related-header">
  46. <text class="related-title">相关商品</text>
  47. <text class="related-subtitle">根据文章内容推荐</text>
  48. </view>
  49. <dimension-products :products="relatedProducts" @productClick="goRelatedProduct" />
  50. </view>
  51. <!-- 相关文章(按标签关联,无匹配整块隐藏) -->
  52. <view v-if="relatedArticles.length > 0" class="related-section">
  53. <view class="related-header">
  54. <text class="related-title">相关文章</text>
  55. <text class="related-subtitle">根据标签推荐</text>
  56. </view>
  57. <view class="ra-list">
  58. <view
  59. v-for="item in relatedArticles"
  60. :key="item.id"
  61. class="ra-item"
  62. hover-class="ra-item-pressed"
  63. @click="goRelatedArticle(item)"
  64. >
  65. <image class="ra-cover" :src="getImageUrl(item.coverImage) || '/static/default-article.png'" mode="aspectFill" />
  66. <view class="ra-body">
  67. <text class="ra-title">{{ item.title }}</text>
  68. <text class="ra-summary" v-if="item.summary">{{ item.summary }}</text>
  69. </view>
  70. </view>
  71. </view>
  72. </view>
  73. <!-- 评论区(已屏蔽) -->
  74. <view style="height: 200rpx;"></view>
  75. </scroll-view>
  76. <!-- 底部 -->
  77. <view class="detail-footer">
  78. <view v-if="isLogin" class="listen-btn" :class="{ playing: audioPlaying }" @click="toggleArticleAudio">
  79. <text class="listen-btn-icon">{{ audioLoading ? '⏳' : (audioPlaying ? '⏸' : '🔊') }}</text>
  80. <text class="listen-btn-text">{{ audioLoading ? '生成中' : (audioPlaying ? '停止' : '听文章') }}</text>
  81. </view>
  82. <view class="share-btn" @click="generatePoster">
  83. <text class="share-btn-icon">🖼</text>
  84. <text class="share-btn-text">生成海报</text>
  85. </view>
  86. </view>
  87. </template>
  88. <ContentSharePoster :show="showPoster" :title="(article && article.title) || ''" :coverImage="(article && article.coverImage) || ''" :qrCodeBase64="posterQrCode || ''" typeLabel="好文推荐" :dimensionCode="posterDimension || ''" :summary="posterSummary || ''" :inviteText="posterInviteText || ''" @close="closePoster" />
  89. <!-- 浠宝答题弹窗(一题一页) -->
  90. <view class="quiz-mask" v-if="showQuiz" @click="closeQuiz">
  91. <view class="quiz-dialog" @click.stop>
  92. <view class="quiz-mascot">
  93. <text class="mascot-icon">{{ mascotIcon }}</text>
  94. <text class="mascot-name">{{ mascotName }}</text>
  95. </view>
  96. <template v-if="currentQuestion">
  97. <view class="quiz-step-bar">
  98. <text class="quiz-step-text">第 {{ currentQuizIndex + 1 }} / {{ quizQuestions.length }} 题</text>
  99. <view class="quiz-dots">
  100. <view v-for="(q, qi) in quizQuestions" :key="qi" class="quiz-dot" :class="{ active: qi === currentQuizIndex, done: quizAnswers[qi] }"></view>
  101. </view>
  102. </view>
  103. <text class="q-title">{{ currentQuestion.question }}</text>
  104. <view class="q-options">
  105. <text v-for="(opt, oi) in currentQuestion.options" :key="oi" class="q-option" :class="{ selected: quizAnswers[currentQuizIndex] === String.fromCharCode(65 + oi) }" @click="selectQuizAnswer(currentQuizIndex, oi)">{{ opt }}</text>
  106. </view>
  107. <view class="quiz-nav">
  108. <button v-if="currentQuizIndex > 0" class="quiz-prev" @click="goPrevQuestion">上一题</button>
  109. <button v-if="currentQuizIndex < quizQuestions.length - 1" class="quiz-submit" @click="goNextQuestion">下一题</button>
  110. <button v-else class="quiz-submit" @click="submitQuiz">提交答案</button>
  111. </view>
  112. </template>
  113. </view>
  114. </view>
  115. <!-- 答题结果 -->
  116. <view class="result-mask" v-if="showResult">
  117. <view class="result-dialog">
  118. <text class="result-icon">{{ resultIcon }}</text>
  119. <text class="result-text">答对 {{ quizResult.correctCount }}/{{ quizResult.totalQuestions }} 题</text>
  120. <text class="result-energy">获得 {{ quizResult.cfEarned }} CF值 + {{ quizResult.energyEarned }} 能量 ⚡</text>
  121. <button class="result-btn" @click="closeResult">知道了</button>
  122. </view>
  123. </view>
  124. </view>
  125. </template>
  126. <script>
  127. import { getArticleDetail, getArticleAudio, getArticleRelatedProducts, getRelatedArticles, reportReadingTime, getShareQrCode, completeArticleRead, generateQuiz, submitQuiz } from '@/utils/api.js'
  128. import shareMixin from '../../components/share-mixin.js'
  129. import ContentSharePoster from '@/components/ContentSharePoster.vue'
  130. import DimensionProducts from '@/components/DimensionProducts.vue'
  131. import MpHtml from '@/components/mp-html/mp-html.vue'
  132. import config from '@/config.js'
  133. import { getFontScale } from '@/utils/fontScale.js'
  134. export default {
  135. mixins: [shareMixin],
  136. components: { ContentSharePoster, DimensionProducts, MpHtml },
  137. data() {
  138. return {
  139. articleId: '', article: null, loading: true, error: false, errorMsg: '',
  140. relatedProducts: [],
  141. relatedArticles: [],
  142. readingSeconds: 0, lastReportedSeconds: 0, readingCompleted: false,
  143. isTimerRunning: false, timerHandle: null, syncHandle: null,
  144. currentChildId: null, isLogin: false,
  145. // 听文章(AI 朗读)
  146. audioUrls: [], audioPlaying: false, audioLoading: false, audioContext: null, audioChunkIndex: 0,
  147. showPoster: false, posterQrCode: '',
  148. posterDimension: '', posterSummary: '', posterInviteText: '',
  149. fontScale: 1,
  150. // 答题
  151. showQuiz: false, quizQuestions: [], quizAnswers: [], showResult: false, quizResult: {}, quizRecordId: null, currentQuizIndex: 0,
  152. mascotList: [{ icon: '🐶', name: '浠宝' }, { icon: '🐼', name: '福宝' }],
  153. mascotIcon: '🐶', mascotName: '浠宝'
  154. }
  155. },
  156. computed: {
  157. contentTagStyle() {
  158. var scale = this.fontScale || 1
  159. var fontSize = 32 * scale
  160. return { p: 'font-size:' + fontSize + 'rpx;color:#444;line-height:1.8;', div: 'font-size:' + fontSize + 'rpx;color:#444;line-height:1.8;', section: 'font-size:' + fontSize + 'rpx;color:#444;line-height:1.8;', li: 'font-size:' + fontSize + 'rpx;color:#444;line-height:1.8;', blockquote: 'font-size:' + fontSize + 'rpx;color:#444;line-height:1.8;' }
  161. },
  162. // 当前展示的一题(一题一页)
  163. currentQuestion: function() {
  164. if (!this.quizQuestions || !this.quizQuestions.length) return null
  165. return this.quizQuestions[this.currentQuizIndex] || null
  166. }
  167. },
  168. onLoad(options) {
  169. this.mascotIcon = this.mascotList[Math.floor(Math.random() * 2)].icon
  170. this.mascotName = this.mascotList[Math.floor(Math.random() * 2)].name
  171. // 兼容旧分享链接:refCode / inviteCode 参数(shareMixin onLoad 也会处理,此处显式兜底)
  172. if (options && (options.refCode || options.inviteCode)) {
  173. this.autoBindInviteCode(options.refCode || options.inviteCode)
  174. }
  175. var aid = ''
  176. if (options && options.id) {
  177. aid = options.id
  178. } else if (options && options.scene) {
  179. try {
  180. var sceneDecoded = decodeURIComponent(options.scene)
  181. var idMatch = sceneDecoded.match(/id=(\d+)/)
  182. if (idMatch && idMatch[1]) aid = idMatch[1]
  183. var refMatch = sceneDecoded.match(/ref=([^&]+)/)
  184. if (refMatch && refMatch[1]) this.autoBindInviteCode(refMatch[1])
  185. } catch (e) {}
  186. }
  187. if (aid) { this.articleId = aid; this.loadDetail(aid) }
  188. else { this.error = true; this.errorMsg = '参数错误'; this.loading = false }
  189. this.currentChildId = uni.getStorageSync('currentChildId') || null
  190. this.isLogin = !!uni.getStorageSync('token')
  191. },
  192. onShow() {
  193. this.fontScale = getFontScale()
  194. var memberId = uni.getStorageSync('currentChildId')
  195. if (!this.article || this.error) return
  196. // 已登录用户(家长/成员/规划师)均可计时,匿名用户不计时
  197. if (this.isLogin) this.startNewReadingSession()
  198. },
  199. onHide() { this.pauseReadingTimer(); this.stopArticleAudio() },
  200. onUnload() { this.pauseReadingTimer(); this.stopArticleAudio() },
  201. methods: {
  202. async loadDetail(id) {
  203. this.loading = true; this.error = false
  204. try {
  205. var res = await getArticleDetail({ id: id })
  206. if (res.code === 200 && res.data) {
  207. this.article = res.data
  208. this.article.coverImage = this.getImageUrl(this.article.coverImage)
  209. this.article.content = this.resolveContentUrls(this.article.content)
  210. this.setShareInfo('推荐阅读: ' + (res.data.title || ''), '/pages/article-center/article-detail?id=' + id)
  211. this.startReadingTimer()
  212. // 关联商品:放在 loadDetail 内只走一次(onShow 不重复请求,避免双发)
  213. this.loadRelatedProducts(id)
  214. // 相关文章:同样只走一次,无匹配时为空数组 → 模板整块隐藏
  215. this.loadRelatedArticles(id)
  216. } else { this.error = true; this.errorMsg = '文章不存在或无权限查看' }
  217. } catch (e) { this.error = true; this.errorMsg = '加载失败' }
  218. finally { this.loading = false }
  219. },
  220. // 加载文章关联商品(后端按 标签匹配数 > 用户权重 > 家庭权重 > 销量 排序)
  221. loadRelatedProducts: function(articleId) {
  222. var self = this
  223. if (!articleId) return
  224. getArticleRelatedProducts({ articleId: articleId, size: 4 }).then(function(res) {
  225. if (res && res.code === 200 && res.data && Array.isArray(res.data)) {
  226. self.relatedProducts = res.data
  227. }
  228. }).catch(function() { self.relatedProducts = [] })
  229. },
  230. // 加载相关文章(后端按共享标签数降序,无匹配返回空数组 → 前端整块隐藏)
  231. loadRelatedArticles: function(articleId) {
  232. var self = this
  233. if (!articleId) return
  234. getRelatedArticles({ articleId: articleId, size: 3 }).then(function(res) {
  235. if (res && res.code === 200 && res.data && Array.isArray(res.data)) {
  236. self.relatedArticles = res.data
  237. }
  238. }).catch(function() { self.relatedArticles = [] })
  239. },
  240. // 跳转相关文章详情
  241. goRelatedArticle: function(item) {
  242. if (!item || !item.id) return
  243. uni.navigateTo({ url: '/pages/article-center/article-detail?id=' + item.id })
  244. },
  245. // 跳转商品详情(优先使用后端返回的 url)
  246. goRelatedProduct: function(prod) {
  247. if (!prod) return
  248. var pid = prod.productId || prod.id
  249. var url = prod.url || ('/pages/discover-detail/product-detail/product-detail?id=' + (pid || ''))
  250. if (url.indexOf('/pages/') !== 0) return
  251. uni.navigateTo({ url: url })
  252. },
  253. formatDate(d) { return d ? d.slice(0, 10) : '' },
  254. getImageUrl: function(path) {
  255. if (!path) return ''
  256. if (path.indexOf('http://') === 0 || path.indexOf('https://') === 0) return path
  257. return config.API_BASE_URL + path
  258. },
  259. resolveContentUrls: function(html) {
  260. if (!html) return html
  261. // 将富文本内容中的相对图片路径(/uploads/...)拼接为绝对 URL
  262. return html.replace(/(src|href)=["']\/(uploads\/[^"']+)/g, function(match, attr, path) {
  263. return attr + '="' + config.API_BASE_URL + '/' + path + '"'
  264. })
  265. },
  266. parseDimensions: function(article) {
  267. 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: '富' } }
  268. var raw = article && article.relatedDimensions ? article.relatedDimensions.split(',').map(function(s) { return s.trim().toLowerCase() }) : []
  269. var weights = null
  270. if (article && article.dimensionWeights) try { var w = JSON.parse(article.dimensionWeights); if (w && typeof w === 'object') weights = w } catch (e) {}
  271. if (!weights) {
  272. var n = raw.filter(function(c) { return dimMap[c] }).length
  273. 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++ }) }
  274. }
  275. 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 } })
  276. },
  277. getMainDimension: function() {
  278. var dims = this.article ? this.parseDimensions(this.article) : []
  279. var best = null
  280. var i
  281. for (i = 0; i < dims.length; i++) {
  282. if (!best || dims[i].weight > best.weight) best = dims[i]
  283. }
  284. return best ? best.code : ''
  285. },
  286. onScrollToBottom() {},
  287. startNewReadingSession: function() {
  288. // 每次进入文章详情页都从 0 开始新会话,时长不延续上一次
  289. this.readingSeconds = 0
  290. this.lastReportedSeconds = 0
  291. this.startReadingTimer()
  292. },
  293. // 获取当前操作对象的 memberId:优先 currentChildId(切换视角),否则用 userId
  294. getActiveMemberId: function() {
  295. var mid = uni.getStorageSync('currentChildId')
  296. if (mid) return parseInt(mid)
  297. var uid = uni.getStorageSync('userId')
  298. return uid ? parseInt(uid) : null
  299. },
  300. startReadingTimer: function() {
  301. // 无登录态时不计时(匿名浏览只显示时长要求,不计时)
  302. if (!this.isLogin) return
  303. if (this.isTimerRunning || this.readingCompleted) return
  304. this.isTimerRunning = true
  305. var self = this
  306. this.timerHandle = setInterval(function() { self.readingSeconds++ }, 1000)
  307. // 每 10 秒自动记录一次该用户已读时长
  308. this.syncHandle = setInterval(function() { self.reportReadingTime() }, 10000)
  309. },
  310. pauseReadingTimer: function() {
  311. if (!this.isTimerRunning) return
  312. this.isTimerRunning = false
  313. if (this.timerHandle) { clearInterval(this.timerHandle); this.timerHandle = null }
  314. if (this.syncHandle) { clearInterval(this.syncHandle); this.syncHandle = null }
  315. this.reportReadingTime()
  316. },
  317. async reportReadingTime() {
  318. var delta = this.readingSeconds - this.lastReportedSeconds
  319. if (delta <= 0) return
  320. this.lastReportedSeconds = this.readingSeconds
  321. var memberId = this.getActiveMemberId()
  322. if (!this.isLogin || !this.article) return
  323. reportReadingTime({ articleId: this.article.id, memberId: memberId || 0, durationSeconds: delta })
  324. // 达到阅读时长自动完成
  325. var targetSeconds = (this.article.readTime || 3) * 60
  326. if (!this.readingCompleted && this.readingSeconds >= targetSeconds) {
  327. this.readingCompleted = true
  328. this.pauseReadingTimer()
  329. try { await completeArticleRead({ articleId: this.article.id, memberId: memberId || 0, durationSeconds: this.readingSeconds }) } catch (e) {}
  330. uni.showToast({ title: '阅读完成 +5能量', icon: 'success' })
  331. // 弹出答题(登录用户)
  332. this.startQuiz()
  333. }
  334. },
  335. formatReadingTime: function(seconds) {
  336. if (seconds < 60) return '已读 ' + seconds + '秒'
  337. var min = Math.floor(seconds / 60); var sec = seconds % 60
  338. return '已读 ' + min + '分' + (sec > 0 ? sec + '秒' : '')
  339. },
  340. // 听文章:首次点击拉取(或复用已生成)音频,多段顺序播放;播放期间保证阅读计时运行
  341. async toggleArticleAudio() {
  342. if (this.audioPlaying) { this.stopArticleAudio(); return }
  343. if (this.audioLoading || !this.article) return
  344. if (this.audioUrls && this.audioUrls.length > 0) { this.playAudioChunk(0); return }
  345. this.audioLoading = true
  346. try {
  347. var res = await getArticleAudio({ articleId: this.article.id })
  348. if (res.code === 200 && res.data && res.data.audioUrls && res.data.audioUrls.length > 0) {
  349. this.audioUrls = res.data.audioUrls
  350. this.playAudioChunk(0)
  351. } else {
  352. uni.showToast({ title: (res && res.message) || '抱歉,音频暂不可用', icon: 'none' })
  353. }
  354. } catch (e) {
  355. uni.showToast({ title: '音频生成失败,请稍后重试', icon: 'none' })
  356. } finally {
  357. this.audioLoading = false
  358. }
  359. },
  360. playAudioChunk: function(index) {
  361. var self = this
  362. // 播放期间保证阅读计时运行(未登录不计时)
  363. if (this.isLogin && !this.isTimerRunning && !this.readingCompleted) this.startReadingTimer()
  364. this.audioChunkIndex = index
  365. var ctx = uni.createInnerAudioContext()
  366. this.audioContext = ctx
  367. ctx.src = this.audioUrls[index]
  368. ctx.onEnded(function() {
  369. if (index + 1 < self.audioUrls.length) { self.playAudioChunk(index + 1) }
  370. else { self.stopArticleAudio() }
  371. })
  372. ctx.onError(function() {
  373. uni.showToast({ title: '播放失败', icon: 'none' })
  374. self.stopArticleAudio()
  375. })
  376. ctx.play()
  377. this.audioPlaying = true
  378. },
  379. stopArticleAudio: function() {
  380. if (this.audioContext) {
  381. try { this.audioContext.stop() } catch (e) {}
  382. try { this.audioContext.destroy() } catch (e) {}
  383. this.audioContext = null
  384. }
  385. this.audioPlaying = false
  386. },
  387. async generatePoster() {
  388. if (!this.articleId) return
  389. uni.showLoading({ title: '生成海报中...' })
  390. try {
  391. if (!this.referralCode) {
  392. await this.loadReferralCode()
  393. }
  394. var scene = 'id=' + this.articleId + (this.referralCode ? '&ref=' + this.referralCode : '')
  395. if (scene.length > 32) {
  396. scene = scene.substring(0, 32)
  397. }
  398. var res = await getShareQrCode('pages/article-center/article-detail', scene)
  399. if (res && res.data) {
  400. this.posterQrCode = res.data.qrCodeBase64 || ''
  401. this.posterDimension = this.getMainDimension()
  402. this.posterSummary = (this.article && this.article.summary) || ''
  403. this.posterInviteText = '「' + (uni.getStorageSync('nickname') || '好友') + '」邀请你一起阅读这篇文章'
  404. this.showPoster = true
  405. }
  406. } catch (e) { uni.showToast({ title: '生成失败', icon: 'none' }) }
  407. finally { uni.hideLoading() }
  408. },
  409. // 答题
  410. async startQuiz() {
  411. if (!this.isLogin) {
  412. uni.showToast({ title: '登录后即可完成知识测评', icon: 'none' })
  413. setTimeout(() => { this.openLoginSheet() }, 1500)
  414. return
  415. }
  416. var memberId = this.getActiveMemberId()
  417. try {
  418. var res = await generateQuiz({ articleId: this.articleId, memberId: memberId || 0 })
  419. if (res.code === 200 && res.data && res.data.questions && res.data.questions.length > 0) {
  420. this.quizRecordId = res.data.recordId
  421. this.quizQuestions = res.data.questions
  422. this.quizAnswers = []
  423. this.currentQuizIndex = 0
  424. this.showQuiz = true
  425. }
  426. } catch (e) {}
  427. },
  428. async submitQuiz() {
  429. var memberId = this.getActiveMemberId()
  430. var answers = []
  431. for (var i = 0; i < this.quizQuestions.length; i++) {
  432. answers.push(this.quizAnswers[i] || '')
  433. }
  434. try {
  435. var res = await submitQuiz({ recordId: this.quizRecordId, memberId: parseInt(memberId || 0), answers: answers })
  436. if (res.code === 200) {
  437. this.quizResult = res.data || {}
  438. this.showQuiz = false
  439. this.showResult = true
  440. }
  441. } catch (e) {}
  442. },
  443. closePoster: function() {
  444. this.showPoster = false
  445. },
  446. closeQuiz: function() {
  447. this.showQuiz = false
  448. },
  449. closeResult: function() {
  450. this.showResult = false
  451. },
  452. selectQuizAnswer: function(qi, oi) {
  453. this.quizAnswers[qi] = String.fromCharCode(65 + oi)
  454. },
  455. // 一题一页:上一题 / 下一题
  456. goPrevQuestion: function() {
  457. if (this.currentQuizIndex > 0) this.currentQuizIndex--
  458. },
  459. goNextQuestion: function() {
  460. if (this.currentQuizIndex < this.quizQuestions.length - 1) this.currentQuizIndex++
  461. }
  462. }
  463. }
  464. </script>
  465. <style scoped lang="scss">
  466. .detail-container { min-height: 100vh; background: #fff; }
  467. .loading-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  468. .loading-spinner { width: 60rpx; height: 60rpx; border: 4rpx solid #e0e0e0; border-top-color: $uni-info; border-radius: 50%; animation: spin 0.8s linear infinite; margin-bottom: 20rpx; }
  469. @keyframes spin { 0% { transform: rotate(0deg); } 360% { transform: rotate(360deg); } }
  470. .loading-text { font-size: 26rpx; color: #999; }
  471. .error-wrap { display: flex; flex-direction: column; align-items: center; padding-top: 300rpx; }
  472. .error-icon { font-size: 100rpx; margin-bottom: 24rpx; }
  473. .error-text { font-size: 28rpx; color: #999; margin-bottom: 30rpx; }
  474. .retry-btn { width: 240rpx; height: 72rpx; line-height: 72rpx; background: $brand-primary; color: #fff; font-size: 28rpx; border-radius: 36rpx; text-align: center; border: none; }
  475. .content-scroll { height: calc(100vh - 120rpx); }
  476. .detail-cover { width: 100%; display: block; }
  477. .detail-title { display: block; font-size: calc(36rpx * var(--font-scale, 1)); font-weight: bold; color: #333; line-height: 1.4; padding: 30rpx 30rpx 0; }
  478. .detail-meta { display: flex; align-items: center; padding: 16rpx 30rpx 0; font-size: 22rpx; color: #999; flex-wrap: wrap; }
  479. .meta-author { color: $uni-info; }
  480. .meta-sep { margin: 0 12rpx; color: #ddd; }
  481. .reading-time-badge { margin-left: auto; font-size: 20rpx; color: $uni-info; background: rgba(14, 165, 233, 0.08); padding: 4rpx 12rpx; border-radius: 20rpx; white-space: nowrap; }
  482. .reading-time-badge.completed { color: #10B981; background: rgba(16,185,129,0.1); }
  483. .meta-readcount {
  484. font-size: 20rpx;
  485. color: #bbb;
  486. }
  487. .detail-category-row { padding: 16rpx 30rpx 0; }
  488. .detail-category { display: inline-block; font-size: 20rpx; color: $uni-info; background: rgba(14, 165, 233, 0.1); padding: 4rpx 16rpx; border-radius: 8rpx; }
  489. .detail-dimensions { padding: 16rpx 30rpx 0; display: flex; gap: 12rpx; }
  490. .dim-bar-item { flex: 1; }
  491. .dim-bar { height: 8rpx; border-radius: 4rpx; }
  492. .dim-label { font-size: 18rpx; color: #999; text-align: center; display: block; margin-top: 4rpx; }
  493. .detail-summary { padding: 20rpx 30rpx; }
  494. .detail-summary-text { font-size: calc(26rpx * var(--font-scale, 1)); color: #888; line-height: 1.7; display: block; border-left: 4rpx solid #F97316; padding-left: 20rpx; }
  495. .divider { height: 1rpx; background: #eee; margin: 24rpx 30rpx; }
  496. .detail-body { padding: 0 30rpx; font-size: calc(32rpx * var(--font-scale, 1)); color: #444; line-height: 1.8; }
  497. /* 关联商品推荐 */
  498. .related-section { margin-top: 20rpx; padding: 0 30rpx 8rpx; }
  499. .related-header { display: flex; align-items: baseline; margin-bottom: 16rpx; }
  500. .related-title { font-size: 30rpx; font-weight: bold; color: #333; }
  501. .related-subtitle { font-size: 20rpx; color: #999; margin-left: 12rpx; }
  502. /* 相关文章 */
  503. .ra-list { display: flex; flex-direction: column; gap: 16rpx; }
  504. .ra-item { display: flex; align-items: center; background: #fafafa; border-radius: 12rpx; padding: 16rpx; }
  505. .ra-item-pressed { opacity: 0.7; }
  506. .ra-cover { width: 140rpx; height: 100rpx; border-radius: 8rpx; background: #eee; flex-shrink: 0; }
  507. .ra-body { margin-left: 20rpx; flex: 1; min-width: 0; }
  508. .ra-title { display: block; font-size: 28rpx; font-weight: 600; color: #333; line-height: 1.4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  509. .ra-summary { display: block; margin-top: 8rpx; font-size: 22rpx; color: #999; line-height: 1.4; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  510. .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; }
  511. .share-btn { height: 72rpx; padding: 0 30rpx; background: linear-gradient(135deg, #F97316, #FB923C); border-radius: 36rpx; display: flex; align-items: center; justify-content: center; border: none; }
  512. .share-btn::after { border: none; }
  513. .share-btn-icon { font-size: 30rpx; margin-right: 8rpx; }
  514. .share-btn-text { font-size: 26rpx; color: #fff; font-weight: 600; }
  515. .listen-btn { height: 72rpx; padding: 0 30rpx; background: rgba(14, 165, 233, 0.1); border: 2rpx solid $uni-info; border-radius: 36rpx; display: flex; align-items: center; justify-content: center; }
  516. .listen-btn.playing { background: rgba(14, 165, 233, 0.2); }
  517. .listen-btn-icon { font-size: 30rpx; margin-right: 8rpx; }
  518. .listen-btn-text { font-size: 26rpx; color: $uni-info; font-weight: 600; }
  519. /* 答题弹窗 */
  520. .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; }
  521. .quiz-dialog, .result-dialog { background: #fff; border-radius: 24rpx; width: 650rpx; padding: 32rpx; }
  522. .quiz-mascot { display: flex; align-items: center; justify-content: center; gap: 12rpx; margin-bottom: 16rpx; }
  523. .mascot-icon { font-size: 60rpx; }
  524. .mascot-name { font-size: 32rpx; font-weight: 700; color: #F97316; }
  525. .quiz-step-bar { display: flex; align-items: center; justify-content: center; margin-bottom: 24rpx; }
  526. .quiz-step-text { font-size: 24rpx; color: #999; margin-right: 12rpx; }
  527. .quiz-dots { display: flex; align-items: center; gap: 8rpx; }
  528. .quiz-dot { width: 12rpx; height: 12rpx; border-radius: 50%; background: #E5E7EB; }
  529. .quiz-dot.active { background: #F97316; }
  530. .quiz-dot.done { background: #FDBA74; }
  531. .q-title { font-size: 30rpx; font-weight: 600; color: #333; line-height: 1.5; margin-bottom: 20rpx; }
  532. .q-options { display: flex; flex-direction: column; gap: 12rpx; }
  533. .q-option { padding: 16rpx 20rpx; border: 2rpx solid #E5E7EB; border-radius: 12rpx; font-size: 26rpx; color: #333; }
  534. .q-option.selected { border-color: #F97316; background: #FFF7ED; color: #92400E; }
  535. .quiz-nav { display: flex; gap: 16rpx; margin-top: 28rpx; }
  536. .quiz-prev { flex: 1; padding: 20rpx; background: #fff; color: #F97316; border: 2rpx solid #F97316; font-size: 28rpx; border-radius: 12rpx; }
  537. .quiz-prev::after { border: none; }
  538. .quiz-submit { flex: 1; padding: 20rpx; background: linear-gradient(135deg, #F97316, #FB923C); color: #fff; font-size: 28rpx; border-radius: 12rpx; border: none; }
  539. .quiz-submit::after { border: none; }
  540. .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; }
  541. .result-dialog { text-align: center; }
  542. .result-icon { font-size: 80rpx; }
  543. .result-text { font-size: 32rpx; font-weight: 700; color: #333; margin: 16rpx 0; }
  544. .result-energy { font-size: 28rpx; color: #F97316; }
  545. </style>