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

2555 lines
95 KiB

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