index.tsx 10 KB

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