index copy.vue 9.2 KB

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