index.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="ezy-xuexi-page">
  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-page-body xuexi-page-body">
  8. <view class="change-dy-box">
  9. <!-- 显示内容 -->
  10. <view class="change-dy-content">
  11. <view>{{ banbenInfo.chanpinName }}</view>
  12. <view>
  13. <text>等级</text>
  14. <text>{{banbenInfo.dengjiName}}</text>
  15. </view>
  16. <view>
  17. <text>版本</text>
  18. <text>{{banbenInfo.name}}</text>
  19. </view>
  20. <view>
  21. <text>单元</text>
  22. <text>{{banbenInfo.curDanyuanName}}</text>
  23. </view>
  24. <view>
  25. <text>课程</text>
  26. <text>{{banbenInfo.curKechengName}}</text>
  27. </view>
  28. <view>
  29. <view class="xx-progress-box">
  30. <view>当前学习进度</view>
  31. <progress :percent="curProcess" class="xx-progress" stroke-width="10"
  32. backgroundColor="#3c7dfd" activeColor="#ffd11c" />
  33. </view>
  34. <view class="jxxx-btn" @click="handlePlay(banbenInfo,'jixu')">继续学习</view>
  35. </view>
  36. </view>
  37. </view>
  38. <view class="xx-scroll-body">
  39. <view class="xx-item-list">
  40. <view v-for="danyuanItem in danyuanList" :key="danyuanItem.danyuanId">
  41. <view @click="handleClickDanyuan">
  42. <view>L{{danyuanItem.danyuanId}}</view>
  43. <view>{{danyuanItem.danyuanName}}</view>
  44. <view>{{danyuanItem.danyuanIntro}}</view>
  45. </view>
  46. <view class="xx-item-box" v-for="jieItem in danyuanItem.jieList" :key="jieItem.jieId"
  47. @click="handlePlay(jieItem,'play')">
  48. <view class="xx-item-status"
  49. :class="jieItem.wanchengFlag == 1 ? 'completed-status' : 'uncompleted-status'"> </view>
  50. <img :src="jieItem.cover" />
  51. <view class="xx-text-box">
  52. <view>{{ jieItem.jieName }}</view>
  53. <view>{{ jieItem.jieIntro }}</view>
  54. </view>
  55. <view class="xx-item-btn"></view>
  56. </view>
  57. </view>
  58. </view>
  59. </view>
  60. </view>
  61. <danyuanInfoVue ref="dyRef" v-if="isShow" @close="isShow= false"></danyuanInfoVue>
  62. <CustomTabBar :currentTabNumber="1"></CustomTabBar>
  63. </view>
  64. </template>
  65. <script>
  66. import CustomTabBar from '@/components/custom-tabbar/custom-tabbar.vue';
  67. import cacheManager from "@/utils/cacheManager.js";
  68. import {
  69. shuxueChanpinBanbenInfo,
  70. shuxueSave
  71. } from "@/api/chanpinneirong.js"
  72. import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
  73. import {
  74. toast
  75. } from '../../utils/common';
  76. export default {
  77. data() {
  78. return {
  79. danyuanList: [],
  80. currentUnitName: "",
  81. isShow: true,
  82. banbenInfo: {},
  83. banbenId: '',
  84. danyuanId: '',
  85. chanpinId: '',
  86. dengjiId: '',
  87. curProcess: '',
  88. cacheManagerLocal: null,
  89. hasCache: false, // 是否有缓存的标志
  90. needRefresh: false // 是否需要刷新数据的标志
  91. }
  92. },
  93. components: {
  94. CustomTabBar,
  95. danyuanInfoVue
  96. },
  97. onShow() {
  98. console.log('学习页面显示,使用缓存数据')
  99. console.log('this.needRefresh',this.needRefresh)
  100. console.log('this.hasCache',this.hasCache)
  101. // 如果从其他页面返回需要刷新数据
  102. if (this.needRefresh) {
  103. this.needRefresh = false;
  104. this.updateFromCache();
  105. }
  106. // 如果是第一次进入,已经有缓存数据了,直接使用缓存
  107. if (this.hasCache) {
  108. this.updateFromCache();
  109. }
  110. },
  111. onHide() {
  112. console.log('学习页面隐藏,保存滚动位置')
  113. this.needRefresh = true; // 标记离开页面,返回时需要刷新
  114. },
  115. onLoad(options) {
  116. const cacheData = cacheManager.get('xuexi-shuxue');
  117. // cacheData && this.shouldUseCache(options, cacheData)
  118. if (cacheData) {
  119. // 使用缓存数据
  120. console.log('使用缓存数据');
  121. this.hasCache = true;
  122. this.initFromCache(cacheData);
  123. } else {
  124. // 没有缓存或参数不同,重新请求
  125. console.log('重新请求数据');
  126. this.hasCache = false;
  127. this.initFromOptions(options);
  128. this.loadDataFromApi();
  129. }
  130. },
  131. methods: {
  132. // 新增的从缓存初始化方法
  133. initFromCache(cacheData) {
  134. this.banbenInfo = cacheData;
  135. this.curProcess = cacheData.curProcess;
  136. this.danyuanList = cacheData.danyuanList || [];
  137. this.banbenId = cacheData.banbenId || '';
  138. this.chanpinId = cacheData.chanpinId || '';
  139. this.danyuanId = cacheData.danyuanId || '';
  140. this.dengjiId = cacheData.dengjiId || '';
  141. },
  142. // 新增的从参数初始化方法
  143. initFromOptions(options) {
  144. this.chanpinId = options.chanpinId;
  145. this.danyuanId = options.danyuanId;
  146. this.banbenId = options.banbenId;
  147. this.dengjiId = options.dengjiId;
  148. },
  149. // 新增的从缓存更新数据方法
  150. updateFromCache() {
  151. const cacheData = cacheManager.get('xuexi-shuxue');
  152. console.log('cacheData 新增的从缓存更新数据方法',cacheData);
  153. if (cacheData) {
  154. this.banbenInfo = {
  155. ...cacheData
  156. };
  157. this.curProcess = cacheData.curProcess;
  158. this.danyuanList = [...(cacheData.danyuanList || [])];
  159. }
  160. },
  161. shouldUseCache(options, cacheData) {
  162. if (options.banbenId && cacheData.banbenId === options.banbenId) {
  163. return true;
  164. }
  165. return false;
  166. },
  167. // 改成了
  168. loadDataFromApi() {
  169. const req = {
  170. banbenId: this.banbenId
  171. }
  172. shuxueChanpinBanbenInfo(req).then(res => {
  173. if (res.code === 0) {
  174. this.banbenInfo = res.data;
  175. this.curProcess = res.data.curProcess;
  176. this.danyuanList = res.data.danyuanList || [];
  177. // 保存到缓存(新增了参数保存)
  178. const cacheData = {
  179. ...res.data,
  180. banbenId: this.banbenId,
  181. chanpinId: this.chanpinId,
  182. danyuanId: this.danyuanId,
  183. dengjiId: this.dengjiId
  184. };
  185. cacheManager.set('xuexi-shuxue', cacheData);
  186. this.hasCache = true;
  187. // 更新全局auth信息
  188. cacheManager.updateObject('auth', {
  189. chanpinId: this.chanpinId,
  190. banbenId: this.banbenId,
  191. danyuanId: this.danyuanId,
  192. dengjiId: this.dengjiId
  193. });
  194. }
  195. }).catch(res => {
  196. cacheManager.remove("xuexi-shuxue");
  197. this.hasCache = false;
  198. toast("加载失败,请重试");
  199. });
  200. },
  201. getJieAndDanyuan(data, jieId) {
  202. // 循环单元
  203. for (let danyuan of data.danyuanList) {
  204. // 循环节
  205. for (let jie of danyuan.jieList) {
  206. if (jie.jieId == jieId) {
  207. return {
  208. danyuan: danyuan,
  209. jie: jie
  210. }
  211. }
  212. }
  213. }
  214. return null;
  215. },
  216. // 这个是新增的统一保存和跳转方法
  217. async saveAndNavigate(jieId, type) {
  218. let req = {
  219. "banbenId": this.banbenId,
  220. "danyuanId": this.danyuanId,
  221. "jieId": jieId
  222. }
  223. try {
  224. const res = await shuxueSave(req);
  225. console.log('res', res);
  226. if (res.code == 0 && res.data) {
  227. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  228. console.log('curJieAndDanyuan', curJieAndDanyuan);
  229. if (!curJieAndDanyuan) {
  230. toast("未找到课程信息");
  231. return false;
  232. }
  233. // 更新缓存
  234. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  235. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  236. cacheData.curKechengName = curJieAndDanyuan.danyuan.danyuanIntro;
  237. cacheData.curJieId = jieId;
  238. cacheData.type = curJieAndDanyuan.jie.type;
  239. cacheManager.set('xuexi-shuxue', cacheData);
  240. // 跳转到学习页面
  241. if (type == 1) {
  242. uni.navigateTo({
  243. url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
  244. })
  245. } else {
  246. uni.navigateTo({
  247. url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
  248. })
  249. }
  250. } else {
  251. toast("保存位置出错");
  252. return false;
  253. }
  254. } catch (error) {
  255. toast("保存失败");
  256. return false;
  257. }
  258. },
  259. handlePlay(da, code) {
  260. console.log('da', da);
  261. let jieId = null;
  262. if (code == 'jixu') {
  263. if (!da.curJieId) {
  264. toast("无课程ID");
  265. return;
  266. }
  267. jieId = da.curJieId;
  268. } else {
  269. jieId = da.jieId;
  270. }
  271. this.saveAndNavigate(jieId, da.type);
  272. },
  273. handleClickDanyuan() {
  274. this.isShow = true;
  275. setTimeout(() => {
  276. this.$refs.dyRef.handleShow(this.danyuanId)
  277. }, 100)
  278. },
  279. handleBack() {
  280. uni.navigateTo({
  281. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  282. })
  283. },
  284. }
  285. }
  286. </script>