index.tsx 14 KB

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