index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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" backgroundColor="#3c7dfd"
  23. 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('学习页面显示,使用缓存数据')
  100. this.currentTabIndex = 1
  101. const cacheData = cacheManager.get('xuexi-shuxue');
  102. // cacheData && this.shouldUseCache(options, cacheData)
  103. if (cacheData) {
  104. // 使用缓存数据
  105. console.log('使用缓存数据');
  106. this.updateFromCache();
  107. } else {
  108. // 没有缓存或参数不同,重新请求
  109. console.log('重新请求数据');
  110. this.hasCache = false;
  111. const cacheDataAuth = cacheManager.get('auth');
  112. this.initFromOptions(cacheDataAuth);
  113. }
  114. // // 如果从其他页面返回需要刷新数据
  115. // if (this.needRefresh) {
  116. // this.needRefresh = false;
  117. // this.updateFromCache();
  118. // }
  119. // // 如果是第一次进入,已经有缓存数据了,直接使用缓存
  120. // if (this.hasCache) {
  121. // this.updateFromCache();
  122. // }
  123. },
  124. onHide() {
  125. console.log('学习页面隐藏,保存滚动位置')
  126. this.needRefresh = true; // 标记离开页面,返回时需要刷新
  127. },
  128. onLoad(options) {
  129. // debugger
  130. uni.hideTabBar()
  131. },
  132. methods: {
  133. // 从缓存初始化方法
  134. // initFromCache(cacheData) {
  135. // console.log('cacheData',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. console.log('options', options);
  147. this.chanpinId = options.chanpinId;
  148. this.danyuanId = options.danyuanId;
  149. this.banbenId = options.banbenId;
  150. this.dengjiId = options.dengjiId;
  151. this.loadDataFromApi();
  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. this.banbenInfo = {}
  174. this.curProcess = ''
  175. this.danyuanList = []
  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. if(!this.banbenId ||!this.danyuanId){
  226. toast("banbenId或者danyuanId 丢失")
  227. return false
  228. }
  229. let req = {
  230. "banbenId": this.banbenId,
  231. "danyuanId": this.danyuanId,
  232. "jieId": jieId
  233. }
  234. try {
  235. const res = await shuxueSave(req);
  236. console.log('res', res);
  237. if (res.code == 0 && res.data) {
  238. let curJieAndDanyuan = this.getJieAndDanyuan(this.banbenInfo, jieId);
  239. console.log('curJieAndDanyuan', curJieAndDanyuan);
  240. if (!curJieAndDanyuan) {
  241. toast("未找到课程信息");
  242. return false;
  243. }
  244. // 更新缓存
  245. const cacheData = cacheManager.get('xuexi-shuxue') || {};
  246. cacheData.curDanyuanName = curJieAndDanyuan.danyuan.danyuanName;
  247. cacheData.curKechengName = curJieAndDanyuan.danyuan.danyuanIntro;
  248. cacheData.curJieId = jieId;
  249. cacheData.type = curJieAndDanyuan.jie.type;
  250. cacheManager.set('xuexi-shuxue', cacheData);
  251. // 跳转到学习页面
  252. if (type == 1) {
  253. uni.navigateTo({
  254. url: `/pages/xinshuxue/lookShipin?jieId=${jieId}`
  255. })
  256. } else {
  257. uni.navigateTo({
  258. url: `/pages/xinshuxue/unitTest?jieId=${jieId}`
  259. })
  260. }
  261. } else {
  262. toast("保存位置出错");
  263. return false;
  264. }
  265. } catch (error) {
  266. toast("保存失败");
  267. return false;
  268. }
  269. },
  270. handlePlay(da, code) {
  271. console.log('da', da);
  272. let jieId = null;
  273. if (code == 'jixu') {
  274. if (!da.curJieId) {
  275. toast("无课程ID");
  276. return;
  277. }
  278. jieId = da.curJieId;
  279. } else {
  280. jieId = da.jieId;
  281. }
  282. this.saveAndNavigate(jieId, da.type);
  283. },
  284. handleClickDanyuan() {
  285. if(!this.danyuanId){
  286. toast("this.danyuanId丢失")
  287. return false
  288. }
  289. this.isShow = true;
  290. setTimeout(() => {
  291. this.$refs.dyRef.handleShow(this.danyuanId)
  292. }, 100)
  293. },
  294. handleBack() {
  295. uni.navigateTo({
  296. url: `/pages/chanpinXuanze/banben?dengjiId=` + this.dengjiId
  297. })
  298. },
  299. }
  300. }
  301. </script>