index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <template>
  2. <view class="word-view-page">
  3. <view class="icon-title-navBar-box">
  4. <view @click="handleBack" class="nav-bar-icon"></view>
  5. <text class="nav-bar-title">学习</text>
  6. </view>
  7. <view class="word-view-border">
  8. <template v-for="item in data.wordList">
  9. <mainCardVue :active-word="data.activeWord" :pageData="data" :active-words="activeWords"
  10. @play-audio="handlePlayAudio" @goXiangjie="goXiangjie" @swiper-change="handleSwiperChange"
  11. :is-playing="isAudioPlaying" v-if="item.id == data.activeId" :key="item.id">
  12. </mainCardVue>
  13. </template>
  14. </view>
  15. <view class="word-view-bottom">
  16. <view class="bottom-btn-box">
  17. <view class="word-view-btn" @click="prevWord" v-if="!isFirst&&isLearnRecord!=0">上一词</view>
  18. <view v-else></view>
  19. <view class="word-view-btn" @click="nextWord" v-if="!isLast&&isLearnRecord!=0">下一词</view>
  20. <view class="word-view-btn" v-if="isLast&&isLearnRecord!=0" @click="handleComplete">完成</view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script setup>
  26. import mainCardVue from "./components/mainCard.vue";
  27. import {
  28. ref,
  29. reactive,
  30. computed,
  31. nextTick,
  32. onUnmounted
  33. } from "vue";
  34. import {
  35. onLoad
  36. } from "@dcloudio/uni-app";
  37. import * as httpApi from "@/api/chaojidanci.js"
  38. import {
  39. useAudioCache,
  40. audioPlayer
  41. } from "./components/useAudio.js"
  42. import cacheManager from "@/utils/cacheManager";
  43. const {
  44. cacheAudio,
  45. clearAudioCache
  46. } = useAudioCache();
  47. const AudioP = new audioPlayer();
  48. const isLearnRecord = ref(null)
  49. const isLearnStatus = ref(null)
  50. const isAudioPlaying = ref(false) // 音频播放状态
  51. // 监听音频播放事件
  52. uni.$on('danci-audio-play', updateAudioPlayingTrue)
  53. uni.$on('danci-audio-ended', updateAudioPlayingFalse)
  54. onUnmounted(() => {
  55. uni.$off('danci-audio-play', updateAudioPlayingTrue)
  56. uni.$off('danci-audio-ended', updateAudioPlayingFalse)
  57. })
  58. function updateAudioPlayingFalse() {
  59. isAudioPlaying.value = false
  60. }
  61. function updateAudioPlayingTrue() {
  62. isAudioPlaying.value = true
  63. }
  64. function handleSwiperChange(index) {
  65. return false
  66. // 客户暂时不要
  67. if (isAudioPlaying.value) return; // 如果正在播放,不允许切换
  68. // 无论切换到哪个页面,都播放单词的主发音(yinpin)
  69. if (data.activeWord && data.activeWord.yinpin) {
  70. handlePlayAudio({
  71. url: data.activeWord.yinpin,
  72. code: 'swiper-auto-play'
  73. });
  74. }
  75. }
  76. function courseBjFun() {
  77. return 'static/images/course/course-cjdc-bj.png'
  78. }
  79. function chunkArray(arr, chunkSize) {
  80. const result = [];
  81. for (let i = 0; i < arr.length; i += chunkSize) {
  82. result.push(arr.slice(i, i + chunkSize));
  83. }
  84. return result;
  85. }
  86. const data = reactive({
  87. danyuanId: null, // 节/单元ID
  88. activeId: null, // 当前单词
  89. wordList: [], // 单词列表
  90. arrayList: [], // 整合数据
  91. activeWord: null, // 单词详情数据
  92. collectFlag: 0, // 收藏状态
  93. subjectId: null, // 学科ID
  94. levelId: null, // 等级
  95. typeId: null, // 类型
  96. tipFlag: null, // 提示
  97. youkeZhangId: null, // 章ID
  98. isLearnStatus: null, // 学习记录状态
  99. })
  100. onLoad(({
  101. danyuanId,
  102. wordId,
  103. subjectId,
  104. levelId,
  105. typeId,
  106. tipFlag,
  107. isLearnStatus,
  108. }) => {
  109. data.danyuanId = danyuanId;
  110. isLearnRecord.value = danyuanId;
  111. data.activeId = wordId;
  112. data.isLearnStatus = isLearnStatus;
  113. data.subjectId = subjectId;
  114. data.levelId = levelId;
  115. data.typeId = typeId;
  116. data.tipFlag = tipFlag;
  117. // 获取单词列表数据
  118. initWordInfo();
  119. // 清理过期文件
  120. clearAudioCache();
  121. })
  122. // 是否是最后一题
  123. const isLast = computed(() => {
  124. if (!data.wordList.length) {
  125. return false
  126. }
  127. return data.activeId == data.wordList[data.wordList.length - 1].id;
  128. })
  129. const isFirst = computed(() => {
  130. if (!data.wordList.length) {
  131. return false
  132. }
  133. return data.activeId == data.wordList[0].id;
  134. })
  135. const activeWords = computed(() => {
  136. if (!data.activeId) {
  137. return null
  138. }
  139. return data.arrayList.find(item => item.some(cit => cit.id == data.activeId))
  140. })
  141. function nextWord() {
  142. const index = data.wordList.findIndex(item => item.id == data.activeId);
  143. if (index < data.wordList.length - 1) {
  144. uni.redirectTo({
  145. url: `/pages/chaojidanci/newEnglish/index?danyuanId=${data.danyuanId}&wordId=${data.wordList[index + 1].id}`
  146. })
  147. }
  148. }
  149. function prevWord() {
  150. const index = data.wordList.findIndex(item => item.id == data.activeId);
  151. if (index > 0) {
  152. uni.redirectTo({
  153. url: `/pages/chaojidanci/newEnglish/index?danyuanId=${data.danyuanId}&wordId=${data.wordList[index - 1].id}`
  154. })
  155. }
  156. }
  157. function handleBack() {
  158. // 返回单词列表
  159. uni.navigateBack()
  160. }
  161. function handleComplete() {
  162. // 返回岛
  163. uni.switchTab({
  164. url: `/pages/chanpinneirong/index`
  165. })
  166. }
  167. function initWordInfo() {
  168. httpApi.getWordInfo({
  169. danyuanId: data.danyuanId,
  170. wordId: data.activeId
  171. }).then(res => {
  172. data.activeWord = res.data;
  173. data.wordList = res.data.danciList; // 单词组
  174. data.collectFlag = res.data.collectFlag; // 收藏状态
  175. if (data.wordList.length) {
  176. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  177. }
  178. handleCacheAudio()
  179. })
  180. }
  181. // 新增自动播放功能
  182. const autoPlayAudio = async () => {
  183. // 等待数据加载完成
  184. await nextTick();
  185. // 检查是否有单词发音数据
  186. if (data.activeWord && data.activeWord.yinpin) {
  187. // 延迟500ms确保音频已缓存
  188. setTimeout(() => {
  189. handlePlayAudio({
  190. url: data.activeWord.yinpin,
  191. code: 'auto-play' // 特殊标记,区分自动播放
  192. });
  193. }, 500);
  194. }
  195. };
  196. function goXiangjie() {
  197. uni.redirectTo({
  198. url: `/pages/chaojidanci/newEnglish/components/xiangjie?levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&wordId=${data.activeId}&youkeZhangId=${data.youkeZhangId}&danyuanId=${data.danyuanId}`
  199. })
  200. }
  201. function handleCacheAudio() {
  202. const arr = []
  203. // yinpin
  204. arr.push(data.activeWord.yinpin);
  205. // yinjie
  206. data.activeWord.yinjie.forEach(item => {
  207. arr.push(item.yinpin)
  208. })
  209. // pindu
  210. data.activeWord.pindu.forEach(item => {
  211. arr.push(item.yinpin)
  212. })
  213. // 缓存音频
  214. arr.map(item => cacheAudio(item));
  215. // console.log('arr',arr)
  216. }
  217. async function handlePlayAudio({
  218. url,
  219. code
  220. }) {
  221. console.log('播放', url)
  222. console.log('播放', code)
  223. const cachedPath = await cacheAudio(url);
  224. if (cachedPath && cachedPath.includes('.mp3')) {
  225. // console.log('地址:', cachedPath)
  226. AudioP.play(cachedPath, code);
  227. } else {
  228. uni.showToast({
  229. title: '音频加载失败',
  230. icon: 'none'
  231. });
  232. }
  233. }
  234. </script>
  235. <style>
  236. </style>