index.vue 8.5 KB

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