index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. <custom-tab-bar :show="true" :current-index="currentTabIndex" />
  69. </view>
  70. </template>
  71. <script>
  72. import CustomTabBar from '@/components/custom-tabbar/index.vue';
  73. import cacheManager from "@/utils/cacheManager.js";
  74. import {
  75. shuxueChanpinBanbenInfo,
  76. shuxueSave
  77. } from "@/api/chanpinneirong.js"
  78. import {
  79. onLoad,
  80. onShow
  81. } from "@dcloudio/uni-app"
  82. import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
  83. import {
  84. toast
  85. } from '../../utils/common';
  86. export default {
  87. data() {
  88. return {
  89. danyuanList: [],
  90. currentUnitName: "",
  91. isShow: true,
  92. banbenInfo: {},
  93. banbenId: '',
  94. danyuanId: '',
  95. chanpinId: '',
  96. dengjiId: '',
  97. curProcess: '',
  98. currentTabIndex: 1,
  99. cacheManagerLocal: null,
  100. hasCache: false, // 是否有缓存的标志
  101. needRefresh: false // 是否需要刷新数据的标志
  102. }
  103. },
  104. components: {
  105. CustomTabBar,
  106. danyuanInfoVue
  107. },
  108. onShow() {
  109. console.log('学习页面显示,使用缓存数据')
  110. console.log('this.needRefresh',this.needRefresh)
  111. console.log('this.hasCache',this.hasCache)
  112. this.currentTabIndex = 1
  113. // 如果从其他页面返回需要刷新数据
  114. if (this.needRefresh) {
  115. this.needRefresh = false;
  116. this.updateFromCache();
  117. }
  118. // 如果是第一次进入,已经有缓存数据了,直接使用缓存
  119. if (this.hasCache) {
  120. this.updateFromCache();
  121. }
  122. },
  123. onHide() {
  124. console.log('学习页面隐藏,保存滚动位置')
  125. this.needRefresh = true; // 标记离开页面,返回时需要刷新
  126. },
  127. onLoad(options) {
  128. const cacheData = cacheManager.get('xuexi-shuxue');
  129. // cacheData && this.shouldUseCache(options, cacheData)
  130. if (cacheData) {
  131. // 使用缓存数据
  132. console.log('使用缓存数据');
  133. this.hasCache = true;
  134. this.initFromCache(cacheData);
  135. } else {
  136. // 没有缓存或参数不同,重新请求
  137. console.log('重新请求数据');
  138. this.hasCache = false;
  139. const cacheDataAuth = cacheManager.get('auth');
  140. this.initFromOptions(cacheDataAuth);
  141. this.loadDataFromApi();
  142. }
  143. },
  144. methods: {
  145. // 从缓存初始化方法
  146. initFromCache(cacheData) {
  147. this.banbenInfo = cacheData;
  148. this.curProcess = cacheData.curProcess;
  149. this.danyuanList = cacheData.danyuanList || [];
  150. this.banbenId = cacheData.banbenId || '';
  151. this.chanpinId = cacheData.chanpinId || '';
  152. this.danyuanId = cacheData.danyuanId || '';
  153. this.dengjiId = cacheData.dengjiId || '';
  154. },
  155. // 从参数初始化方法
  156. initFromOptions(options) {
  157. this.chanpinId = options.chanpinId;
  158. this.danyuanId = options.danyuanId;
  159. this.banbenId = options.banbenId;
  160. this.dengjiId = options.dengjiId;
  161. },
  162. // 从缓存更新数据方法
  163. updateFromCache() {
  164. const cacheData = cacheManager.get('xuexi-shuxue');
  165. console.log('cacheData 从缓存更新数据方法',cacheData);
  166. if (cacheData) {
  167. this.banbenInfo = {
  168. ...cacheData
  169. };
  170. this.curProcess = cacheData.curProcess;
  171. this.danyuanList = [...(cacheData.danyuanList || [])];
  172. }
  173. },
  174. shouldUseCache(options, cacheData) {
  175. if (options.banbenId && cacheData.banbenId === options.banbenId) {
  176. return true;
  177. }
  178. return false;
  179. },
  180. // 改成了
  181. loadDataFromApi() {
  182. const req = {
  183. banbenId: this.banbenId
  184. }
  185. shuxueChanpinBanbenInfo(req).then(res => {
  186. if (res.code === 0) {
  187. this.banbenInfo = res.data;
  188. this.curProcess = res.data.curProcess;
  189. this.danyuanList = res.data.danyuanList || [];
  190. // 保存到缓存(新增了参数保存)
  191. const cacheData = {
  192. ...res.data,
  193. banbenId: this.banbenId,
  194. chanpinId: this.chanpinId,
  195. danyuanId: this.danyuanId,
  196. dengjiId: this.dengjiId
  197. };
  198. cacheManager.set('xuexi-shuxue', cacheData);
  199. this.hasCache = true;
  200. // 更新全局auth信息
  201. cacheManager.updateObject('auth', {
  202. chanpinId: this.chanpinId,
  203. banbenId: this.banbenId,
  204. danyuanId: this.danyuanId,
  205. dengjiId: this.dengjiId
  206. });
  207. }
  208. }).catch(res => {
  209. cacheManager.remove("xuexi-shuxue");
  210. this.hasCache = false;
  211. toast("加载失败,请重试");
  212. });
  213. },
  214. getJieAndDanyuan(data, jieId) {
  215. // 循环单元
  216. for (let danyuan of data.danyuanList) {
  217. // 循环节
  218. for (let jie of danyuan.jieList) {
  219. if (jie.jieId == jieId) {
  220. return {
  221. danyuan: danyuan,
  222. jie: jie
  223. }
  224. }
  225. }
  226. }
  227. return null;
  228. },
  229. // 统一保存和跳转方法
  230. async saveAndNavigate(jieId, type) {
  231. let req = {
  232. "banbenId": this.banbenId,
  233. "danyuanId": this.danyuanId,
  234. "jieId": jieId
  235. }
  236. try {
  237. const res = await shuxueSave(req);
  238. console.log('res', res);
  239. if (res.code == 0 && res.data) {
  240. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  241. console.log('curJieAndDanyuan', curJieAndDanyuan);
  242. if (!curJieAndDanyuan) {
  243. toast("未找到课程信息");
  244. return false;
  245. }
  246. // 更新缓存
  247. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  248. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  249. cacheData.curKechengName = curJieAndDanyuan.danyuan.danyuanIntro;
  250. cacheData.curJieId = jieId;
  251. cacheData.type = curJieAndDanyuan.jie.type;
  252. cacheManager.set('xuexi-shuxue', cacheData);
  253. // 跳转到学习页面
  254. if (type == 1) {
  255. uni.navigateTo({
  256. url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
  257. })
  258. } else {
  259. uni.navigateTo({
  260. url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
  261. })
  262. }
  263. } else {
  264. toast("保存位置出错");
  265. return false;
  266. }
  267. } catch (error) {
  268. toast("保存失败");
  269. return false;
  270. }
  271. },
  272. handlePlay(da, code) {
  273. console.log('da', da);
  274. let jieId = null;
  275. if (code == 'jixu') {
  276. if (!da.curJieId) {
  277. toast("无课程ID");
  278. return;
  279. }
  280. jieId = da.curJieId;
  281. } else {
  282. jieId = da.jieId;
  283. }
  284. this.saveAndNavigate(jieId, da.type);
  285. },
  286. handleClickDanyuan() {
  287. this.isShow = true;
  288. setTimeout(() => {
  289. this.$refs.dyRef.handleShow(this.danyuanId)
  290. }, 100)
  291. },
  292. handleBack() {
  293. uni.navigateTo({
  294. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  295. })
  296. },
  297. }
  298. }
  299. </script>