index.tsx 15 KB

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