index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. <view class="ezy-border-body">
  9. <template v-for="item in data.wordList">
  10. <mainCardVue :active-word="data.activeWord" :pageData="data" :active-words="activeWords"
  11. @play-audio="handlePlayAudio" v-if="item.id == data.activeId" :key="item.id">
  12. </mainCardVue>
  13. </template>
  14. </view>
  15. </view>
  16. <view class="word-view-bottom">
  17. <view class="collect-btn" v-if="userCode != 'Visitor'" @click="handleShouCang">
  18. <icon :class="{active: data.collectFlag}"></icon>
  19. <view>收藏</view>
  20. </view>
  21. <view class="bottom-btn-box">
  22. <view class="word-view-btn" @click="prevWord" v-if="!isFirst&&isLearnRecord!=0">上一词</view>
  23. <view class="word-view-btn" @click="nextWord" v-if="!isLast&&isLearnRecord!=0">下一词</view>
  24. <view class="word-view-btn" v-if="isLast&&isLearnRecord!=0" @click="handleComplete">完成</view>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script setup>
  30. import mainCardVue from "./components/mainCard.vue";
  31. import {
  32. ref,
  33. reactive,
  34. computed
  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. const userCode = getUserIdentity();
  48. const {
  49. cacheAudio,
  50. clearAudioCache
  51. } = useAudioCache();
  52. const AudioP = new audioPlayer();
  53. const isLearnRecord = ref(null)
  54. const isLearnStatus = ref(null)
  55. function courseBjFun() {
  56. return 'static/images/course/course-cjdc-bj.png'
  57. }
  58. function chunkArray(arr, chunkSize) {
  59. const result = [];
  60. for (let i = 0; i < arr.length; i += chunkSize) {
  61. result.push(arr.slice(i, i + chunkSize));
  62. }
  63. return result;
  64. }
  65. const data = reactive({
  66. jieId: null, // 节/单元ID
  67. activeId: null, // 当前单词
  68. wordList: [], // 单词列表
  69. arrayList: [], // 整合数据
  70. activeWord: null, // 单词详情数据
  71. collectFlag: 0, // 收藏状态
  72. subjectId: null, // 学科ID
  73. levelId: null, // 等级
  74. typeId: null, // 类型
  75. tipFlag: null, // 提示
  76. zhangId: null, // 章ID
  77. isLearnStatus: null, // 学习记录状态
  78. })
  79. onLoad(({
  80. jieId,
  81. wordId,
  82. subjectId,
  83. levelId,
  84. typeId,
  85. tipFlag,
  86. isLearnStatus,
  87. zhangId
  88. }) => {
  89. data.jieId = jieId;
  90. isLearnRecord.value = jieId;
  91. data.activeId = wordId;
  92. data.isLearnStatus = isLearnStatus;
  93. data.subjectId = subjectId;
  94. data.levelId = levelId;
  95. data.typeId = typeId;
  96. data.tipFlag = tipFlag;
  97. data.zhangId = zhangId;
  98. // 获取单词列表数据
  99. initWordInfo();
  100. // 清理过期文件
  101. clearAudioCache();
  102. })
  103. // 是否是最后一题
  104. const isLast = computed(() => {
  105. if (!data.wordList.length) {
  106. return false
  107. }
  108. return data.activeId == data.wordList[data.wordList.length - 1].id;
  109. })
  110. const isFirst = computed(() => {
  111. if (!data.wordList.length) {
  112. return false
  113. }
  114. return data.activeId == data.wordList[0].id;
  115. })
  116. const activeWords = computed(() => {
  117. if (!data.activeId) {
  118. return null
  119. }
  120. return data.arrayList.find(item => item.some(cit => cit.id == data.activeId))
  121. })
  122. function nextWord() {
  123. const index = data.wordList.findIndex(item => item.id == data.activeId);
  124. if (index < data.wordList.length - 1) {
  125. uni.redirectTo({
  126. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index + 1].id }`
  127. })
  128. }
  129. }
  130. function prevWord() {
  131. const index = data.wordList.findIndex(item => item.id == data.activeId);
  132. if (index > 0) {
  133. uni.redirectTo({
  134. url: `/pages/newEnglish/index?jieId=${data.jieId}&wordId=${data.wordList[index - 1].id }`
  135. })
  136. }
  137. }
  138. function handleBack() {
  139. // 返回单词列表
  140. if (userCode !== 'Visitor') {
  141. if (data.jieId == 0) {
  142. uni.redirectTo({
  143. url: `/pages/my/learnRecord?jieId=${data.jieId}&isLearnStatus=${data.isLearnStatus}`
  144. })
  145. } else {
  146. uni.redirectTo({
  147. url: `/pages/wordList/wordList?jieId=${data.jieId}`
  148. })
  149. }
  150. } else {
  151. const youkePageData = JSON.stringify({
  152. subjectId: data.subjectId,
  153. levelId: data.levelId,
  154. typeId: data.typeId,
  155. tipFlag: data.tipFlag,
  156. youkeZhangId: data.zhangId,
  157. jieId: data.jieId
  158. })
  159. uni.redirectTo({
  160. url: `/pages/wordList/wordList?youkePageData=${youkePageData}`
  161. })
  162. }
  163. }
  164. function handleComplete() {
  165. // 返回岛
  166. if (userCode !== 'Visitor') {
  167. uni.redirectTo({
  168. url: `/pages/study/index`
  169. })
  170. } else {
  171. uni.redirectTo({
  172. url: `/pages/study/index?levelId=${data.levelId}&typeId=${data.typeId}&subjectId=${data.subjectId}&tipFlag=${data.tipFlag}&youkeZhangId=${data.zhangId}&jieId=${data.jieId}`
  173. })
  174. }
  175. }
  176. function handleShouCang() {
  177. httpApi.getWordShouCang({
  178. wordId: data.activeId
  179. }).then(res => {
  180. if (res.data) {
  181. uni.showToast({
  182. title: '收藏成功'
  183. })
  184. data.collectFlag = 1;
  185. } else {
  186. uni.showToast({
  187. title: '收藏取消'
  188. })
  189. data.collectFlag = 0;
  190. }
  191. })
  192. }
  193. function initWordInfo() {
  194. if (userCode !== 'Visitor') {
  195. httpApi.getWordInfo({
  196. jieId: data.jieId,
  197. wordId: data.activeId
  198. }).then(res => {
  199. data.activeWord = res.data;
  200. data.wordList = res.data.danciList; // 单词组
  201. data.collectFlag = res.data.collectFlag; // 收藏状态
  202. if (data.wordList.length) {
  203. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  204. }
  205. // console.log('res.data',res.data)
  206. handleCacheAudio()
  207. })
  208. } else {
  209. httpApi.getCommonWordInfo({
  210. jieId: data.jieId,
  211. wordId: data.activeId
  212. }).then(res => {
  213. data.activeWord = res.data;
  214. data.wordList = res.data.danciList; // 单词组
  215. data.collectFlag = res.data.collectFlag; // 收藏状态
  216. if (data.wordList.length) {
  217. data.arrayList = chunkArray(data.wordList, 5); // 将1维单词数组转换为2维数组
  218. }
  219. handleCacheAudio()
  220. })
  221. }
  222. }
  223. function handleCacheAudio() {
  224. const arr = []
  225. // yinpin
  226. arr.push(data.activeWord.yinpin);
  227. // yinjie
  228. data.activeWord.yinjie.forEach(item => {
  229. arr.push(item.yinpin)
  230. })
  231. // pindu
  232. data.activeWord.pindu.forEach(item => {
  233. arr.push(item.yinpin)
  234. })
  235. // 缓存音频
  236. arr.map(item => cacheAudio(item));
  237. // console.log('arr',arr)
  238. }
  239. async function handlePlayAudio({
  240. url,
  241. code
  242. }) {
  243. // console.log('播放', url)
  244. const cachedPath = await cacheAudio(url);
  245. if (cachedPath.includes('.mp3')) {
  246. // console.log('地址:', cachedPath)
  247. AudioP.play(cachedPath, code);
  248. } else {
  249. uni.showToast({
  250. title: '音频加载失败',
  251. icon: 'none'
  252. });
  253. }
  254. }
  255. </script>
  256. <style>
  257. </style>