index.vue 7.2 KB

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