index.vue 9.1 KB

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