四零语境前端代码仓库
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.

2196 lines
65 KiB

1 month ago
1 month ago
2 weeks ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view class="book-container">
  3. <!-- 条件编译 -->
  4. <!-- #ifndef H5 -->
  5. <uv-status-bar></uv-status-bar>
  6. <!-- 自定义顶部导航栏 -->
  7. <view class="custom-navbar" :class="{ 'navbar-hidden': !showNavbar }">
  8. <uv-status-bar></uv-status-bar>
  9. <view class="navbar-content">
  10. <view class="navbar-left" @click="goBack">
  11. <uv-icon name="arrow-left" size="20" color="#262626"></uv-icon>
  12. </view>
  13. <view class="navbar-title">{{ currentPageTitle }}</view>
  14. </view>
  15. </view>
  16. <!-- #endif -->
  17. <!-- Swiper内容区域 -->
  18. <swiper
  19. class="content-swiper"
  20. :current="currentPage - 1"
  21. @change="onSwiperChange"
  22. >
  23. <swiper-item
  24. v-for="(page, index) in bookPages"
  25. :key="index"
  26. class="swiper-item"
  27. >
  28. <scroll-view
  29. scroll-y
  30. :scroll-top="scrollTops[index] || 0"
  31. :scroll-with-animation="true"
  32. style="height: 100vh;"
  33. class="scroll-container"
  34. @scroll="onScroll"
  35. >
  36. <view class="content-area" @click="toggleNavbar">
  37. <view class="title">{{ currentPageTitle }}</view>
  38. <!-- 会员限制页面 -->
  39. <view v-if="!isMember && pagePay[index] === 'Y'" class="member-content" >
  40. <text class="member-title">{{ pageTitles[index] }}</text>
  41. <view class="member-button" @click.stop="unlockBook">
  42. <text class="member-button-text">升级会员解锁</text>
  43. </view>
  44. </view>
  45. <!-- 图片卡片页面 -->
  46. <view class="card-content" v-else-if="pageTypes[index] === '1'">
  47. <view class="card-line">
  48. <image :src="configParamContent('highlight_icon')" class="card-line-image" mode="aspectFill" />
  49. <text class="card-line-text">划线重点</text>
  50. </view>
  51. <view v-for="(item, itemIndex) in page" :key="itemIndex">
  52. <image class="card-image" v-if="item && item.type === 'image'" :src="item.imageUrl" mode="aspectFill" ></image>
  53. <view class="english-text-container clickable-text" v-else-if="item && item.type === 'text' && item.language === 'en' && item.content" @click.stop="handleTextClick(item.content, item, index)" >
  54. <text
  55. v-for="(token, tokenIndex) in splitEnglishSentence(item.content)"
  56. :key="tokenIndex"
  57. :class="['english-token', { 'clickable-word': token.isWord && findWordDefinition(token.text) }]"
  58. @click.stop="token.isWord && findWordDefinition(token.text) ? handleWordClick(token.text) : null"
  59. user-select
  60. :style="item.style"
  61. >{{ token.text }}</text>
  62. </view>
  63. <view v-else-if="item && item.type === 'text' && item.language === 'zh' && item.content" @click.stop="handleTextClick(item.content, item, index)">
  64. <text
  65. v-for="(segment, segmentIndex) in processChineseText(item.content)"
  66. :key="segmentIndex"
  67. :class="['chinese-segment', { 'clickable-keyword': segment.isKeyword }]"
  68. @click.stop="segment.isKeyword ? handleChineseKeywordClick(segment.keywordData) : null"
  69. user-select
  70. :style="item.style"
  71. :id="`text-segment-${segmentIndex}`"
  72. >{{ segment.text }}</text>
  73. </view>
  74. </view>
  75. </view>
  76. <view v-else>
  77. <view v-for="(item, itemIndex) in page" :key="itemIndex">
  78. <!-- 文本页面 -->
  79. <view v-if="item && item.type === 'text' && item.content" class="text-content" >
  80. <view :class="{ 'lead-text': isTextHighlighted(page, itemIndex) }" @click.stop="handleTextClick(item.content, item, index)" :ref="`textRef_${index}_${itemIndex}`" :id="`text-${itemIndex}`">
  81. <text
  82. v-if="!item.isLead"
  83. class="content-text clickable-text"
  84. :style="item.style"
  85. user-select
  86. >
  87. {{ item.content }}
  88. </text>
  89. <view v-else-if="item.isLead" class="content-text clickable-text lead-text" :style="item.style" user-select>{{ item.content }}</view>
  90. </view>
  91. </view>
  92. <!-- 图片页面 -->
  93. <view v-else-if="item.type === 'image'" class="image-container" :ref="`imageRef_${index}_${itemIndex}`">
  94. <image class="content-image" :src="item.imageUrl" mode="widthFix"></image>
  95. </view>
  96. <!-- 视频页面 -->
  97. <view v-else-if="item.type === 'video'" class="video-content" @click.stop>
  98. <!-- 视频加载状态 -->
  99. <view v-if="videoLoading" class="video-loading">
  100. <text class="loading-text">视频加载中...</text>
  101. </view>
  102. <!-- 视频播放器 -->
  103. <video
  104. v-else
  105. :src="item.url"
  106. class="video-player"
  107. controls
  108. :poster="item.coverUrl"
  109. @loadstart="onVideoLoadStart"
  110. @loadeddata="onVideoLoadStart"
  111. @error="onVideoError"
  112. ></video>
  113. </view>
  114. </view>
  115. </view>
  116. </view>
  117. </scroll-view>
  118. </swiper-item>
  119. </swiper>
  120. <!-- 自定义底部控制栏 -->
  121. <CustomTabbar
  122. :show-navbar="showNavbar"
  123. :current-page="currentPage"
  124. :course-id="courseId"
  125. :voice-id="voiceId"
  126. :book-pages="bookPages"
  127. :is-text-page="isTextPage"
  128. :should-load-audio="shouldLoadAudio"
  129. :is-member="isMember"
  130. :current-page-requires-member="currentPageRequiresMember"
  131. :page-pay="pagePay"
  132. :is-word-audio-playing="isWordAudioPlaying"
  133. @toggle-course-popup="toggleCoursePopup"
  134. @toggle-sound="toggleSound"
  135. @go-to-page="goToPage"
  136. @previous-page="previousPage"
  137. @next-page="nextPage"
  138. @audio-state-change="onAudioStateChange"
  139. @highlight-change="onHighlightChange"
  140. @scroll-to-text="onScrollToText"
  141. @voice-change-complete="onVoiceChangeComplete"
  142. @voice-change-error="onVoiceChangeError"
  143. @page-data-needed="onPageDataNeeded"
  144. ref="customTabbar"
  145. />
  146. <!-- 课程选择弹出窗 -->
  147. <CoursePopup
  148. :style="{zIndex: 10000}"
  149. :course-list="courseList"
  150. :current-course="currentCourse"
  151. :is-reversed="isReversed"
  152. @toggle-sort="toggleSort"
  153. @select-course="selectCourse"
  154. ref="coursePopup"
  155. />
  156. <!-- 释义弹出窗 -->
  157. <MeaningPopup
  158. :style="{zIndex: 10000}"
  159. :current-word-meaning="currentWordMeaning"
  160. @close-meaning-popup="closeMeaningPopup"
  161. @repeat-word-audio="repeatWordAudio"
  162. ref="meaningPopup"
  163. />
  164. </view>
  165. </template>
  166. <script>
  167. import AudioControls from './AudioControls.vue'
  168. import CustomTabbar from './components/CustomTabbar.vue'
  169. import CoursePopup from './components/CoursePopup.vue'
  170. import MeaningPopup from './components/MeaningPopup.vue'
  171. export default {
  172. components: {
  173. AudioControls,
  174. CustomTabbar,
  175. CoursePopup,
  176. MeaningPopup
  177. },
  178. data() {
  179. return {
  180. isMember: false,
  181. memberId: '',
  182. voiceId: null,
  183. courseId: '',
  184. showNavbar: true,
  185. currentPage: 1,
  186. currentCourse: 1, // 当前课程索引
  187. currentWordMeaning: null, // 当前显示的单词释义
  188. isReversed: false, // 是否倒序显示
  189. // 文本高亮相关 - 由AudioControls组件管理,这里只保留必要的接口
  190. currentHighlightIndex: -1, // 当前高亮的文本索引,用于模板渲染
  191. wordAudioCache: {}, // 單詞語音緩存
  192. currentWordAudio: null, // 當前播放的單詞音頻實例
  193. isWordAudioPlaying: false, // 是否有单词音频正在播放
  194. // 音频状态相关 - 这些状态现在由AudioControls组件管理
  195. // 保留这些属性用于与AudioControls组件的数据同步
  196. isAudioLoading: false, // 音频是否正在加载
  197. hasAudioData: false, // 是否有音频数据
  198. audioLoadFailed: false, // 音频加载是否失败
  199. // 视频状态相关
  200. videoLoading: false, // 视频是否正在加载
  201. // 滚动相关
  202. scrollTops: [], // 每个页面的scroll-view滚动位置数组
  203. scrollDebounceTimer: null, // 滚动防抖定时器
  204. isScrolling: false, // 是否正在滚动中
  205. courseIdList: [],
  206. bookTitle: '',
  207. courseList: [
  208. ],
  209. // 二维数组 代表每个页面
  210. bookPages: [
  211. ],
  212. // 存储每个页面的标题
  213. pageTitles: [],
  214. // 存储每个页面的type信息
  215. pageTypes: [],
  216. // 存储每个页面的单词释义数据
  217. pageWords: [],
  218. // 存储每个页面的付费状态
  219. pagePay: [],
  220. }
  221. },
  222. computed: {
  223. displayCourseList() {
  224. return this.isReversed ? [...this.courseList].reverse() : this.courseList;
  225. },
  226. // 判断当前页面是否为文字类型
  227. isTextPage() {
  228. // 如果是卡片页面(type为'1'),不显示音频控制栏
  229. if (this.currentPageType === '1') {
  230. return false;
  231. }
  232. const currentPageData = this.bookPages[this.currentPage - 1];
  233. // currentPageData是一个数组 其中的一个元素的type是text就会返回true
  234. return currentPageData && currentPageData.some(item => item.type === 'text');
  235. },
  236. // 判断当前页面是否需要加载音频(包括文本页面和卡片页面)
  237. shouldLoadAudio() {
  238. // 文本页面需要加载音频
  239. if (this.isTextPage) {
  240. return true;
  241. }
  242. // 卡片页面(type为'1')也需要加载音频以支持点击播放
  243. if (this.currentPageType === '1') {
  244. return true;
  245. }
  246. return false;
  247. },
  248. // 动态页面标题
  249. currentPageTitle() {
  250. return this.pageTitles[this.currentPage - 1] || this.bookTitle;
  251. },
  252. // 当前页面类型
  253. currentPageType() {
  254. return this.pageTypes[this.currentPage - 1] || '';
  255. },
  256. // 当前页面的单词释义数据
  257. currentPageWords() {
  258. return this.pageWords[this.currentPage - 1] || [];
  259. },
  260. // 当前页面是否需要会员
  261. currentPageRequiresMember() {
  262. return this.pagePay[this.currentPage - 1] === 'Y';
  263. }
  264. },
  265. // watch: {
  266. // scrollTops: {
  267. // handler(newVal, oldVal) {
  268. // console.log('📊 scrollTops变化:', {
  269. // currentPage: this.currentPage,
  270. // newScrollTops: newVal,
  271. // currentPageScrollTop: newVal[this.currentPage - 1]
  272. // });
  273. // },
  274. // deep: true
  275. // }
  276. // },
  277. methods: {
  278. // 处理scroll-view滚动事件
  279. onScroll(e) {
  280. // 更新当前页面的滚动位置
  281. const scrollTop = e.detail.scrollTop;
  282. const currentPageIndex = this.currentPage - 1;
  283. // 只有当滚动位置发生显著变化时才更新
  284. if (Math.abs((this.scrollTops[currentPageIndex] || 0) - scrollTop) > 5) {
  285. this.$set(this.scrollTops, currentPageIndex, scrollTop);
  286. }
  287. },
  288. // 视频事件处理方法
  289. onVideoLoadStart() {
  290. this.videoLoading = true;
  291. },
  292. onVideoCanPlay() {
  293. this.videoLoading = false;
  294. },
  295. onVideoError() {
  296. this.videoLoading = false;
  297. uni.showToast({
  298. title: '视频加载失败',
  299. icon: 'none',
  300. duration: 2000
  301. });
  302. },
  303. // 獲取用戶會員信息 判斷是否和傳參傳過來的會員id相同
  304. async getMemberInfo(){
  305. const memberRes = await this.$api.member.getUserMemberInfo()
  306. if (memberRes.code === 200) {
  307. this.isMember = memberRes.result.map(item => item.memberId).includes(this.memberId)
  308. }
  309. },
  310. // 处理AudioControls组件的事件
  311. onAudioStateChange(audioState) {
  312. // 更新高亮状态
  313. this.currentHighlightIndex = audioState.currentHighlightIndex;
  314. // 更新音频加载状态(用于控制UI显示)
  315. if (audioState.hasOwnProperty('isLoading')) {
  316. this.isAudioLoading = audioState.isLoading;
  317. }
  318. // 更新音频数据状态
  319. if (audioState.hasOwnProperty('hasAudioData')) {
  320. this.hasAudioData = audioState.hasAudioData;
  321. }
  322. // 更新音频加载失败状态
  323. if (audioState.hasOwnProperty('audioLoadFailed')) {
  324. this.audioLoadFailed = audioState.audioLoadFailed;
  325. }
  326. },
  327. // 处理页面数据需要重新加载的事件
  328. async onPageDataNeeded(pageNumber) {
  329. console.log('收到页面数据需要重新加载的请求,页面:', pageNumber);
  330. // 如果页面数据不存在或为空,重新获取
  331. if (!this.bookPages || this.bookPages.length === 0 || !this.bookPages[pageNumber - 1]) {
  332. console.log('页面数据不存在,重新获取页面数据');
  333. try {
  334. await this.getBookPages();
  335. console.log('页面数据重新获取完成');
  336. // 页面数据更新后,AudioControls组件的bookPages监听器会自动触发音频获取
  337. // 无需手动调用getCurrentPageAudio,避免重复调用
  338. } catch (error) {
  339. console.error('重新获取页面数据失败:', error);
  340. }
  341. } else {
  342. console.log('页面数据已存在,无需重新获取');
  343. }
  344. },
  345. // 处理音色切换完成事件
  346. onVoiceChangeComplete(data) {
  347. // 可以在这里添加一些UI反馈,比如显示切换成功的提示
  348. if (data.hasAudioData) {
  349. } else {
  350. }
  351. // 如果启用了预加载所有页面
  352. if (data.preloadAllPages) {
  353. // 可以显示一个提示,告诉用户正在后台加载
  354. uni.showToast({
  355. title: '正在加载新音色...',
  356. icon: 'loading',
  357. duration: 2000
  358. });
  359. }
  360. },
  361. // 处理音色切换错误事件
  362. onVoiceChangeError(error) {
  363. console.error('音色切换失败:', error);
  364. // 可以在这里显示错误提示给用户
  365. uni.showToast({
  366. title: '音色切换失败,请重试',
  367. icon: 'none',
  368. duration: 2000
  369. });
  370. },
  371. // 处理音频切换时的自动滚动
  372. // onScrollToText(refName) {
  373. // try {
  374. // console.log('🎯 onScrollToText 被调用:', refName);
  375. //
  376. // // 调用scrollTo插件
  377. // this.$scrollTo(refName);
  378. //
  379. // } catch (error) {
  380. // console.error('❌ onScrollToText 执行失败:', error);
  381. // }
  382. // },
  383. // 处理文本点击事件
  384. handleTextClick(textContent, item, pageIndex) {
  385. // console.log('🎯 ===== 文本点击事件开始 =====');
  386. // console.log('📝 点击文本:', textContent);
  387. // console.log('📄 textContent类型:', typeof textContent);
  388. // console.log('❓ textContent是否为undefined:', textContent === undefined);
  389. // console.log('📦 完整item对象:', item);
  390. // console.log('📝 item.content:', item ? item.content : 'item为空');
  391. // console.log('📖 当前页面索引:', this.currentPage);
  392. // console.log('👆 点击的页面索引:', pageIndex);
  393. // console.log('📊 当前页面类型:', this.currentPageType);
  394. // console.log('📄 是否为文本页面:', this.isTextPage);
  395. // console.log('📋 当前页面数据:', this.bookPages[this.currentPage - 1]);
  396. // console.log('📏 页面数据长度:', this.bookPages[this.currentPage - 1] ? this.bookPages[this.currentPage - 1].length : '页面不存在');
  397. // 检查音频播放状态
  398. // console.log('🎵 ===== 音频状态检查 =====');
  399. // console.log(' isWordAudioPlaying:', this.isWordAudioPlaying);
  400. // console.log(' currentWordAudio存在:', !!this.currentWordAudio);
  401. // console.log(' currentWordMeaning存在:', !!this.currentWordMeaning);
  402. if (this.isWordAudioPlaying) {
  403. // console.log('⚠️ 检测到单词音频正在播放状态,这可能会阻止句子音频播放');
  404. // console.log('🔄 尝试重置音频播放状态...');
  405. this.isWordAudioPlaying = false;
  406. // console.log('✅ 音频播放状态已重置');
  407. }
  408. // 检查是否点击的是当前页面
  409. if (pageIndex !== undefined && pageIndex !== this.currentPage - 1) {
  410. console.warn('⚠️ 点击的不是当前页面,忽略点击事件');
  411. // console.log(` 期望页面: ${this.currentPage - 1}, 点击页面: ${pageIndex}`);
  412. return;
  413. }
  414. // 验证参数有效性
  415. if (!item) {
  416. console.error('❌ handleTextClick: item参数为空');
  417. uni.showToast({
  418. title: '数据错误,请刷新页面',
  419. icon: 'none'
  420. });
  421. return;
  422. }
  423. // 如果textContent为undefined,尝试从item中获取
  424. if (!textContent && item && item.content) {
  425. textContent = item.content;
  426. // console.log('🔄 从item中获取到content:', textContent);
  427. }
  428. // 最终验证textContent
  429. if (!textContent || typeof textContent !== 'string' || textContent.trim() === '') {
  430. console.error('❌ handleTextClick: 无效的文本内容', textContent);
  431. // console.log(' textContent:', textContent);
  432. // console.log(' 类型:', typeof textContent);
  433. // console.log(' 是否为空字符串:', textContent === '');
  434. // console.log(' trim后是否为空:', textContent && textContent.trim() === '');
  435. uni.showToast({
  436. title: '文本内容无效',
  437. icon: 'none'
  438. });
  439. return;
  440. }
  441. // console.log('✅ 文本内容验证通过:', textContent);
  442. // console.log(' 文本长度:', textContent.length);
  443. // console.log(' 文本预览:', textContent.substring(0, 50) + (textContent.length > 50 ? '...' : ''));
  444. // 检查是否有音频控制组件的引用
  445. // console.log('🔍 检查音频控制组件引用...');
  446. // console.log(' customTabbar存在:', !!this.$refs.customTabbar);
  447. if (!this.$refs.customTabbar) {
  448. console.error('❌ customTabbar引用不存在');
  449. uni.showToast({
  450. title: '音频控制组件未准备好',
  451. icon: 'none'
  452. });
  453. return;
  454. }
  455. // console.log(' audioControls存在:', !!this.$refs.customTabbar.$refs.audioControls);
  456. if (!this.$refs.customTabbar.$refs.audioControls) {
  457. console.error('❌ audioControls引用不存在');
  458. uni.showToast({
  459. title: '音频控制组件未准备好',
  460. icon: 'none'
  461. });
  462. return;
  463. }
  464. // 检查当前页面是否为文本页面或卡片页面
  465. // 卡片页面(type为'1')现在也支持整句音频播放
  466. // 特别针对划线重点页面的调试
  467. if (this.currentPageType === '1') {
  468. }
  469. if (!this.isTextPage && this.currentPageType !== '1') {
  470. console.warn('⚠️ 当前页面不是文本页面或卡片页面');
  471. uni.showToast({
  472. title: '当前页面不支持音频播放',
  473. icon: 'none'
  474. });
  475. return;
  476. }
  477. // 获取音频控制组件实例
  478. const audioControls = this.$refs.customTabbar.$refs.audioControls;
  479. // 检查音频是否正在加载中
  480. if (audioControls.isAudioLoading) {
  481. uni.showToast({
  482. title: '音频正在加载中,请稍后再试',
  483. icon: 'loading',
  484. duration: 1500
  485. });
  486. // 等待音频加载完成后自动播放
  487. const checkAndPlay = () => {
  488. if (!audioControls.isAudioLoading && audioControls.currentPageAudios.length > 0) {
  489. const success = audioControls.playSpecificAudio(textContent);
  490. if (!success) {
  491. console.error('❌ 音频加载完成后播放失败');
  492. }
  493. } else if (!audioControls.isAudioLoading) {
  494. console.error('❌ 音频加载完成但没有音频数据');
  495. uni.showToast({
  496. title: '当前页面没有音频内容',
  497. icon: 'none'
  498. });
  499. } else {
  500. // 继续等待
  501. setTimeout(checkAndPlay, 500);
  502. }
  503. };
  504. // 延迟检查,给音频加载一些时间
  505. setTimeout(checkAndPlay, 1000);
  506. return;
  507. }
  508. // 检查是否有音频数据
  509. if (!audioControls.currentPageAudios || audioControls.currentPageAudios.length === 0) {
  510. console.warn('⚠️ 当前页面没有音频数据,尝试重新加载');
  511. uni.showToast({
  512. title: '正在重新加载音频...',
  513. icon: 'loading'
  514. });
  515. // 尝试重新加载音频
  516. audioControls.getCurrentPageAudio();
  517. // 等待重新加载完成后播放
  518. const retryPlay = () => {
  519. if (!audioControls.isAudioLoading && audioControls.currentPageAudios.length > 0) {
  520. const success = audioControls.playSpecificAudio(textContent);
  521. if (!success) {
  522. console.error('❌ 音频重新加载后播放失败');
  523. }
  524. } else if (!audioControls.isAudioLoading) {
  525. console.error('❌ 音频重新加载失败');
  526. uni.showToast({
  527. title: '音频加载失败,请检查网络连接',
  528. icon: 'none'
  529. });
  530. } else {
  531. // 继续等待
  532. setTimeout(retryPlay, 500);
  533. }
  534. };
  535. setTimeout(retryPlay, 1500);
  536. return;
  537. }
  538. // 调用AudioControls组件的播放指定音频方法
  539. const success = audioControls.playSpecificAudio(textContent);
  540. // console.log('🎵 playSpecificAudio 返回结果:', success);
  541. if (success) {
  542. // console.log('✅ 成功播放指定音频段落');
  543. } else {
  544. console.error('❌ 播放指定音频段落失败');
  545. // console.log('💡 失败可能原因:');
  546. // console.log(' 1. 文本内容与音频数据不匹配');
  547. // console.log(' 2. 音频数据尚未加载完成');
  548. // console.log(' 3. 音频文件路径错误或文件损坏');
  549. // console.log(' 4. 网络连接问题');
  550. }
  551. // console.log('🎯 ===== 文本点击事件结束 =====');
  552. },
  553. onHighlightChange(highlightData) {
  554. // 兼容旧格式(直接传递索引)和新格式(传递对象)
  555. if (typeof highlightData === 'number') {
  556. // 旧格式:直接是索引
  557. this.currentHighlightIndex = highlightData;
  558. } else if (typeof highlightData === 'object' && highlightData !== null) {
  559. // 新格式:包含详细信息的对象
  560. this.currentHighlightIndex = highlightData.highlightIndex;
  561. // 可以在这里处理分段音频的额外信息
  562. if (highlightData.isSegmented) {
  563. // console.log('分段音频高亮:', {
  564. // highlightIndex: highlightData.highlightIndex,
  565. // segmentIndex: highlightData.segmentIndex,
  566. // startIndex: highlightData.startIndex,
  567. // endIndex: highlightData.endIndex,
  568. // currentText: highlightData.currentText
  569. // });
  570. }
  571. } else {
  572. // 清除高亮
  573. this.currentHighlightIndex = -1;
  574. }
  575. },
  576. // 处理滚动到高亮文本
  577. onScrollToText(scrollData) {
  578. // 防抖处理:如果正在滚动中,清除之前的定时器
  579. if (this.scrollDebounceTimer) {
  580. clearTimeout(this.scrollDebounceTimer);
  581. }
  582. this.scrollDebounceTimer = setTimeout(() => {
  583. this.performScrollToText(scrollData);
  584. }, 100); // 100ms防抖延迟
  585. },
  586. // 执行滚动到高亮文本的具体逻辑
  587. performScrollToText(scrollData) {
  588. if (!scrollData || typeof scrollData.highlightIndex !== 'number' || scrollData.highlightIndex < 0) {
  589. console.warn('滚动数据无效:', scrollData);
  590. return;
  591. }
  592. // 确保在当前页面
  593. if (scrollData.currentPage && scrollData.currentPage !== this.currentPage) {
  594. console.warn('页面不匹配,跳过滚动:', {
  595. scrollDataPage: scrollData.currentPage,
  596. currentPage: this.currentPage
  597. });
  598. return;
  599. }
  600. // 如果正在滚动中,跳过本次滚动
  601. if (this.isScrolling) {
  602. console.warn('正在滚动中,跳过本次滚动');
  603. return;
  604. }
  605. // 构建元素选择器
  606. let selector = '';
  607. if (scrollData.isSegmented && typeof scrollData.segmentIndex === 'number') {
  608. // 分段音频:使用分段索引
  609. selector = `#text-segment-${scrollData.segmentIndex}`;
  610. } else {
  611. // 普通音频:需要找到对应的文本元素
  612. // originalTextIndex是指向原始页面数据中的索引,需要映射到实际的DOM元素
  613. const targetItemIndex = this.findTextItemIndex(scrollData.highlightIndex);
  614. if (targetItemIndex !== -1) {
  615. selector = `#text-${targetItemIndex}`;
  616. } else {
  617. console.warn('无法找到对应的文本元素索引:', scrollData.highlightIndex);
  618. selector = `#text-${scrollData.highlightIndex}`; // 备用方案
  619. }
  620. }
  621. console.log('开始滚动到文本:', { selector, scrollData });
  622. // 标记正在滚动
  623. this.isScrolling = true;
  624. // 等待DOM更新后再查找元素
  625. this.$nextTick(() => {
  626. // 使用uni.createSelectorQuery获取元素位置
  627. const query = uni.createSelectorQuery().in(this);
  628. // 获取scroll-view容器的位置信息
  629. query.select('.scroll-container').boundingClientRect();
  630. // 获取目标元素的位置信息
  631. query.select(selector).boundingClientRect();
  632. query.exec((res) => {
  633. const scrollViewRect = res[0];
  634. const targetRect = res[1];
  635. console.log('查询结果:', {
  636. scrollViewRect: scrollViewRect ? '找到' : '未找到',
  637. targetRect: targetRect ? '找到' : '未找到',
  638. selector
  639. });
  640. if (scrollViewRect && targetRect) {
  641. // 计算目标元素相对于scroll-view的位置
  642. const currentScrollTop = this.scrollTops[this.currentPage - 1] || 0;
  643. const targetOffsetTop = targetRect.top - scrollViewRect.top + currentScrollTop;
  644. // 计算滚动位置,让目标元素在屏幕上方1/4处(更好的阅读体验)
  645. const screenHeight = uni.getSystemInfoSync().windowHeight;
  646. const targetScrollTop = targetOffsetTop - screenHeight / 4;
  647. // 更新scroll-view的滚动位置
  648. const finalScrollTop = Math.max(0, targetScrollTop);
  649. // 检查是否需要滚动(避免不必要的滚动)
  650. const currentScroll = this.scrollTops[this.currentPage - 1] || 0;
  651. const scrollDifference = Math.abs(finalScrollTop - currentScroll);
  652. if (scrollDifference > 30) { // 降低滚动阈值,提高响应性
  653. this.$set(this.scrollTops, this.currentPage - 1, finalScrollTop);
  654. // 滚动完成后重置状态
  655. setTimeout(() => {
  656. this.isScrolling = false;
  657. }, 300); // 稍微减少等待时间
  658. console.log('✅ 滚动到高亮文本:', {
  659. selector,
  660. targetOffsetTop,
  661. finalScrollTop,
  662. currentPage: this.currentPage,
  663. scrollDifference
  664. });
  665. } else {
  666. // 不需要滚动,立即重置状态
  667. this.isScrolling = false;
  668. console.log('📍 目标已在视野内,无需滚动');
  669. }
  670. } else {
  671. console.error('❌ 未找到目标元素或scroll-view:', {
  672. selector,
  673. scrollViewFound: !!scrollViewRect,
  674. targetFound: !!targetRect,
  675. currentPage: this.currentPage,
  676. highlightIndex: scrollData.highlightIndex
  677. });
  678. this.isScrolling = false;
  679. // 尝试备用方案:直接滚动到页面顶部附近
  680. if (!targetRect) {
  681. console.log('🔄 尝试备用滚动方案');
  682. const fallbackScrollTop = scrollData.highlightIndex * 100; // 简单估算位置
  683. this.$set(this.scrollTops, this.currentPage - 1, fallbackScrollTop);
  684. setTimeout(() => {
  685. this.isScrolling = false;
  686. }, 300);
  687. }
  688. }
  689. });
  690. });
  691. },
  692. // 查找文本元素在页面中的实际索引
  693. findTextItemIndex(originalTextIndex) {
  694. const currentPageData = this.bookPages[this.currentPage - 1];
  695. if (!currentPageData || !Array.isArray(currentPageData)) {
  696. return -1;
  697. }
  698. let textCount = 0;
  699. for (let i = 0; i < currentPageData.length; i++) {
  700. const item = currentPageData[i];
  701. if (item && item.type === 'text' && item.content) {
  702. if (textCount === originalTextIndex) {
  703. return i; // 返回在页面数组中的实际索引
  704. }
  705. textCount++;
  706. }
  707. }
  708. return -1; // 未找到
  709. },
  710. // 获取音色列表 拿第一个做默认的音色id
  711. async getVoiceList() {
  712. const voiceRes = await this.$api.music.list()
  713. if(voiceRes.code === 200){
  714. // console.log('音色列表API返回:', voiceRes.result);
  715. // console.log('第一个音色数据:', voiceRes.result[0]);
  716. this.voiceId = Number(voiceRes.result[0].voiceType)
  717. // console.log('获取默认音色ID:', this.voiceId, '类型:', typeof this.voiceId);
  718. }
  719. },
  720. toggleNavbar() {
  721. this.showNavbar = !this.showNavbar
  722. },
  723. goBack() {
  724. uni.navigateBack()
  725. },
  726. toggleCoursePopup() {
  727. if (this.$refs.coursePopup) {
  728. this.$refs.coursePopup.open()
  729. }
  730. // console.log('123123123');
  731. },
  732. toggleSort() {
  733. this.isReversed = !this.isReversed
  734. },
  735. selectCourse(courseId) {
  736. this.currentCourse = courseId
  737. // 这里可以添加切换课程的逻辑
  738. // console.log('选择课程:', courseId)
  739. this.getCourseList(courseId)
  740. },
  741. showWordMeaning() {
  742. if (this.$refs.meaningPopup) {
  743. this.$refs.meaningPopup.open()
  744. }
  745. },
  746. closeMeaningPopup() {
  747. this.currentWordMeaning = null;
  748. // 重置音频播放状态,确保后续句子点击能正常播放
  749. if (this.isWordAudioPlaying) {
  750. this.isWordAudioPlaying = false;
  751. // 如果有正在播放的音频,停止它
  752. if (this.currentWordAudio) {
  753. try {
  754. this.currentWordAudio.pause();
  755. this.currentWordAudio.destroy();
  756. } catch (error) {
  757. }
  758. this.currentWordAudio = null;
  759. }
  760. }
  761. },
  762. // 将英文句子分割成单词数组
  763. splitEnglishSentence(sentence) {
  764. // 使用正则表达式分割句子,保留标点符号
  765. const tokens = sentence.match(/\b\w+\b|[^\w\s]/g) || [];
  766. return tokens.map((token, index) => ({
  767. text: token,
  768. index: index,
  769. isWord: /\b\w+\b/.test(token), // 判断是否为单词
  770. hasDefinition: false // 是否有释义,稍后会设置
  771. }));
  772. },
  773. // 查找单词释义
  774. findWordDefinition(word) {
  775. const currentPageWords = this.pageWords[this.currentPage - 1] || [];
  776. // 不区分大小写匹配
  777. return currentPageWords.find(wordData =>
  778. wordData.word.toLowerCase() === word.toLowerCase()
  779. );
  780. },
  781. // 处理中文文本,标记重点词汇
  782. processChineseText(text) {
  783. const currentPageWords = this.pageWords[this.currentPage - 1] || [];
  784. if (!text || currentPageWords.length === 0) {
  785. return [{ text: text, isKeyword: false, keywordData: null }];
  786. }
  787. // 创建一个数组来存储处理后的文本片段
  788. const segments = [];
  789. let currentIndex = 0;
  790. // 按照重点词汇的长度排序,优先匹配较长的词汇
  791. const sortedWords = [...currentPageWords].sort((a, b) => b.word.length - a.word.length);
  792. while (currentIndex < text.length) {
  793. let matched = false;
  794. // 尝试匹配重点词汇
  795. for (const wordData of sortedWords) {
  796. const keyword = wordData.word;
  797. if (text.substr(currentIndex, keyword.length) === keyword) {
  798. // 找到匹配的重点词汇
  799. segments.push({
  800. text: keyword,
  801. isKeyword: true,
  802. keywordData: wordData
  803. });
  804. currentIndex += keyword.length;
  805. matched = true;
  806. break;
  807. }
  808. }
  809. if (!matched) {
  810. // 没有匹配到重点词汇,添加单个字符
  811. segments.push({
  812. text: text[currentIndex],
  813. isKeyword: false,
  814. keywordData: null
  815. });
  816. currentIndex++;
  817. }
  818. }
  819. // 合并相邻的非重点词汇片段
  820. const mergedSegments = [];
  821. let currentSegment = null;
  822. for (const segment of segments) {
  823. if (segment.isKeyword) {
  824. // 如果当前有未完成的非重点词汇片段,先添加它
  825. if (currentSegment) {
  826. mergedSegments.push(currentSegment);
  827. currentSegment = null;
  828. }
  829. // 添加重点词汇
  830. mergedSegments.push(segment);
  831. } else {
  832. // 非重点词汇,合并到当前片段
  833. if (currentSegment) {
  834. currentSegment.text += segment.text;
  835. } else {
  836. currentSegment = { ...segment };
  837. }
  838. }
  839. }
  840. // 添加最后的非重点词汇片段
  841. if (currentSegment) {
  842. mergedSegments.push(currentSegment);
  843. }
  844. return mergedSegments;
  845. },
  846. async playWordAudio(word) {
  847. try {
  848. // 检查是否有音频正在播放,如果有则停止
  849. if (this.isWordAudioPlaying) {
  850. this.isWordAudioPlaying = false;
  851. }
  852. // 🎯 全局音频管理:停止主音频播放,确保只有一个音频播放
  853. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  854. const audioControls = this.$refs.customTabbar.$refs.audioControls;
  855. if (audioControls.isPlaying) {
  856. audioControls.pauseAudio();
  857. }
  858. }
  859. // 停止當前播放的單詞語音
  860. if (this.currentWordAudio) {
  861. try {
  862. // 先暂停,再销毁,确保清理完整
  863. this.currentWordAudio.pause();
  864. this.currentWordAudio.destroy();
  865. } catch (error) {
  866. }
  867. this.currentWordAudio = null;
  868. this.isWordAudioPlaying = false;
  869. } else {
  870. }
  871. // 🎯 确保音色ID已加载完成后再获取音频
  872. if (!this.voiceId || this.voiceId === '' || this.voiceId === null || this.voiceId === undefined) {
  873. uni.showToast({
  874. title: '音色未加载,请稍后重试',
  875. icon: 'none',
  876. duration: 2000
  877. });
  878. return;
  879. }
  880. // 調用語音轉換API
  881. const audioRes = await this.$api.music.textToVoice({
  882. text: word,
  883. voiceType: this.voiceId
  884. });
  885. // 檢查響應並播放音頻
  886. if (audioRes && audioRes.result && audioRes.result.url) {
  887. // 新格式:使用返回的url字段
  888. const audioUrl = audioRes.result.url;
  889. // 創建並播放音頻
  890. const audio = uni.createInnerAudioContext();
  891. audio.src = audioUrl;
  892. // 添加音频加载完成监听器
  893. audio.onCanplay(() => {
  894. });
  895. audio.onPlay(() => {
  896. this.isWordAudioPlaying = true;
  897. });
  898. audio.onEnded(() => {
  899. this.isWordAudioPlaying = false;
  900. audio.destroy();
  901. if (this.currentWordAudio === audio) {
  902. this.currentWordAudio = null;
  903. } else {
  904. }
  905. });
  906. audio.onError((error) => {
  907. console.error('❌ 音频播放失败:', error);
  908. this.isWordAudioPlaying = false;
  909. console.log('🔄 播放状态已重置为 false (错误)');
  910. try {
  911. audio.destroy();
  912. } catch (destroyError) {
  913. console.error('⚠️ 销毁音频实例时出错:', destroyError);
  914. }
  915. if (this.currentWordAudio === audio) {
  916. this.currentWordAudio = null;
  917. console.log('🔄 currentWordAudio 已重置为 null (错误)');
  918. } else {
  919. }
  920. uni.showToast({
  921. title: '語音播放失敗',
  922. icon: 'none'
  923. });
  924. });
  925. // 保存當前音頻實例並播放
  926. this.currentWordAudio = audio;
  927. // 添加一个小延迟确保音频实例完全准备好
  928. setTimeout(() => {
  929. if (this.currentWordAudio === audio) {
  930. try {
  931. audio.play();
  932. } catch (playError) {
  933. console.error('❌ 播放命令失败:', playError);
  934. uni.showToast({
  935. title: '音频播放失败',
  936. icon: 'none'
  937. });
  938. }
  939. } else {
  940. }
  941. }, 100);
  942. } else {
  943. console.error('❌ API响应无效:', audioRes);
  944. uni.showToast({
  945. title: '語音播放失敗',
  946. icon: 'none'
  947. });
  948. }
  949. } catch (error) {
  950. console.error('❌ 播放单词语音异常:', error);
  951. uni.showToast({
  952. title: '語音播放失敗',
  953. icon: 'none'
  954. });
  955. }
  956. },
  957. // 重複播放單詞語音(用於釋義彈窗中的揚聲器圖標)
  958. repeatWordAudio() {
  959. if (this.currentWordMeaning && this.currentWordMeaning.word) {
  960. // 将单词和解释合并后播放音频
  961. const combinedText = `${this.currentWordMeaning.word}${this.currentWordMeaning.meaning || ''}`;
  962. this.playWordAudio(combinedText);
  963. } else {
  964. console.warn('沒有當前單詞可以播放');
  965. }
  966. },
  967. // 处理单词点击事件
  968. handleWordClick(word) {
  969. const definition = this.findWordDefinition(word);
  970. if (definition) {
  971. this.currentWordMeaning = {
  972. word: definition.word,
  973. phonetic: definition.soundmark || '',
  974. partOfSpeech: '', // 可以根据需要添加词性
  975. meaning: definition.paraphrase || '',
  976. knowledgeGain: definition.knowledge || '',
  977. image: definition.image || ''
  978. };
  979. // 将单词和解释合并后播放音频
  980. const combinedText = `${word}${definition.paraphrase || ''}`;
  981. this.playWordAudio(combinedText);
  982. this.showWordMeaning();
  983. } else {
  984. // 如果没有释义,只播放单词
  985. if (word) {
  986. this.playWordAudio(word);
  987. } else {
  988. }
  989. }
  990. },
  991. // 处理中文重点词汇点击事件
  992. handleChineseKeywordClick(keywordData) {
  993. if (keywordData) {
  994. this.currentWordMeaning = {
  995. word: keywordData.word,
  996. phonetic: keywordData.soundmark || '',
  997. partOfSpeech: '', // 可以根据需要添加词性
  998. meaning: keywordData.paraphrase || '',
  999. knowledgeGain: keywordData.knowledge || ''
  1000. };
  1001. // 将词汇和解释合并后播放音频
  1002. const combinedText = `${keywordData.word}${keywordData.paraphrase || ''}`;
  1003. this.playWordAudio(combinedText);
  1004. this.showWordMeaning();
  1005. } else {
  1006. }
  1007. },
  1008. // 计算音频总时长
  1009. async calculateTotalDuration() {
  1010. let totalDuration = 0;
  1011. for (let i = 0; i < this.currentPageAudios.length; i++) {
  1012. const audio = this.currentPageAudios[i];
  1013. // 優先使用API返回的時長信息
  1014. if (audio.duration && audio.duration > 0) {
  1015. totalDuration += audio.duration;
  1016. continue;
  1017. }
  1018. // 如果沒有API時長信息,嘗試獲取音頻時長
  1019. try {
  1020. const duration = await this.getAudioDuration(audio.url);
  1021. audio.duration = duration;
  1022. totalDuration += duration;
  1023. } catch (error) {
  1024. console.error('获取音频时长失败:', error);
  1025. // 如果无法获取时长,根據文字長度估算(更精確的估算)
  1026. const textLength = audio.text.length;
  1027. // 假設每分鐘可以讀150-200個字符,這裡用180作為平均值
  1028. const estimatedDuration = Math.max(2, textLength / 3); // 每3個字符約1秒
  1029. audio.duration = estimatedDuration;
  1030. totalDuration += estimatedDuration;
  1031. console.log(`估算音頻時長 ${i + 1}:`, estimatedDuration, '秒 (文字長度:', textLength, ')');
  1032. }
  1033. }
  1034. this.totalTime = totalDuration;
  1035. },
  1036. // 获取音频时长
  1037. getAudioDuration(audioUrl) {
  1038. return new Promise((resolve, reject) => {
  1039. const audio = uni.createInnerAudioContext();
  1040. audio.src = audioUrl;
  1041. let resolved = false;
  1042. // 监听音频加载完成事件
  1043. audio.onCanplay(() => {
  1044. if (!resolved && audio.duration && audio.duration > 0) {
  1045. resolved = true;
  1046. resolve(audio.duration);
  1047. audio.destroy();
  1048. }
  1049. });
  1050. // 监听音频元数据加载完成事件
  1051. audio.onLoadedmetadata = () => {
  1052. if (!resolved && audio.duration && audio.duration > 0) {
  1053. resolved = true;
  1054. resolve(audio.duration);
  1055. audio.destroy();
  1056. }
  1057. };
  1058. // 监听音频时长更新事件
  1059. audio.onDurationChange = () => {
  1060. if (!resolved && audio.duration && audio.duration > 0) {
  1061. resolved = true;
  1062. resolve(audio.duration);
  1063. audio.destroy();
  1064. }
  1065. };
  1066. // 如果以上方法都無法獲取時長,嘗試播放一小段來獲取時長
  1067. audio.onPlay(() => {
  1068. if (!resolved) {
  1069. setTimeout(() => {
  1070. if (!resolved && audio.duration && audio.duration > 0) {
  1071. resolved = true;
  1072. resolve(audio.duration);
  1073. audio.destroy();
  1074. }
  1075. }, 100); // 播放100ms後檢查時長
  1076. }
  1077. });
  1078. audio.onError((error) => {
  1079. console.error('音频加载失败:', error);
  1080. if (!resolved) {
  1081. resolved = true;
  1082. reject(error);
  1083. audio.destroy();
  1084. }
  1085. });
  1086. // 設置較長的超時時間,並在超時前嘗試播放
  1087. setTimeout(() => {
  1088. if (!resolved) {
  1089. audio.play();
  1090. }
  1091. }, 1000);
  1092. // 最終超時處理
  1093. setTimeout(() => {
  1094. if (!resolved) {
  1095. console.warn('獲取音頻時長超時,使用默認值');
  1096. resolved = true;
  1097. reject(new Error('获取音频时长超时'));
  1098. audio.destroy();
  1099. }
  1100. }, 5000);
  1101. });
  1102. },
  1103. // 音频控制方法 - 这些方法现在由AudioControls组件处理
  1104. // 保留一些简单的接口方法用于与AudioControls组件通信
  1105. // 判断当前文本是否应该高亮 - 这个方法需要保留,因为它用于模板渲染
  1106. isTextHighlighted(page, index) {
  1107. // 只有当前页面且是文本类型才可能高亮
  1108. if (page !== this.bookPages[this.currentPage - 1]) return false;
  1109. // 计算当前页面中text类型元素的索引
  1110. let textIndex = 0;
  1111. for (let i = 0; i <= index; i++) {
  1112. if (page[i].type === 'text') {
  1113. if (i === index) {
  1114. const shouldHighlight = textIndex === this.currentHighlightIndex;
  1115. if (shouldHighlight) {
  1116. }
  1117. return shouldHighlight;
  1118. }
  1119. textIndex++;
  1120. }
  1121. }
  1122. return false;
  1123. },
  1124. async previousPage() {
  1125. if (this.currentPage > 1) {
  1126. this.currentPage--;
  1127. // 获取对应页面的数据(如果还没有获取过)
  1128. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1129. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1130. }
  1131. }
  1132. },
  1133. async nextPage() {
  1134. if (this.currentPage < this.bookPages.length) {
  1135. this.currentPage++;
  1136. // 获取对应页面的数据(如果还没有获取过)
  1137. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1138. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1139. }
  1140. }
  1141. },
  1142. toggleSound() {
  1143. // 检查是否正在加载音频,如果是则阻止音色切换
  1144. if (this.isAudioLoading) {
  1145. uni.showToast({
  1146. title: '音频加载中,请稍后再试',
  1147. icon: 'none',
  1148. duration: 2000
  1149. });
  1150. return;
  1151. }
  1152. // 检查AudioControls组件是否正在加载音频
  1153. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls && this.$refs.customTabbar.$refs.audioControls.isAudioLoading) {
  1154. uni.showToast({
  1155. title: '音频加载中,请稍后再试',
  1156. icon: 'none',
  1157. duration: 2000
  1158. });
  1159. return;
  1160. }
  1161. console.log('音色切换')
  1162. uni.navigateTo({
  1163. url: '/subPages/home/music?voiceId=' + this.voiceId
  1164. })
  1165. },
  1166. unlockBook() {
  1167. console.log('解锁全书')
  1168. // 这里可以跳转到会员页面或者调用解锁接口
  1169. uni.navigateTo({
  1170. url: '/subPages/member/recharge'
  1171. })
  1172. },
  1173. async goToPage(page) {
  1174. this.currentPage = page
  1175. console.log('跳转到页面:', page)
  1176. // 获取对应页面的数据(如果还没有获取过)
  1177. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1178. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1179. }
  1180. },
  1181. async onSwiperChange(e) {
  1182. this.currentPage = e.detail.current + 1
  1183. // 获取对应页面的数据(如果还没有获取过)
  1184. if (this.courseIdList[this.currentPage - 1] && this.bookPages[this.currentPage - 1].length === 0) {
  1185. await this.getBookPages(this.courseIdList[this.currentPage - 1]);
  1186. }
  1187. },
  1188. async getCourseList(id) {
  1189. const res = await this.$api.book.coursePage({
  1190. id: id
  1191. })
  1192. if (res.code === 200) {
  1193. // 课程切换时,先清理音频控制组件的所有数据
  1194. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1195. this.$refs.customTabbar.$refs.audioControls.resetForCourseChange();
  1196. }
  1197. // 清空当前页面相关数据
  1198. this.currentPage = 1; // 重置到第一页
  1199. this.currentCourse = 1; // 重置当前课程索引
  1200. this.currentWordMeaning = null; // 清空单词释义
  1201. this.currentWordAudio = null; // 清空单词音频
  1202. this.currentHighlightIndex = -1; // 清空高亮索引
  1203. // 清理单词音频缓存
  1204. this.clearWordAudioCache();
  1205. // 重新初始化课程数据
  1206. this.courseIdList = res.result.map(item => item.id)
  1207. // 初始化二维数组 换一种方式
  1208. this.bookPages = this.courseIdList.map(() => [])
  1209. // 初始化标题数组
  1210. this.pageTitles = this.courseIdList.map(() => '')
  1211. // 初始化页面类型数组
  1212. this.pageTypes = this.courseIdList.map(() => '')
  1213. // 初始化页面单词数组
  1214. this.pageWords = this.courseIdList.map(() => [])
  1215. // 初始化滚动位置数组
  1216. this.scrollTops = this.courseIdList.map(() => 0)
  1217. // 初始化第一页
  1218. if (this.courseIdList.length > 0) {
  1219. await this.getBookPages(this.courseIdList[0])
  1220. // 课程切换后,确保音频控件能正确加载新课程的音频
  1221. // 使用$nextTick确保DOM和数据都已更新
  1222. this.$nextTick(async () => {
  1223. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1224. try {
  1225. // 直接调用getCurrentPageAudio方法,更可靠
  1226. await this.$refs.customTabbar.$refs.audioControls.getCurrentPageAudio();
  1227. } catch (error) {
  1228. console.error('课程切换后音频加载失败:', error);
  1229. }
  1230. }
  1231. });
  1232. // 预加载后续几页的内容(异步执行,不阻塞当前页面显示)
  1233. this.preloadNextPages()
  1234. }
  1235. }
  1236. },
  1237. async getBookPages(id) {
  1238. const res = await this.$api.book.coursesPageDetail({
  1239. id: id
  1240. })
  1241. if (res.code === 200) {
  1242. // 使用$set确保响应式更新
  1243. const rawPageData = JSON.parse(res.result.content)
  1244. console.log('获取到的原始页面数据:', rawPageData)
  1245. // 过滤掉无效的数据项
  1246. const filteredPageData = rawPageData.filter(item => {
  1247. return item && typeof item === 'object' && (item.type || item.content)
  1248. })
  1249. console.log('过滤后的页面数据:', filteredPageData)
  1250. // 确保当前页面存在
  1251. if (this.currentPage - 1 < this.bookPages.length) {
  1252. this.$set(this.bookPages, this.currentPage - 1, filteredPageData)
  1253. // 保存页面标题
  1254. this.$set(this.pageTitles, this.currentPage - 1, res.result.title || '')
  1255. // 保存页面类型
  1256. this.$set(this.pageTypes, this.currentPage - 1, res.result.type || '')
  1257. // 保存页面单词释义数据
  1258. this.$set(this.pageWords, this.currentPage - 1, res.result.words || [])
  1259. // 保存页面付费状态
  1260. this.$set(this.pagePay, this.currentPage - 1, res.result.pay || 'N')
  1261. }
  1262. }
  1263. },
  1264. // 获取课程列表
  1265. async getCoursePageList (bookId) {
  1266. const res = await this.$api.book.course({
  1267. id: bookId
  1268. })
  1269. if (res.code === 200) {
  1270. this.courseList = res.result.records
  1271. // 打上序列号
  1272. this.courseList = this.courseList.map((item, index) => ({
  1273. ...item,
  1274. index,
  1275. }))
  1276. }
  1277. },
  1278. // 清理音频缓存
  1279. clearAudioCache() {
  1280. this.audioCache = {};
  1281. },
  1282. // 清理單詞語音緩存
  1283. clearWordAudioCache() {
  1284. this.wordAudioCache = {};
  1285. },
  1286. // 限制缓存大小,保留最近访问的页面
  1287. limitCacheSize(maxSize = 10) {
  1288. const cacheKeys = Object.keys(this.audioCache);
  1289. if (cacheKeys.length > maxSize) {
  1290. // 删除最旧的缓存项
  1291. const keysToDelete = cacheKeys.slice(0, cacheKeys.length - maxSize);
  1292. keysToDelete.forEach(key => {
  1293. delete this.audioCache[key];
  1294. });
  1295. }
  1296. },
  1297. // 预加载后续页面内容
  1298. async preloadNextPages() {
  1299. try {
  1300. // 优化策略:只预加载接下来的2-3页内容,避免过多请求
  1301. const preloadCount = Math.min(3, this.courseIdList.length - 1); // 预加载3页或剩余页数
  1302. // 串行预加载,避免并发请求过多
  1303. for (let i = 1; i <= preloadCount; i++) {
  1304. if (i < this.courseIdList.length && this.bookPages[i].length === 0) {
  1305. try {
  1306. await this.preloadSinglePage(this.courseIdList[i], i);
  1307. // 每页之间间隔800ms,给服务器更多缓冲时间
  1308. if (i < preloadCount) {
  1309. await new Promise(resolve => setTimeout(resolve, 800));
  1310. }
  1311. } catch (error) {
  1312. console.error(`预加载第${i + 1}页失败:`, error);
  1313. // 继续预加载下一页
  1314. }
  1315. }
  1316. }
  1317. // 延迟1.5秒后再通知AudioControls组件开始预加载音频,避免接口冲突
  1318. setTimeout(() => {
  1319. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1320. this.$refs.customTabbar.$refs.audioControls.startPreloadAudio();
  1321. }
  1322. }, 1500);
  1323. } catch (error) {
  1324. console.error('预加载页面内容失败:', error);
  1325. }
  1326. },
  1327. // 预加载单个页面
  1328. async preloadSinglePage(courseId, pageIndex) {
  1329. try {
  1330. const res = await this.$api.book.coursesPageDetail({
  1331. id: courseId
  1332. });
  1333. if (res.code === 200) {
  1334. const rawPageData = JSON.parse(res.result.content);
  1335. const filteredPageData = rawPageData.filter(item => {
  1336. return item && typeof item === 'object' && (item.type || item.content);
  1337. });
  1338. // 使用$set确保响应式更新
  1339. this.$set(this.bookPages, pageIndex, filteredPageData);
  1340. this.$set(this.pageTitles, pageIndex, res.result.title || '');
  1341. this.$set(this.pageTypes, pageIndex, res.result.type || '');
  1342. this.$set(this.pageWords, pageIndex, res.result.words || []);
  1343. this.$set(this.pagePay, pageIndex, res.result.pay || 'N');
  1344. }
  1345. } catch (error) {
  1346. console.error(`预加载第${pageIndex + 1}页失败:`, error);
  1347. throw error;
  1348. }
  1349. },
  1350. // 自動加載第一頁音頻並播放
  1351. async autoLoadAndPlayFirstPage() {
  1352. try {
  1353. // 確保當前是第一頁且需要加載音頻
  1354. if (this.currentPage === 1 && this.shouldLoadAudio) {
  1355. // 加載音頻
  1356. await this.getCurrentPageAudio();
  1357. // 檢查是否成功加載音頻
  1358. if (this.currentPageAudios && this.currentPageAudios.length > 0) {
  1359. // getCurrentPageAudio方法已經處理了第一個音頻的播放,這裡不需要再次調用playAudio
  1360. } else {
  1361. }
  1362. } else {
  1363. }
  1364. } catch (error) {
  1365. console.error('自動加載和播放音頻失敗:', error);
  1366. }
  1367. },
  1368. },
  1369. async onLoad(args) {
  1370. this.$scrollTo('imageRef')
  1371. // 监听音色切换事件,传递给AudioControls组件处理
  1372. uni.$on('selectVoice', async (voiceId) => {
  1373. if (this.voiceId === voiceId) {
  1374. return;
  1375. }
  1376. // 检查是否正在加载音频,如果是则阻止音色切换
  1377. if (this.isAudioLoading || (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls && this.$refs.customTabbar.$refs.audioControls.isAudioLoading)) {
  1378. uni.showToast({
  1379. title: '音频加载中,请稍后再试',
  1380. icon: 'none',
  1381. duration: 2000
  1382. });
  1383. return;
  1384. }
  1385. // 更新本地音色ID
  1386. this.voiceId = voiceId;
  1387. // 清理單詞語音資源
  1388. this.clearWordAudioCache();
  1389. if (this.currentWordAudio) {
  1390. this.currentWordAudio.destroy();
  1391. this.currentWordAudio = null;
  1392. }
  1393. // 通知AudioControls组件处理音色切换
  1394. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1395. try {
  1396. // 传入选项:preloadAllPages: true 表示要预加载所有页面的音频
  1397. await this.$refs.customTabbar.$refs.audioControls.handleVoiceChange(voiceId, {
  1398. preloadAllPages: true
  1399. });
  1400. } catch (error) {
  1401. console.error('音色切换处理失败:', error);
  1402. }
  1403. }
  1404. })
  1405. this.courseId = args.courseId
  1406. this.memberId = args.memberId
  1407. // 先获取点进来的课程的页面列表
  1408. await Promise.all([this.getVoiceList(),this.getMemberInfo(),this.getCourseList(this.courseId), this.getCoursePageList(args.bookId)])
  1409. // 页面加载完成后,通知AudioControls组件自动加载第一页音频
  1410. this.$nextTick(() => {
  1411. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1412. this.$refs.customTabbar.$refs.audioControls.autoLoadAndPlayFirstPage();
  1413. }
  1414. });
  1415. },
  1416. // 页面卸载时清理资源
  1417. onUnload() {
  1418. uni.$off('selectVoice')
  1419. // 0. 清理滚动防抖定时器
  1420. if (this.scrollDebounceTimer) {
  1421. clearTimeout(this.scrollDebounceTimer);
  1422. this.scrollDebounceTimer = null;
  1423. }
  1424. // 1. 清理单词语音资源
  1425. if (this.currentWordAudio) {
  1426. try {
  1427. this.currentWordAudio.destroy();
  1428. } catch (error) {
  1429. console.error('销毁单词音频实例失败:', error);
  1430. }
  1431. this.currentWordAudio = null;
  1432. }
  1433. // 2. 清理单词语音缓存
  1434. this.clearWordAudioCache();
  1435. // 3. 停止单词音频播放状态
  1436. this.isWordAudioPlaying = false;
  1437. // 4. 通知AudioControls组件清理资源
  1438. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1439. this.$refs.customTabbar.$refs.audioControls.destroyAudio();
  1440. }
  1441. // 5. 清理全局音频实例(防止遗漏)
  1442. try {
  1443. // 获取所有可能的音频上下文并销毁
  1444. if (typeof wx !== 'undefined' && wx.getBackgroundAudioManager) {
  1445. const bgAudio = wx.getBackgroundAudioManager();
  1446. if (bgAudio) {
  1447. bgAudio.stop();
  1448. }
  1449. }
  1450. } catch (error) {
  1451. console.error('清理背景音频失败:', error);
  1452. }
  1453. },
  1454. // 页面隐藏时暂停音频
  1455. onHide() {
  1456. // 1. 暂停单词音频
  1457. if (this.currentWordAudio && this.isWordAudioPlaying) {
  1458. try {
  1459. this.currentWordAudio.pause();
  1460. this.isWordAudioPlaying = false;
  1461. } catch (error) {
  1462. console.error('暂停单词音频失败:', error);
  1463. }
  1464. }
  1465. // 2. 通知AudioControls组件暂停音频
  1466. if (this.$refs.customTabbar && this.$refs.customTabbar.$refs.audioControls) {
  1467. this.$refs.customTabbar.$refs.audioControls.pauseOnHide();
  1468. }
  1469. }
  1470. }
  1471. </script>
  1472. <style lang="scss" scoped>
  1473. .book-container {
  1474. width: 100%;
  1475. min-height: 100vh;
  1476. background-color: #F8F8F8;
  1477. position: relative;
  1478. overflow: hidden;
  1479. }
  1480. .custom-navbar {
  1481. position: fixed;
  1482. top: 0;
  1483. left: 0;
  1484. right: 0;
  1485. background-color: #F8F8F8;
  1486. z-index: 1000;
  1487. transition: transform 0.3s ease;
  1488. &.navbar-hidden {
  1489. transform: translateY(-100%);
  1490. }
  1491. }
  1492. .navbar-content {
  1493. display: flex;
  1494. align-items: center;
  1495. justify-content: space-between;
  1496. padding: 20rpx 32rpx;
  1497. // padding-top: calc(20rpx + var(--status-bar-height, 0));
  1498. height: 60rpx;
  1499. }
  1500. .navbar-left,
  1501. .navbar-right {
  1502. width: 80rpx;
  1503. display: flex;
  1504. align-items: center;
  1505. }
  1506. .navbar-right {
  1507. justify-content: flex-end;
  1508. flex: 1;
  1509. }
  1510. .navbar-title {
  1511. transform: translateX(-50rpx);
  1512. flex: 1;
  1513. text-align: center;
  1514. font-family: PingFang SC;
  1515. font-weight: 500;
  1516. font-size: 32rpx;
  1517. color: #262626;
  1518. line-height: 48rpx;
  1519. }
  1520. .content-swiper {
  1521. flex: 1;
  1522. // min-height: calc(100vh - 100rpx);
  1523. // margin-top: 100rpx;
  1524. height: 100vh;
  1525. }
  1526. .swiper-item {
  1527. min-height: 100vh;
  1528. // background-color: red;
  1529. }
  1530. .content-area {
  1531. flex: 1;
  1532. padding: 30rpx 40rpx 100rpx;
  1533. /* #ifndef H5 */
  1534. padding: 100rpx 40rpx;
  1535. /* #endif */
  1536. // padding-top: ;
  1537. // background: linear-gradient(180deg, #DEFFFF 0%, #FBFEFF 22.65%, #F0FBFF 100%);
  1538. min-height: 100%;
  1539. box-sizing: border-box;
  1540. overflow-y: auto;
  1541. .title{
  1542. font-family: PingFang SC;
  1543. font-weight: 500;
  1544. font-size: 34rpx;
  1545. text-align: center;
  1546. color: #181818;
  1547. line-height: 48rpx;
  1548. margin-bottom: 32rpx;
  1549. }
  1550. .image-container {
  1551. width: 100%;
  1552. display: flex;
  1553. justify-content: center;
  1554. align-items: center;
  1555. margin: 30rpx 0;
  1556. /* 平板设备适配 */
  1557. @media screen and (min-width: 768px) {
  1558. margin: 40rpx 0;
  1559. }
  1560. }
  1561. .content-image{
  1562. width: 100%;
  1563. height: auto;
  1564. max-width: 600rpx; /* 限制最大宽度,避免在大屏设备上过大 */
  1565. display: block;
  1566. border-radius: 12rpx; /* 添加圆角,提升视觉效果 */
  1567. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1); /* 添加阴影,增强层次感 */
  1568. /* 平板设备适配 */
  1569. @media screen and (min-width: 768px) {
  1570. max-width: 500rpx;
  1571. }
  1572. }
  1573. .video-content{
  1574. width: 100%;
  1575. height: auto;
  1576. margin: 30rpx auto;
  1577. position: relative;
  1578. .video-player{
  1579. // height: 100%;
  1580. width: 100%;
  1581. // margin: 0 auto;
  1582. // height: auto;
  1583. }
  1584. .video-loading {
  1585. position: absolute;
  1586. top: 50%;
  1587. left: 50%;
  1588. transform: translate(-50%, -50%);
  1589. color: #666;
  1590. font-size: 28rpx;
  1591. }
  1592. }
  1593. }
  1594. .card-content {
  1595. background: linear-gradient(180deg, #DEFFFF 0%, #FBFEFF 22.65%, #F0FBFF 100%);
  1596. display: flex;
  1597. flex-direction: column;
  1598. gap: 32rpx;
  1599. min-height: 1172rpx;
  1600. margin-top: 20rpx;
  1601. border-radius: 32rpx;
  1602. // height: 100%;
  1603. padding: 40rpx;
  1604. padding-bottom: 100rpx;
  1605. // margin: 0
  1606. border: 1px solid #FFFFFF;
  1607. box-sizing: border-box;
  1608. .card-line {
  1609. display: flex;
  1610. align-items: center;
  1611. // margin-bottom: 20rpx;
  1612. }
  1613. .card-line-image {
  1614. width: 48rpx;
  1615. height: 48rpx;
  1616. margin-right: 16rpx;
  1617. }
  1618. .card-line-text {
  1619. font-family: PingFang SC;
  1620. font-weight: 600;
  1621. font-size: 30rpx;
  1622. line-height: 48rpx;
  1623. color: #3B3D3D;
  1624. }
  1625. .card-image {
  1626. width: 590rpx;
  1627. height: 268rpx;
  1628. border-radius: 24rpx;
  1629. margin: 30rpx auto;
  1630. // margin-bottom: 20rpx;
  1631. }
  1632. .english-text {
  1633. display: block;
  1634. font-family: PingFang SC;
  1635. font-weight: 600;
  1636. font-size: 32rpx;
  1637. line-height: 48rpx;
  1638. color: #3B3D3D;
  1639. // margin-bottom: 16rpx;
  1640. }
  1641. .english-text-container {
  1642. display: flex;
  1643. flex-wrap: wrap;
  1644. align-items: baseline;
  1645. }
  1646. .english-token {
  1647. font-family: PingFang SC;
  1648. font-weight: 600;
  1649. font-size: 32rpx;
  1650. line-height: 48rpx;
  1651. color: #3B3D3D;
  1652. margin-right: 10rpx;
  1653. }
  1654. .clickable-word {
  1655. background: $primary-color;
  1656. text-decoration: underline;
  1657. cursor: pointer;
  1658. transition: all 0.2s ease;
  1659. }
  1660. .clickable-word:hover {
  1661. background-color: rgba(0, 122, 255, 0.1);
  1662. border-radius: 4rpx;
  1663. }
  1664. .chinese-segment {
  1665. font-family: PingFang SC;
  1666. font-weight: 400;
  1667. font-size: 28rpx;
  1668. line-height: 48rpx;
  1669. color: #3B3D3D;
  1670. }
  1671. .clickable-keyword {
  1672. background: $primary-color;
  1673. text-decoration: underline;
  1674. cursor: pointer;
  1675. transition: all 0.2s ease;
  1676. border-radius: 4rpx;
  1677. padding: 2rpx 4rpx;
  1678. }
  1679. .clickable-keyword:hover {
  1680. background-color: rgba(0, 122, 255, 0.1);
  1681. }
  1682. .chinese-text {
  1683. display: block;
  1684. font-family: PingFang SC;
  1685. font-weight: 400;
  1686. font-size: 28rpx;
  1687. line-height: 48rpx;
  1688. color: #4F4F4F;
  1689. }
  1690. }
  1691. /* 会员限制页面样式 */
  1692. .member-content {
  1693. display: flex;
  1694. flex-direction: column;
  1695. align-items: center;
  1696. justify-content: center;
  1697. height: 90%;
  1698. background-color: #F8F8F8;
  1699. padding: 40rpx;
  1700. margin: -40rpx;
  1701. box-sizing: border-box;
  1702. }
  1703. .member-title {
  1704. font-family: PingFang SC;
  1705. font-weight: 500;
  1706. font-size: 40rpx;
  1707. line-height: 1;
  1708. color: #6f6f6f;
  1709. text-align: center;
  1710. margin-bottom: 48rpx;
  1711. }
  1712. .member-button {
  1713. width: 670rpx;
  1714. height: 72rpx;
  1715. background: #06DADC;
  1716. border-radius: 200rpx;
  1717. display: flex;
  1718. align-items: center;
  1719. justify-content: center;
  1720. }
  1721. .member-button-text {
  1722. font-family: PingFang SC;
  1723. font-weight: 400;
  1724. font-size: 30rpx;
  1725. color: #FFFFFF;
  1726. }
  1727. // .video-content {
  1728. // width: 100%;
  1729. // height: auto;
  1730. // // margin: 200rpx -40rpx 0;
  1731. // // height: 500rpx;
  1732. // background-color: #FFFFFF;
  1733. // // padding: 40rpx;
  1734. // border-radius: 24rpx;
  1735. // display: flex;
  1736. // align-items: center;
  1737. // justify-content: center;
  1738. // .video-player{
  1739. // width: 100%;
  1740. // margin: 0 auto;
  1741. // height: auto;
  1742. // }
  1743. // }
  1744. .text-content {
  1745. width: 100vw;
  1746. background-color: #F6F6F6;
  1747. height: 100%;
  1748. padding: 40rpx;
  1749. margin: -40rpx;
  1750. box-sizing: border-box;
  1751. }
  1752. .content-text {
  1753. font-family: PingFang SC;
  1754. // font-weight: 400;
  1755. font-size: 28rpx;
  1756. color: #3B3D3D;
  1757. line-height: 48rpx;
  1758. letter-spacing: 0;
  1759. text-align: justify;
  1760. word-break: break-all;
  1761. transition: all 0.3s ease;
  1762. }
  1763. .clickable-text {
  1764. cursor: pointer;
  1765. &:active {
  1766. background-color: rgba(6, 218, 220, 0.1);
  1767. border-radius: 4rpx;
  1768. }
  1769. }
  1770. .lead-text {
  1771. background: #fffbe6; /* 柔和的提示背景 */
  1772. border: 1px solid #ffe58f;
  1773. border-radius: 8px;
  1774. padding: 10rpx 20rpx;
  1775. }
  1776. .text-highlight {
  1777. background-color: rgba(255, 248, 220, 0.8); /* 温暖的米黄色,对眼睛友好 */
  1778. border-left: 4rpx solid #ffd700; /* 左侧金色边框作为朗读指示 */
  1779. padding: 4rpx 8rpx;
  1780. border-radius: 6rpx;
  1781. transition: all 0.3s ease;
  1782. box-shadow: 0 2rpx 6rpx rgba(255, 215, 0, 0.15); /* 柔和的阴影 */
  1783. }
  1784. </style>