index.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import request from '@/helpers/request'
  2. import { state } from '@/state'
  3. import {
  4. Button,
  5. Cell,
  6. CellGroup,
  7. Dialog,
  8. Icon,
  9. Popup,
  10. showConfirmDialog,
  11. showLoadingToast
  12. } from 'vant'
  13. import { defineComponent, onMounted, reactive, onUnmounted, TransitionGroup } from 'vue'
  14. import styles from './index.module.less'
  15. import { useRoute, useRouter } from 'vue-router'
  16. import {
  17. listenerMessage,
  18. postMessage,
  19. promisefiyPostMessage,
  20. removeListenerMessage
  21. } from '@/helpers/native-message'
  22. import iconLook from './image/look.svg'
  23. import iconCachePoint from './image/icon-cache-point.png'
  24. import iconCourse from './image/icon-course.png'
  25. import iconCourseLock from './image/icon-course-lock.png'
  26. import iconTip from './image/iconTip.png'
  27. import { browser } from '@/helpers/utils'
  28. import OEmpty from '@/components/o-empty'
  29. import { handleCheckVip } from '../hook/useFee'
  30. import iconList from './image/icon-list.png'
  31. import OSticky from '@/components/o-sticky'
  32. import OHeader from '@/components/o-header'
  33. import { useEventListener } from '@vant/use'
  34. import OLoading from '@/components/o-loading'
  35. export default defineComponent({
  36. name: 'courseList',
  37. setup() {
  38. const route = useRoute()
  39. const router = useRouter()
  40. const browserInfo = browser()
  41. // const catchList = store
  42. const data = reactive({
  43. titleOpacity: 0,
  44. catchStatus: false,
  45. catchItem: {} as any,
  46. loading: true,
  47. detail: {
  48. cover: '',
  49. name: '',
  50. des: ''
  51. },
  52. list: [] as any,
  53. isDownloading: false // 是否在下载资源
  54. })
  55. /** 获取课件详情 */
  56. const getDetail = async () => {
  57. const res: any = await request.get(
  58. `${state.platformApi}/lessonCourseware/detail/${route.query.id}`
  59. )
  60. if (res?.data) {
  61. data.detail.cover = res.data.coverImg
  62. data.detail.name = res.data.name
  63. data.detail.des = res.data.lessonTargetDesc
  64. }
  65. }
  66. const getList = async () => {
  67. data.loading = true
  68. if (route.query.courseScheduleId) {
  69. try {
  70. const res: any = await request.post(
  71. state.platformApi + '/courseSchedule/getCoursewareDetail',
  72. {
  73. params: {
  74. courseScheduleId: route.query.courseScheduleId,
  75. coursewareId: route.query.id
  76. }
  77. }
  78. )
  79. if (Array.isArray(res?.data)) {
  80. data.list = res.data
  81. }
  82. } catch (error) {}
  83. } else {
  84. try {
  85. const res: any = await request.post(
  86. state.platformApi + '/courseSchedule/myCoursewareDetail/' + route.query.id
  87. )
  88. if (Array.isArray(res?.data)) {
  89. res.data.forEach((item: any) => {
  90. const { knowledgePointList, ...res } = item
  91. const tempK = knowledgePointList || []
  92. tempK.forEach((child: any) => {
  93. child.materialList = [
  94. ...(child.materialList || []),
  95. ...getKnowledgeMaterials(child.children || [])
  96. ]
  97. child.children = null
  98. })
  99. })
  100. const _list = await checkCoursewareCache(res.data)
  101. data.list = browserInfo.isApp
  102. ? res.data.map((item: any) => {
  103. const _item = _list.find(
  104. (n: any) => n.lessonCoursewareDetailId == item.lessonCoursewareDetailId
  105. )
  106. const n = {
  107. ...item
  108. }
  109. if (_item) {
  110. n.hasCache = _item.hasCache
  111. }
  112. return n
  113. })
  114. : res.data
  115. }
  116. } catch (error) {}
  117. }
  118. data.loading = false
  119. }
  120. // 获取子节点数据
  121. const getKnowledgeMaterials = (list: any = []) => {
  122. const tempList: any = []
  123. list.forEach((item: any) => {
  124. if (item.materialList && item.materialList.length > 0) {
  125. tempList.push(...(item.materialList || []))
  126. }
  127. if (item.children && item.children.length > 0) {
  128. tempList.push(...getKnowledgeMaterials(item.children || []))
  129. }
  130. })
  131. return tempList
  132. }
  133. onMounted(() => {
  134. getDetail()
  135. getList()
  136. listenerMessage('downloadCoursewareToCache', getProgress)
  137. })
  138. onUnmounted(() => {
  139. removeListenerMessage('downloadCoursewareToCache', getProgress)
  140. })
  141. const handleClick = async (item: any) => {
  142. if (!item.knowledgePointList) {
  143. showConfirmDialog({
  144. message: '该课件暂无知识点'
  145. })
  146. return
  147. }
  148. if (route.query.code === 'select') {
  149. console.log('选择课时')
  150. setCoursewareDetail(item)
  151. return
  152. }
  153. if (!item.hasCache) {
  154. const hasFree = String(item.accessScope) === '0'
  155. if (!hasFree) {
  156. const hasVip = handleCheckVip()
  157. if (!hasVip) return
  158. }
  159. // 下载中不提示
  160. if (item.downloadStatus == 1) {
  161. // 取消下载
  162. postMessage({ api: 'cancelDownloadCourseware' })
  163. setTimeout(() => {
  164. postMessage({ api: 'cancelDownloadCourseware' })
  165. item.downloadStatus = 0
  166. data.isDownloading = false
  167. }, 1000)
  168. showLoadingToast({
  169. message: '取消中...',
  170. forbidClick: false,
  171. loadingType: 'spinner',
  172. duration: 1000
  173. })
  174. return
  175. }
  176. // 重新下载
  177. if (item.downloadStatus == 3) {
  178. downCatch(item)
  179. return
  180. }
  181. data.catchStatus = true
  182. data.catchItem = item
  183. // 下载中不提示
  184. // if (item.downloadStatus == 1) {
  185. // return
  186. // }
  187. // // 重新下载
  188. // if (item.downloadStatus == 3) {
  189. // downCatch(item)
  190. // return
  191. // }
  192. // data.catchStatus = true
  193. // data.catchItem = item
  194. // try {
  195. // await showConfirmDialog({
  196. // message: '当前课程没有缓存,是否缓存?',
  197. // })
  198. // } catch (error) {
  199. // gotoPlay(item)
  200. // return
  201. // }
  202. // downCatch(item)
  203. return
  204. }
  205. gotoPlay(item)
  206. }
  207. // 去课件播放
  208. const gotoPlay = (item: any) => {
  209. data.catchStatus = false
  210. postMessage({
  211. api: 'openWebView',
  212. content: {
  213. url: `${location.origin}${location.pathname}#/coursewarePlay?id=${item.lessonCoursewareDetailId}&source=my-course`,
  214. orientation: 0,
  215. isHideTitle: true,
  216. statusBarTextColor: false,
  217. isOpenLight: true,
  218. showLoadingAnim: true
  219. }
  220. })
  221. }
  222. // 检查数据的缓存状态
  223. const checkCoursewareCache = (list: []): Promise<any[]> => {
  224. if (!browser().isApp) {
  225. return Promise.resolve(list)
  226. }
  227. return new Promise((resolve) => {
  228. postMessage(
  229. {
  230. api: 'checkCoursewareCache',
  231. content: {
  232. data: list
  233. }
  234. },
  235. (res) => {
  236. if (res?.content?.data) {
  237. resolve(res.content.data)
  238. return
  239. }
  240. return []
  241. }
  242. )
  243. })
  244. }
  245. // 下载缓存
  246. const downCatch = async (item: any) => {
  247. if (browserInfo.isApp) {
  248. data.catchStatus = false
  249. data.isDownloading = true
  250. const res = await postMessage({
  251. api: 'downloadCoursewareToCache',
  252. content: {
  253. data: item
  254. }
  255. })
  256. return res
  257. }
  258. return true
  259. }
  260. // 下载缓存进度
  261. const getProgress = (res: any) => {
  262. // console.log('🚀 ~ res', res)
  263. if (!data.isDownloading) {
  264. return
  265. }
  266. if (res?.content?.lessonCoursewareDetailId) {
  267. const { lessonCoursewareDetailId, downloadStatus, progress } = res.content
  268. const course = data.list.find(
  269. (n: any) => n.lessonCoursewareDetailId == lessonCoursewareDetailId
  270. )
  271. if (course) {
  272. course.downloadStatus = downloadStatus
  273. course.progress = progress
  274. if (downloadStatus == 2) {
  275. course.hasCache = 1
  276. course.progress = 100
  277. // 下载完成
  278. data.isDownloading = false
  279. }
  280. }
  281. }
  282. }
  283. // 绑定课时
  284. const setCoursewareDetail = async (item: any) => {
  285. try {
  286. const res: any = await request.post(
  287. state.platformApi + '/courseSchedule/setCoursewareDetail',
  288. {
  289. params: {
  290. courseScheduleId: route.query.courseScheduleId,
  291. coursewareDetailId: item.lessonCoursewareDetailId
  292. }
  293. }
  294. )
  295. if (res.code === 200) {
  296. postMessage({ api: 'back' })
  297. }
  298. } catch (error) {}
  299. }
  300. useEventListener('scroll', (e: Event) => {
  301. const height = window.scrollY || window.pageYOffset || document.documentElement.scrollTop
  302. data.titleOpacity = height > 100 ? 1 : height / 100
  303. })
  304. return () => (
  305. <div class={styles.courseList}>
  306. <OHeader
  307. border={false}
  308. background={`rgba(255,255,255, ${data.titleOpacity})`}
  309. color="rgba(124, 61, 18, 1)"
  310. title="教材详情"
  311. />
  312. <div class={styles.periodContent}>
  313. <div class={styles.cover}>
  314. <img
  315. src={data.detail.cover}
  316. onLoad={(e: Event) => {
  317. if (e.target) {
  318. ;(e.target as any).style.opacity = 1
  319. }
  320. }}
  321. />
  322. </div>
  323. <div>
  324. <div class={styles.contentTitle}>{data.detail.name}</div>
  325. <div class={styles.contentLabel}>教学目标:{data.detail.des}</div>
  326. </div>
  327. </div>
  328. <TransitionGroup name="van-fade">
  329. {!data.loading && (
  330. <>
  331. <div key="periodTitle" class={styles.periodTitle}>
  332. <img class={styles.pIcon} src={iconList} />
  333. <div class={styles.pTitle}>课程列表</div>
  334. <div class={styles.pNum}>共{data.list.length}课</div>
  335. </div>
  336. <div key="list" class={styles.periodList}>
  337. <CellGroup inset>
  338. {data.list.map((item: any) => {
  339. const isLock =
  340. item.lockFlag ||
  341. ((route.query.code == 'select' || state.platformType == 'STUDENT') &&
  342. !item.unlock)
  343. const isSelect = route.query.code === 'select'
  344. return (
  345. <Cell
  346. border
  347. center
  348. title={item.coursewareDetailName}
  349. titleClass={styles.titleName}
  350. label={!browserInfo.isStudent ? `已使用${item.useNum || 0}次` : ''}
  351. onClick={() => !isLock && handleClick(item)}
  352. >
  353. {{
  354. icon: () => (
  355. <div class={styles.periodItem}>
  356. <div class={styles.periodItemModel}>
  357. <img src={isLock ? iconCourseLock : iconCourse} />
  358. {!isLock && String(item.accessScope) === '0' && (
  359. <img class={styles.periodTip} src={iconTip} />
  360. )}
  361. {item.hasCache ? (
  362. <img class={styles.iconCachePoint} src={iconCachePoint} />
  363. ) : (
  364. ''
  365. )}
  366. {item.downloadStatus === 1 && (
  367. <div class={styles.downloading}>{`${item.progress || 0}%`}</div>
  368. )}
  369. </div>
  370. </div>
  371. ),
  372. value: () => (
  373. <>
  374. {isSelect ? (
  375. <Button
  376. disabled={isLock}
  377. class={[styles.baseBtn, isLock ? styles.disable : styles.look]}
  378. >
  379. 选择
  380. </Button>
  381. ) : item.knowledgePointList ? (
  382. <>
  383. {item.hasCache ? (
  384. <Button
  385. class={[
  386. styles.baseBtn,
  387. isLock ? styles.disable : styles.look
  388. ]}
  389. >
  390. 查看
  391. </Button>
  392. ) : (
  393. <Button
  394. class={[
  395. styles.baseBtn,
  396. isLock ? styles.disable : styles.down,
  397. item.downloadStatus ? styles.downing : ''
  398. ]}
  399. >
  400. {item.downloadStatus === 1 ? `取消下载` : '查看'}
  401. </Button>
  402. )}
  403. </>
  404. ) : (
  405. ''
  406. )}
  407. </>
  408. )
  409. }}
  410. </Cell>
  411. )
  412. })}
  413. </CellGroup>
  414. </div>
  415. </>
  416. )}
  417. </TransitionGroup>
  418. {data.loading && <OLoading />}
  419. {!data.loading && !data.list.length && <OEmpty tips="暂无内容" />}
  420. <Popup v-model:show={data.catchStatus} round class={styles.courseDialog}>
  421. <i class={styles.iconClose} onClick={() => (data.catchStatus = false)}></i>
  422. <div class={styles.title}>下载提醒</div>
  423. <div class={styles.content}>
  424. 您尚未下载课件内容,为了更加流畅的学习体验,推荐您下载后观看课件。
  425. </div>
  426. <div class={styles.popupBtnGroup}>
  427. <Button round onClick={() => gotoPlay(data.catchItem)}>
  428. 直接观看
  429. </Button>
  430. <Button round type="primary" onClick={() => downCatch(data.catchItem)}>
  431. 下载课件
  432. </Button>
  433. </div>
  434. </Popup>
  435. </div>
  436. )
  437. }
  438. })