index.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import request from '@/helpers/request'
  2. import { state } from '@/state'
  3. import { Button, Empty, Grid, GridItem, Icon, showConfirmDialog, showToast } from 'vant'
  4. import { defineComponent, onMounted, reactive, onUnmounted } from 'vue'
  5. import styles from './index.module.less'
  6. import { useRoute, useRouter } from 'vue-router'
  7. import {
  8. listenerMessage,
  9. postMessage,
  10. promisefiyPostMessage,
  11. removeListenerMessage
  12. } from '@/helpers/native-message'
  13. import iconLook from './image/look.svg'
  14. import iconCourse from './image/icon-course.png'
  15. import { browser } from '@/helpers/utils'
  16. export default defineComponent({
  17. name: 'lessonCourseware',
  18. setup() {
  19. const route = useRoute()
  20. const router = useRouter()
  21. const browserInfo = browser()
  22. // const catchList = store
  23. const data = reactive({
  24. loading: true,
  25. list: [] as any
  26. })
  27. const getList = async () => {
  28. data.loading = true
  29. if (route.query.courseScheduleId) {
  30. try {
  31. const res: any = await request.post(
  32. state.platformApi + '/courseSchedule/getCoursewareDetail',
  33. {
  34. params: {
  35. courseScheduleId: route.query.courseScheduleId,
  36. coursewareId: route.query.id
  37. }
  38. }
  39. )
  40. if (Array.isArray(res?.data)) {
  41. data.list = res.data
  42. }
  43. } catch (error) {}
  44. } else {
  45. try {
  46. const res: any = await request.post(
  47. state.platformApi + '/courseSchedule/myCoursewareDetail/' + route.query.id
  48. )
  49. if (Array.isArray(res?.data)) {
  50. // data.list = res.data
  51. data.list = browserInfo.isApp ? await checkCoursewareCache(res.data) : res.data
  52. }
  53. } catch (error) {}
  54. }
  55. data.loading = false
  56. }
  57. onMounted(() => {
  58. getList()
  59. listenerMessage('downloadCoursewareToCache', getProgress)
  60. })
  61. onUnmounted(() => {
  62. removeListenerMessage('downloadCoursewareToCache', getProgress)
  63. })
  64. const handleClick = async (item: any) => {
  65. if (route.query.code === 'select') {
  66. console.log('选择课时')
  67. setCoursewareDetail(item)
  68. return
  69. }
  70. if (!item.hasCache) {
  71. if (browserInfo.isStudent || /(192|localhost)/.test(location.origin)) {
  72. try {
  73. await showConfirmDialog({
  74. message: '当前课程没有缓存是否缓存'
  75. })
  76. } catch (error) {
  77. gotoPlay(item)
  78. return
  79. }
  80. }
  81. item.downloadStatus = 1
  82. downCatch(item)
  83. return
  84. }
  85. gotoPlay(item)
  86. }
  87. // 去课件播放
  88. const gotoPlay = (item: any) => {
  89. router.push({
  90. path: '/coursewarePlay',
  91. query: {
  92. id: item.lessonCoursewareDetailId
  93. }
  94. })
  95. }
  96. // 检查数据的缓存状态
  97. const checkCoursewareCache = (list: []) => {
  98. return new Promise((resolve) => {
  99. postMessage(
  100. {
  101. api: 'checkCoursewareCache',
  102. content: {
  103. data: list
  104. }
  105. },
  106. (res) => {
  107. if (res?.content?.data) {
  108. resolve(res.content.data)
  109. return
  110. }
  111. return []
  112. }
  113. )
  114. })
  115. }
  116. // 下载缓存
  117. const downCatch = async (item: any) => {
  118. if (browserInfo.isApp) {
  119. const res = await postMessage({
  120. api: 'downloadCoursewareToCache',
  121. content: {
  122. data: item
  123. }
  124. })
  125. return res
  126. }
  127. return true
  128. }
  129. // 下载缓存进度
  130. const getProgress = (res: any) => {
  131. console.log('🚀 ~ res', res)
  132. if (res?.content?.lessonCoursewareDetailId) {
  133. const { lessonCoursewareDetailId, downloadStatus, progress } = res.content
  134. const course = data.list.find(
  135. (n: any) => n.lessonCoursewareDetailId == lessonCoursewareDetailId
  136. )
  137. if (course) {
  138. if (downloadStatus == 2) {
  139. course.hasCache = 1
  140. course.progress = 100
  141. }
  142. course.progress = progress
  143. }
  144. }
  145. }
  146. // 绑定课时
  147. const setCoursewareDetail = async (item: any) => {
  148. try {
  149. const res: any = await request.post(
  150. state.platformApi + '/courseSchedule/setCoursewareDetail',
  151. {
  152. params: {
  153. courseScheduleId: route.query.courseScheduleId,
  154. coursewareDetailId: item.lessonCoursewareDetailId
  155. }
  156. }
  157. )
  158. if (res.code === 200) {
  159. showToast('保存成功')
  160. postMessage({ api: 'back' })
  161. }
  162. } catch (error) {}
  163. }
  164. return () => (
  165. <div style={{ paddingTop: '14px' }}>
  166. <Grid gutter={14} columnNum={3} class={styles.grid}>
  167. {data.list.map((item: any) => {
  168. return (
  169. <GridItem>
  170. <div class={styles.gridItem} onClick={() => handleClick(item)}>
  171. <img src={iconCourse} class={styles.cover} />
  172. <div class={styles.title}>
  173. <div>{item.coursewareDetailName}</div>
  174. {route.query.code !== 'select' && <div>已使用 {item.useNum} 次</div>}
  175. </div>
  176. {route.query.code !== 'select' ? (
  177. <>
  178. {item.hasCache ? (
  179. <div class={styles.num}>
  180. 查看
  181. <Icon name="play-circle-o" />
  182. </div>
  183. ) : (
  184. <>
  185. {item.downloadStatus === 1 ? (
  186. <div class={styles.num}>
  187. 下载中 {item.progress || 0}%
  188. </div>
  189. ) : (
  190. <div class={styles.num}>下载</div>
  191. )}
  192. </>
  193. )}
  194. </>
  195. ) : (
  196. <div class={styles.num}>选择</div>
  197. )}
  198. {route.query.code == 'select' && !item.unlock && (
  199. <div class={styles.look} onClick={(e: Event) => e.stopPropagation()}>
  200. <Icon name={iconLook} /> 未解锁
  201. </div>
  202. )}
  203. </div>
  204. </GridItem>
  205. )
  206. })}
  207. </Grid>
  208. {/* <Button onClick={() => {
  209. location.href = 'http://192.168.1.10:1000/#/coursewarePlay?id=1610595720511209474&kId=1610115960752414721'
  210. }}>胜强测试</Button> */}
  211. {!data.loading && !data.list.length && <Empty description="空空如也" />}
  212. </div>
  213. )
  214. }
  215. })