index.vue 8.5 KB

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