useKechengCache.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import {
  2. useUserCache
  3. } from "@/utils/userCache.js"
  4. import * as kcApi from "@/api/kecheng.js"
  5. export function useKechengTools() {
  6. const {
  7. saveCache,
  8. getCache,
  9. removeCache
  10. } = useUserCache();
  11. function getCurKjIndex(kjId, operId) {
  12. const arrKecheng = getKechengDataFromHistory(operId);
  13. for (let zi = 0; zi < arrKecheng.length; zi++) {
  14. for (let ji = 0; ji < arrKecheng[zi].jieList.length; ji++) {
  15. if (arrKecheng[zi].jieList[ji].kejianList.some(kj => kj.kjId == kjId)) {
  16. return {
  17. zhangIndex: zi,
  18. jieIndex: ji
  19. }
  20. }
  21. }
  22. }
  23. }
  24. function mergeProgress(backData, historyData) {
  25. const curData = JSON.parse(JSON.stringify(historyData));
  26. backData.forEach((item, zhangIndex) => {
  27. item.jieList.forEach((jieItem, jieIndex) => {
  28. jieItem.kejianList.forEach((kjItem, kjIndex) => {
  29. const backCurJie = backData[zhangIndex].jieList[jieIndex].kejianList[
  30. kjIndex];
  31. // 大于等于100 的进度以后台为准
  32. if (kjItem.maxProcess >= 100) {
  33. curData[zhangIndex].jieList[jieIndex].kejianList[kjIndex] = backCurJie;
  34. } else if (curData[zhangIndex].jieList[jieIndex].kejianList[kjIndex]
  35. .maxProcess == 100) {
  36. // 前台缓存进度为100 使用后台进度
  37. // alert('前台缓存进度为100 使用后台进度')
  38. curData[zhangIndex].jieList[jieIndex].kejianList[kjIndex] = backCurJie;
  39. }
  40. curData[zhangIndex].jieList[jieIndex].kejianList[kjIndex].lockFlag =
  41. backCurJie.lockFlag;
  42. })
  43. })
  44. })
  45. return curData;
  46. }
  47. function saveKechengData(operId, data) {
  48. saveCache('SectionInfo' + operId, 'arrKecheng', data)
  49. }
  50. function getKechengDataFromHistory(operId) {
  51. return getCache('SectionInfo' + operId, 'arrKecheng')
  52. }
  53. function saveKechengSectionPage(operId, data) {
  54. saveCache('SectionInfo' + operId, 'sectionPage', data)
  55. }
  56. function getKechengSectionPageFromHistory(operId) {
  57. return getCache('SectionInfo' + operId, 'sectionPage')
  58. }
  59. function initCourseProgressAll(operId) {
  60. let arrKecheng = getCache('SectionInfo' + operId, 'arrKecheng');
  61. // 判断是否有课程信息
  62. if (!arrKecheng || arrKecheng.length === 0) {
  63. throw new Error('initCourseProgress业务逻辑错误: arrKecheng 异常');
  64. }
  65. for (const chapter of arrKecheng) {
  66. for (const section of chapter.jieList) {
  67. let kejianArr = section.kejianList;
  68. if (kejianArr && kejianArr.length > 0) {
  69. for (const kejian of kejianArr) {
  70. if (!kejian.maxProcess) {
  71. kejian.curProcess = 0;
  72. kejian.maxProcess = 0;
  73. kejian.hasFinished = false;
  74. } else if (kejian.maxProcess < 100) {
  75. kejian.hasFinished = false;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. saveCache('SectionInfo' + operId, 'arrKecheng', arrKecheng);
  82. return arrKecheng;
  83. }
  84. function saveCourseProgress(cur, total, operId, code, type) {
  85. let sectionPage = getCache('SectionInfo' + operId, 'sectionPage');
  86. let arrKecheng = getCache('SectionInfo' + operId, 'arrKecheng');
  87. let section = arrKecheng[sectionPage.iChapter].jieList[sectionPage.iSection];
  88. for (const kejian of section.kejianList) {
  89. if (kejian.kjId === sectionPage.kjId) {
  90. // video或audio的end为100,其他情况最大99
  91. if (code === 'end') {
  92. kejian['curProcess'] = 100;
  93. kejian['maxProcess'] = 100;
  94. } else if (type === 'other') {
  95. // 除了音视频以外的课件
  96. kejian['curProcess'] = 100;
  97. kejian['maxProcess'] = 100;
  98. } else {
  99. // 0 ~ 99
  100. kejian['curProcess'] = parseInt(cur / total * 100) >= 100 ?
  101. 99 :
  102. parseInt(cur / total * 100);
  103. kejian['maxProcess'] = kejian['curProcess'] > kejian['maxProcess'] ?
  104. kejian['curProcess'] :
  105. kejian['maxProcess'];
  106. }
  107. break;
  108. }
  109. }
  110. saveCache('SectionInfo' + operId, 'arrKecheng', arrKecheng);
  111. }
  112. function getCourseProgress(total) {
  113. let sectionPage = getCache('SectionInfo', 'sectionPage');
  114. let arrKecheng = getCache('SectionInfo', 'arrKecheng');
  115. let section = getSection(arrKecheng, sectionPage.iChapter, sectionPage.iSection);
  116. for (const kejian of section.kejianList) {
  117. if (kejian.kjId === sectionPage.kjId) {
  118. return {
  119. cur: total * parseFloat(kejian['curProcess']) / 100,
  120. max: total * parseFloat(kejian['maxProcess']) / 100,
  121. };
  122. }
  123. }
  124. }
  125. function updateSectionProgress(operId, code, type, callback) {
  126. let sectionPage = getCache('SectionInfo' + operId, 'sectionPage');
  127. let arrKecheng = getCache('SectionInfo' + operId, 'arrKecheng');
  128. if (sectionPage && sectionPage.finish) {
  129. console.log('已经结束,不再保存进度')
  130. return;
  131. }
  132. let kejianList = arrKecheng[sectionPage.iChapter].jieList[sectionPage.iSection].kejianList;
  133. let kejianIndex = kejianList.findIndex(item => item.kjId === sectionPage.kjId);
  134. const optX = {
  135. zhangIndex: sectionPage.iChapter,
  136. jieIndex: sectionPage.iSection,
  137. kejianIndex: kejianIndex,
  138. kjId: sectionPage.kjId,
  139. operId: sectionPage.operId,
  140. process: kejianList[kejianIndex].maxProcess,
  141. customLoadingSwitch: false,
  142. };
  143. saveCache('SectionInfo' + operId, 'arrKecheng', arrKecheng);
  144. const saveOpt = {
  145. kejianList: kejianList,
  146. kejianIndex: kejianIndex,
  147. arrKecheng: arrKecheng,
  148. optX: optX,
  149. };
  150. // 音视频end
  151. if (code === 'end') {
  152. kcApi.getClientKechengSave(optX).then(res => {
  153. // 接口正常执行
  154. if (res.code === 0) {
  155. // 音视频end事件
  156. kejianList[kejianIndex].curProcess = 100;
  157. kejianList[kejianIndex].maxProcess = 100;
  158. arrKecheng[optX.zhangIndex].jieList[optX.jieIndex].kejianList[optX.kejianIndex]
  159. .hasFinished = true;
  160. saveCache('SectionInfo' + operId, 'arrKecheng', arrKecheng);
  161. if (res.data.finish === true) {
  162. // 整个课程学完 data为true
  163. uni.showToast({
  164. title: "课程已学完!"
  165. })
  166. sectionPage.finish = true;
  167. saveCache('SectionInfo' + operId, 'sectionPage', sectionPage);
  168. }
  169. } else {
  170. saveInterfaceAbnormal(saveOpt, 99);
  171. callback(99)
  172. }
  173. }).catch(err => {
  174. saveInterfaceAbnormal(saveOpt, 99);
  175. callback(99)
  176. });
  177. } else {
  178. // 其他情况(音视频一秒一保存)
  179. kcApi.getClientKechengSave(optX).then(res => {
  180. if (res.code === 0) {
  181. if (res.data.finish === true) {
  182. uni.showToast({
  183. title: "课程已学完!"
  184. })
  185. sectionPage.finish = true;
  186. saveCache('SectionInfo' + operId, 'sectionPage', sectionPage);
  187. }
  188. }
  189. });
  190. }
  191. }
  192. function saveInterfaceAbnormal(saveOpt, jindu) {
  193. const {
  194. kejianList,
  195. kejianIndex,
  196. arrKecheng,
  197. optX,
  198. operId
  199. } = saveOpt;
  200. kejianList[kejianIndex].curProcess = jindu;
  201. kejianList[kejianIndex].maxProcess = jindu;
  202. arrKecheng[optX.zhangIndex].jieList[optX.jieIndex].kejianList[optX.kejianIndex].hasFinished = false;
  203. saveCache('SectionInfo' + operId, 'arrKecheng', arrKecheng);
  204. uni.redirectTo({
  205. url:"/pages/client/Kecheng/list"
  206. })
  207. }
  208. return {
  209. getCurKjIndex,
  210. saveKechengData,
  211. getKechengDataFromHistory,
  212. saveKechengSectionPage,
  213. getKechengSectionPageFromHistory,
  214. mergeProgress,
  215. initCourseProgressAll,
  216. saveCourseProgress,
  217. getCourseProgress,
  218. updateSectionProgress,
  219. saveInterfaceAbnormal
  220. }
  221. }