index.tsx 9.6 KB

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