index.vue 8.9 KB

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