index.vue 9.3 KB

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