index.vue 8.3 KB

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