index.vue 8.4 KB

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