四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

481 lines
14 KiB

  1. <template>
  2. <view class="article-detail">
  3. <!-- 导航栏 -->
  4. <!-- #ifndef H5 -->
  5. <uv-navbar :placeholder="true" left-icon="arrow-left" :title="articleData.title || '文章详情'"
  6. @leftClick="goBack"></uv-navbar>
  7. <!-- #endif -->
  8. <!-- 文章内容 -->
  9. <view class="article-container" v-if="articleData.id">
  10. <!-- 文章标题 -->
  11. <view class="article-title">{{ articleData.title }}</view>
  12. <!-- 文章信息 -->
  13. <view class="article-info">
  14. <text class="article-time">{{ formatTime(articleData.createTime) }}</text>
  15. <!-- <text class="article-author">{{ articleData.createBy }}</text> -->
  16. </view>
  17. <!-- 文章内容 -->
  18. <view class="article-content">
  19. <uv-parse :content="content"/>
  20. </view>
  21. </view>
  22. <!-- 加载状态 -->
  23. <view class="loading-container" v-else-if="loading">
  24. <uv-loading-icon mode="circle"></uv-loading-icon>
  25. <text class="loading-text">加载中...</text>
  26. </view>
  27. <!-- 音频播放悬浮按钮 -->
  28. <view class="audio-float-button" v-if="hasAudio && articleData.id" @click="toggleAudio">
  29. <uv-icon :name="isPlaying ? 'pause-circle-fill' : 'play-circle-fill'" size="50"
  30. :color="isPlaying ? '#ff6b6b' : '#4CAF50'"></uv-icon>
  31. </view>
  32. <!-- 底部字幕展示 -->
  33. <!-- <view v-if="hasAudio && subtitlesJson.length > 0 && currentSubtitleText" class="subtitle-container">
  34. <text class="subtitle-text">{{ currentSubtitleText }}</text>
  35. </view> -->
  36. </view>
  37. </template>
  38. <script>
  39. import audioManager from '@/utils/audioManager.js'
  40. import { convertToSentences } from '@/utils/audioUtils.js'
  41. export default {
  42. data() {
  43. return {
  44. articleId: '',
  45. articleData: {},
  46. loading: true,
  47. isPlaying: false,
  48. audioUrl: '',
  49. hasAudio: false,
  50. subtitlesJson: [],
  51. // 字幕同步相关
  52. currentSentenceIndex: -1,
  53. currentSubtitleText: '',
  54. }
  55. },
  56. computed: {
  57. content(){
  58. return this.articleData.content
  59. },
  60. },
  61. onLoad(options) {
  62. if (options.id) {
  63. this.articleId = options.id
  64. this.getArticleDetail()
  65. }
  66. // 绑定音频管理器事件监听
  67. this.bindAudioEvents()
  68. },
  69. onUnload() {
  70. // 页面卸载时清理音频资源和事件监听
  71. this.stopAudio()
  72. this.unbindAudioEvents()
  73. },
  74. methods: {
  75. // 绑定音频管理器事件监听
  76. bindAudioEvents() {
  77. audioManager.on('play', this.onAudioPlay)
  78. audioManager.on('pause', this.onAudioPause)
  79. audioManager.on('ended', this.onAudioEnded)
  80. audioManager.on('error', this.onAudioError)
  81. audioManager.on('timeupdate', this.onAudioTimeUpdate)
  82. },
  83. // 解绑音频管理器事件监听
  84. unbindAudioEvents() {
  85. audioManager.off('play', this.onAudioPlay)
  86. audioManager.off('pause', this.onAudioPause)
  87. audioManager.off('ended', this.onAudioEnded)
  88. audioManager.off('error', this.onAudioError)
  89. audioManager.off('timeupdate', this.onAudioTimeUpdate)
  90. },
  91. // 音频播放开始事件
  92. onAudioPlay(data) {
  93. if (data.audioType === 'article') {
  94. this.isPlaying = true
  95. console.log('文章音频开始播放')
  96. }
  97. },
  98. // 音频暂停事件
  99. onAudioPause(data) {
  100. if (data.audioType === 'article') {
  101. this.isPlaying = false
  102. console.log('文章音频暂停播放')
  103. }
  104. },
  105. // 音频播放结束事件
  106. onAudioEnded(data) {
  107. if (data.audioType === 'article') {
  108. this.isPlaying = false
  109. console.log('文章音频播放结束')
  110. // 清空字幕状态
  111. this.currentSentenceIndex = -1
  112. this.currentSubtitleText = ''
  113. }
  114. },
  115. // 音频播放错误事件
  116. onAudioError(data) {
  117. if (data.audioType === 'article') {
  118. this.isPlaying = false
  119. console.error('文章音频播放错误:', data.error)
  120. // 清空字幕状态
  121. this.currentSentenceIndex = -1
  122. this.currentSubtitleText = ''
  123. uni.showToast({
  124. title: '音频播放失败',
  125. icon: 'none'
  126. })
  127. }
  128. },
  129. // 音频时间更新事件(字幕同步)
  130. onAudioTimeUpdate({ audioType, currentTime }) {
  131. if (audioType !== 'article') return
  132. if (!this.subtitlesJson || this.subtitlesJson.length === 0) return
  133. // audioManager.currentTime 为秒,字幕时间为毫秒,这里统一为毫秒比较
  134. const curMs = Math.floor(currentTime * 1000)
  135. const sentences = this.subtitlesJson
  136. // 如果当前索引有效且还在范围内,直接使用当前句子
  137. if (this.currentSentenceIndex >= 0) {
  138. const cur = sentences[this.currentSentenceIndex]
  139. // 容忍度 80ms,避免边界抖动
  140. if (curMs >= cur.beginTime && curMs <= cur.endTime + 80) {
  141. this.currentSubtitleText = cur.text
  142. return
  143. }
  144. }
  145. // 否则扫描找到当前时间对应的句子
  146. let idx = -1
  147. for (let i = 0; i < sentences.length; i++) {
  148. const s = sentences[i]
  149. if (curMs >= s.beginTime && curMs <= s.endTime + 80) {
  150. idx = i
  151. break
  152. }
  153. }
  154. this.currentSentenceIndex = idx
  155. this.currentSubtitleText = idx >= 0 ? sentences[idx].text : ''
  156. },
  157. // 返回上一页
  158. goBack() {
  159. uni.navigateBack()
  160. },
  161. changeSubtitlesJson(articleData) {
  162. if (articleData.subtitlesJson) {
  163. try {
  164. //[{"EndTime":480,"BeginTime":250,"Text":"语","BeginIndex":0,"EndIndex":1,"Phoneme":"yv3"}]
  165. let json = JSON.parse(articleData.subtitlesJson)
  166. if (!Array.isArray(json) || json.length === 0) {
  167. this.subtitlesJson = []
  168. return
  169. }
  170. // 将字级别数据转换为句子级别
  171. const sentences = convertToSentences(json)
  172. this.subtitlesJson = sentences
  173. console.log('转换后的句子数据:', sentences)
  174. } catch (error) {
  175. console.error('解析字幕数据失败:', error)
  176. this.subtitlesJson = []
  177. }
  178. } else {
  179. this.subtitlesJson = []
  180. }
  181. },
  182. chooseAudio(key = '501008') {
  183. let articleData = this.articleData?.audios?.[key]
  184. if (articleData) {
  185. // 获取第一个音频URL
  186. this.audioUrl = articleData.audioId
  187. // 处理字幕数据
  188. this.changeSubtitlesJson(articleData)
  189. this.hasAudio = true
  190. }
  191. },
  192. // 获取文章详情
  193. async getArticleDetail() {
  194. try {
  195. this.loading = true
  196. const res = await this.$api.home.getArticleDetail({ id: this.articleId })
  197. if (res.code === 200) {
  198. this.articleData = res.result
  199. // #ifdef H5
  200. window.document.title = this.articleData.title || '文章详情'
  201. // #endif
  202. // 处理音频数据
  203. this.chooseAudio()
  204. } else {
  205. uni.showToast({
  206. title: res.message || '获取文章详情失败',
  207. icon: 'none'
  208. })
  209. }
  210. } catch (error) {
  211. console.error('获取文章详情失败:', error)
  212. uni.showToast({
  213. title: '网络错误,请重试',
  214. icon: 'none'
  215. })
  216. } finally {
  217. this.loading = false
  218. }
  219. },
  220. // 切换音频播放状态
  221. toggleAudio() {
  222. if (!this.audioUrl) {
  223. console.error('音频URL为空')
  224. return
  225. }
  226. try {
  227. if (this.isPlaying) {
  228. // 暂停音频
  229. this.pauseAudio()
  230. } else {
  231. // 播放音频
  232. this.playAudio()
  233. }
  234. } catch (error) {
  235. console.error('音频播放切换异常:', error)
  236. this.isPlaying = false
  237. }
  238. },
  239. // 播放音频
  240. async playAudio() {
  241. if (!this.audioUrl) {
  242. console.error('音频URL为空')
  243. return
  244. }
  245. try {
  246. // 使用audioManager播放音频,指定音频类型为'article'
  247. await audioManager.playAudio(this.audioUrl, 'article')
  248. console.log('开始播放文章音频:', this.audioUrl)
  249. } catch (error) {
  250. console.error('音频播放异常:', error)
  251. this.isPlaying = false
  252. uni.showToast({
  253. title: '音频播放失败',
  254. icon: 'none'
  255. })
  256. }
  257. },
  258. // 暂停音频
  259. pauseAudio() {
  260. try {
  261. audioManager.pause()
  262. console.log('暂停文章音频')
  263. } catch (error) {
  264. console.error('暂停音频失败:', error)
  265. }
  266. },
  267. // 停止音频
  268. stopAudio() {
  269. try {
  270. audioManager.stopCurrentAudio()
  271. this.isPlaying = false
  272. console.log('停止文章音频')
  273. // 清空字幕状态
  274. this.currentSentenceIndex = -1
  275. this.currentSubtitleText = ''
  276. } catch (error) {
  277. console.error('停止音频失败:', error)
  278. }
  279. },
  280. // 格式化时间
  281. formatTime(timeStr) {
  282. if (!timeStr) return ''
  283. // 处理iOS兼容性问题,将 "2025-10-23 16:36:43" 格式转换为 "2025/10/23 16:36:43"
  284. let formattedTimeStr = timeStr.replace(/-/g, '/')
  285. const date = new Date(formattedTimeStr)
  286. // 检查日期是否有效
  287. if (isNaN(date.getTime())) {
  288. console.warn('Invalid date format:', timeStr)
  289. return timeStr // 返回原始字符串
  290. }
  291. const year = date.getFullYear()
  292. const month = String(date.getMonth() + 1).padStart(2, '0')
  293. const day = String(date.getDate()).padStart(2, '0')
  294. return `${year}-${month}-${day}`
  295. }
  296. }
  297. }
  298. </script>
  299. <style lang="scss" scoped>
  300. .subtitle-container{
  301. position: fixed;
  302. bottom: 220rpx;
  303. left: 5%;
  304. width: 90%;
  305. text-align: center;
  306. background-color: bisque;
  307. border-radius: 10rpx;
  308. }
  309. .article-detail {
  310. min-height: 100vh;
  311. background: #fff;
  312. }
  313. .article-container {
  314. padding: 30rpx;
  315. }
  316. .article-title {
  317. font-size: 40rpx;
  318. font-weight: 600;
  319. color: #333;
  320. line-height: 1.4;
  321. margin-bottom: 30rpx;
  322. }
  323. .article-info {
  324. display: flex;
  325. align-items: center;
  326. gap: 30rpx;
  327. margin-bottom: 40rpx;
  328. padding-bottom: 30rpx;
  329. border-bottom: 1px solid #f0f0f0;
  330. .article-time {
  331. font-size: 26rpx;
  332. color: #999;
  333. }
  334. .article-author {
  335. font-size: 26rpx;
  336. color: #666;
  337. &::before {
  338. content: '作者:';
  339. }
  340. }
  341. }
  342. .article-content {
  343. font-size: 32rpx;
  344. line-height: 1.8;
  345. color: #333;
  346. // 富文本内容样式
  347. :deep(.rich-text) {
  348. p {
  349. margin-bottom: 20rpx;
  350. line-height: 1.8;
  351. }
  352. img {
  353. max-width: 100%;
  354. height: auto;
  355. border-radius: 8rpx;
  356. margin: 20rpx 0;
  357. }
  358. section {
  359. margin: 20rpx 0;
  360. }
  361. }
  362. }
  363. .loading-container {
  364. display: flex;
  365. flex-direction: column;
  366. align-items: center;
  367. justify-content: center;
  368. height: 400rpx;
  369. .loading-text {
  370. margin-top: 20rpx;
  371. font-size: 28rpx;
  372. color: #999;
  373. }
  374. }
  375. // 音频播放悬浮按钮
  376. .audio-float-button {
  377. position: fixed;
  378. right: 30rpx;
  379. bottom: 100rpx;
  380. width: 100rpx;
  381. height: 100rpx;
  382. border-radius: 50%;
  383. background: rgba(255, 255, 255, 0.9);
  384. box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
  385. display: flex;
  386. align-items: center;
  387. justify-content: center;
  388. z-index: 999;
  389. transition: all 0.3s ease;
  390. &:active {
  391. transform: scale(0.95);
  392. }
  393. // 添加呼吸动画效果
  394. &::before {
  395. content: '';
  396. position: absolute;
  397. top: -10rpx;
  398. left: -10rpx;
  399. right: -10rpx;
  400. bottom: -10rpx;
  401. border-radius: 50%;
  402. background: rgba(76, 175, 80, 0.2);
  403. animation: pulse 2s infinite;
  404. z-index: -1;
  405. }
  406. }
  407. @keyframes pulse {
  408. 0% {
  409. transform: scale(1);
  410. opacity: 1;
  411. }
  412. 50% {
  413. transform: scale(1.1);
  414. opacity: 0.7;
  415. }
  416. 100% {
  417. transform: scale(1);
  418. opacity: 1;
  419. }
  420. }
  421. </style>