index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <view class="word-view-page" :style="{backgroundImage: 'url(' + courseBjFun() + ')'}">
  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="ezy-tab-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="collect-btn" v-if="userCode != 'Visitor'" @click="handleShouCang">
  17. <icon :class="{active: data.collectFlag}"></icon>
  18. <view>收藏</view>
  19. </view>
  20. <view class="bottom-btn-box">
  21. <view class="word-view-btn" @click="prevWord" v-if="!isFirst&&isLearnRecord!=0">上一词</view>
  22. <view class="word-view-btn" @click="nextWord" v-if="!isLast&&isLearnRecord!=0">下一词</view>
  23. <view class="word-view-btn" v-if="isLast&&isLearnRecord!=0" @click="handleComplete">完成</view>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script setup>
  29. import mainCardVue from "./components/mainCard.vue";
  30. import {
  31. ref,
  32. reactive,
  33. computed,
  34. nextTick
  35. } from "vue";
  36. import {
  37. onLoad
  38. } from "@dcloudio/uni-app";
  39. import * as httpApi from "@/api/word.js"
  40. import {
  41. getUserIdentity,
  42. } from "@/utils/common.js"
  43. import {
  44. useAudioCache,
  45. audioPlayer
  46. } from "./components/useAudio.js"
  47. import {
  48. getWordCancel
  49. } from "../../api/word";
  50. import cacheManager from "../../utils/cacheManager";
  51. const userCode = getUserIdentity();
  52. const {
  53. cacheAudio,
  54. clearAudioCache
  55. } = useAudioCache();
  56. const AudioP = new audioPlayer();
  57. const isLearnRecord = ref(null)
  58. const isLearnStatus = ref(null)
  59. const isAudioPlaying = ref(false) // 音频播放状态
  60. // 监听音频播放事件
  61. uni.$on('danci-audio-play', (code) => {
  62. isAudioPlaying.value = true
  63. })
  64. uni.$on('danci-audio-ended', () => {
  65. isAudioPlaying.value = false
  66. })
  67. function handleSwiperChange(index) {
  68. if (isAudioPlaying.value) return; // 如果正在播放,不允许切换
  69. // 无论切换到哪个页面,都播放单词的主发音(yinpin)
  70. if (data.activeWord && data.activeWord.yinpin) {
  71. handlePlayAudio({
  72. url: data.activeWord.yinpin,
  73. code: 'swiper-auto-play'
  74. });
  75. }
  76. }
  77. function courseBjFun() {
  78. return 'static/images/course/course-cjdc-bj.png'
  79. }
  80. function chunkArray(arr, chunkSize) {
  81. const result = [];
  82. for (let i = 0; i < arr.length; i += chunkSize) {
  83. result.push(arr.slice(i, i + chunkSize));
  84. }
  85. return result;
  86. }
  87. const data = reactive({
  88. jieId: null, // 节/单元ID
  89. activeId: null, // 当前单词
  90. wordList: [], // 单词列表
  91. arrayList: [], // 整合数据
  92. activeWord: null, // 单词详情数据
  93. collectFlag: 0, // 收藏状态
  94. subjectId: null, // 学科ID
  95. levelId: null, // 等级
  96. typeId: null, // 类型
  97. tipFlag: null, // 提示
  98. youkeZhangId: null, // 章ID
  99. isLearnStatus: null, // 学习记录状态
  100. })
  101. onLoad(({
  102. jieId,
  103. wordId,
  104. subjectId,
  105. levelId,
  106. typeId,
  107. tipFlag,
  108. isLearnStatus,
  109. youkeZhangId
  110. }) => {
  111. data.jieId = jieId;
  112. isLearnRecord.value = jieId;
  113. data.activeId = wordId;
  114. data.isLearnStatus = isLearnStatus;
  115. data.subjectId = subjectId;
  116. data.levelId = levelId;
  117. data.typeId = typeId;
  118. data.tipFlag = tipFlag;
  119. data.youkeZhangId = youkeZhangId;
  120. // 获取单词列表数据
  121. initWordInfo();
  122. // 清理过期文件
  123. clearAudioCache();
  124. })
  125. // 是否是最后一题
  126. const isLast = computed(() => {
  127. if (!data.wordList.length) {
  128. return false
  129. }
  130. return data.activeId == data.wordList[data.wordList.length - 1].id;
  131. })
  132. const isFirst = computed(() => {
  133. if (!data.wordList.length) {
  134. return false
  135. }
  136. return data.activeId == data.wordList[0].id;
  137. })
  138. const activeWords = computed(() => {
  139. if (!data.activeId) {
  140. return null
  141. }
  142. return data.arrayList.find(item => item.some(cit => cit.id == data.activeId))
  143. })
  144. function nextWord() {
  145. const index = data.wordList.findIndex(item => item.id == data.activeId);
  146. if (index < data.wordList.length - 1) {
  147. if (userCode !== 'Visitor') {
  148. uni.redirectTo({
  149. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index + 1].id }`
  150. })
  151. } else {
  152. uni.redirectTo({
  153. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index + 1].id }&levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&youkeZhangId=${data.youkeZhangId}`
  154. })
  155. }
  156. }
  157. }
  158. function prevWord() {
  159. const index = data.wordList.findIndex(item => item.id == data.activeId);
  160. if (index > 0) {
  161. if (userCode !== 'Visitor') {
  162. uni.redirectTo({
  163. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index - 1].id }`
  164. })
  165. } else {
  166. uni.redirectTo({
  167. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index - 1].id }&levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&youkeZhangId=${data.youkeZhangId}`
  168. })
  169. }
  170. }
  171. }
  172. function handleBack() {
  173. // 返回单词列表
  174. if (userCode !== 'Visitor') {
  175. if (data.jieId == 0) {
  176. uni.redirectTo({
  177. url: `/pages/my/learnRecord?jieId=${data.jieId}&isLearnStatus=${data.isLearnStatus}&levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&youkeZhangId=${data.youkeZhangId}`
  178. })
  179. } else {
  180. uni.redirectTo({
  181. url: `/pages/wordList/wordList?jieId=${data.jieId}`
  182. })
  183. }
  184. } else {
  185. const youkePageData = JSON.stringify({
  186. subjectId: data.subjectId,
  187. levelId: data.levelId,
  188. typeId: data.typeId,
  189. tipFlag: data.tipFlag,
  190. youkeZhangId: data.youkeZhangId,
  191. jieId: data.jieId
  192. })
  193. uni.redirectTo({
  194. url: `/pages/wordList/wordList?youkePageData=${youkePageData}`
  195. })
  196. }
  197. }
  198. function handleComplete() {
  199. // 返回岛
  200. if (userCode !== 'Visitor') {
  201. let jieInfo = cacheManager.getUnitInfo('zhangInfo', data.jieId);
  202. if (jieInfo.lastFlag == 1) {
  203. // 更新岛状态
  204. cacheManager.updateObject('zhangInfo', {
  205. curZid: jieInfo.nextZid
  206. })
  207. }
  208. uni.redirectTo({
  209. url: `/pages/study/index`
  210. })
  211. } else {
  212. uni.redirectTo({
  213. url: `/pages/study/index?levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&youkeZhangId=${data.youkeZhangId}&jieId=${data.jieId}`
  214. })
  215. }
  216. }
  217. function handleShouCang() {
  218. if (data.collectFlag == 1) {
  219. httpApi.getWordCancel({
  220. wordId: data.activeId
  221. }).then(res => {
  222. uni.showToast({
  223. title: '操作成功'
  224. })
  225. data.collectFlag = 0;
  226. })
  227. } else {
  228. httpApi.getWordShouCang({
  229. wordId: data.activeId
  230. }).then(res => {
  231. uni.showToast({
  232. title: '操作成功'
  233. })
  234. data.collectFlag = 1;
  235. })
  236. }
  237. }
  238. function initWordInfo() {
  239. if (userCode !== 'Visitor') {
  240. httpApi.getWordInfo({
  241. jieId: data.jieId,
  242. wordId: data.activeId
  243. }).then(res => {
  244. data.activeWord = res.data;
  245. data.wordList = res.data.danciList; // 单词组
  246. data.collectFlag = res.data.collectFlag; // 收藏状态
  247. if (data.wordList.length) {
  248. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  249. }
  250. // console.log('res.data',res.data)
  251. handleCacheAudio()
  252. autoPlayAudio()
  253. })
  254. } else {
  255. httpApi.getCommonWordInfo({
  256. jieId: data.jieId,
  257. wordId: data.activeId
  258. }).then(res => {
  259. data.activeWord = res.data;
  260. data.wordList = res.data.danciList; // 单词组
  261. data.collectFlag = res.data.collectFlag; // 收藏状态
  262. if (data.wordList.length) {
  263. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  264. }
  265. handleCacheAudio()
  266. autoPlayAudio()
  267. })
  268. }
  269. }
  270. // 新增自动播放功能
  271. const autoPlayAudio = async () => {
  272. // 等待数据加载完成
  273. await nextTick();
  274. // 检查是否有单词发音数据
  275. if (data.activeWord && data.activeWord.yinpin) {
  276. // 延迟500ms确保音频已缓存
  277. setTimeout(() => {
  278. handlePlayAudio({
  279. url: data.activeWord.yinpin,
  280. code: 'auto-play' // 特殊标记,区分自动播放
  281. });
  282. }, 500);
  283. }
  284. };
  285. function goXiangjie() {
  286. uni.redirectTo({
  287. url: `/pages/newEnglish/components/xiangjie?levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&wordId=${data.activeId}&youkeZhangId=${data.youkeZhangId}&jieId=${data.jieId}`
  288. })
  289. }
  290. function handleCacheAudio() {
  291. const arr = []
  292. // yinpin
  293. arr.push(data.activeWord.yinpin);
  294. // yinjie
  295. data.activeWord.yinjie.forEach(item => {
  296. arr.push(item.yinpin)
  297. })
  298. // pindu
  299. data.activeWord.pindu.forEach(item => {
  300. arr.push(item.yinpin)
  301. })
  302. // 缓存音频
  303. arr.map(item => cacheAudio(item));
  304. // console.log('arr',arr)
  305. }
  306. async function handlePlayAudio({
  307. url,
  308. code
  309. }) {
  310. console.log('播放', url)
  311. console.log('播放', code)
  312. const cachedPath = await cacheAudio(url);
  313. if (cachedPath && cachedPath.includes('.mp3')) {
  314. // console.log('地址:', cachedPath)
  315. AudioP.play(cachedPath, code);
  316. } else {
  317. uni.showToast({
  318. title: '音频加载失败',
  319. icon: 'none'
  320. });
  321. }
  322. }
  323. </script>
  324. <style>
  325. </style>