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

2164 lines
64 KiB

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