index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view class="ezy-xuexi-page">
  3. <view class="icon-title-navBar-box">
  4. <text class="nav-bar-title">学习</text>
  5. </view>
  6. <view class="ezy-page-body xuexi-page-body">
  7. <view class="xxjl-card-box">
  8. <!-- 显示内容 -->
  9. <view class="card-body-box">
  10. <img :src="banbenInfo.cover" />
  11. <view class="body-right">
  12. <view class="right-name">{{ banbenInfo.chanpinName }}</view>
  13. <view>等级:{{banbenInfo.dengjiName}}</view>
  14. <view>版本:{{banbenInfo.name}}</view>
  15. <view>单元:{{banbenInfo.curDanyuanName}}</view>
  16. <view>课程:{{banbenInfo.curKechengName}}</view>
  17. </view>
  18. </view>
  19. <view class="card-progress-box">
  20. <view class="xx-progress-box">
  21. <view>学习进度</view>
  22. <progress :percent="curProcess" class="xx-progress" stroke-width="20"
  23. backgroundColor="#3c7dfd" activeColor="#ffd11c" />
  24. </view>
  25. <view class="jxxx-btn" @click="handlePlay(banbenInfo,'jixu')"></view>
  26. </view>
  27. </view>
  28. <view class="xx-scroll-body">
  29. <view class="xx-item-list">
  30. <view class="xx-item-title">— 以下为当前等级课程目录 —</view>
  31. <view v-for="danyuanItem in danyuanList" :key="danyuanItem.danyuanId">
  32. <view class="item-dy-box" @click="handleClickDanyuan">
  33. <view class="dy-left-box">L{{danyuanItem.danyuanId}}</view>
  34. <view class="dy-right-box">
  35. <view class="dy-name">{{danyuanItem.danyuanName}}</view>
  36. <view>{{danyuanItem.danyuanIntro}}</view>
  37. </view>
  38. </view>
  39. <view class="xx-item-box" v-for="jieItem in danyuanItem.jieList" :key="jieItem.jieId"
  40. @click="handlePlay(jieItem,'play')">
  41. <view class="xx-item-status"
  42. :class="jieItem.wanchengFlag == 1 ? 'completed-status' : 'uncompleted-status'"> </view>
  43. <img :src="jieItem.cover" />
  44. <view class="xx-text-box">
  45. <view>{{ jieItem.jieName }}</view>
  46. <view>{{ jieItem.jieIntro }}</view>
  47. </view>
  48. <view class="xx-item-btn"></view>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. <danyuanInfoVue ref="dyRef" v-if="isShow" @close="isShow= false"></danyuanInfoVue>
  55. <custom-tab-bar :show="true" :current-index="currentTabIndex" />
  56. <!-- 回到顶部 -->
  57. <view class="go-top-btn" v-if="false"></view>
  58. </view>
  59. </template>
  60. <script>
  61. import CustomTabBar from '@/components/custom-tabbar/index.vue';
  62. import cacheManager from "@/utils/cacheManager.js";
  63. import {
  64. shuxueChanpinBanbenInfo,
  65. shuxueSave
  66. } from "@/api/chanpinneirong.js"
  67. import {
  68. onLoad,
  69. onShow
  70. } from "@dcloudio/uni-app"
  71. import danyuanInfoVue from '@/pages/xinshuxue/components/danyuanInfo.vue';
  72. import {
  73. toast
  74. } from '../../utils/common';
  75. export default {
  76. data() {
  77. return {
  78. canExitApp: false,
  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('this.needRefresh',this.needRefresh)
  100. console.log('this.hasCache',this.hasCache)
  101. console.log('学习页面显示,使用缓存数据')
  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. }
  133. },
  134. methods: {
  135. // 从缓存初始化方法
  136. initFromCache(cacheData) {
  137. console.log('cacheData',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. console.log('options',options);
  149. this.chanpinId = options.chanpinId;
  150. this.danyuanId = options.danyuanId;
  151. this.banbenId = options.banbenId;
  152. this.dengjiId = options.dengjiId;
  153. this.loadDataFromApi();
  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>