index.vue 7.8 KB

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