index.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. import request from '@/helpers/request'
  2. import { state } from '@/state'
  3. import { Button, Cell, CellGroup, Popup, Dialog, Toast, Circle } from 'vant'
  4. import { Vue3Lottie } from 'vue3-lottie'
  5. import AstronautJSON from '../music-detail/animate/refresh_anim.json'
  6. import {
  7. defineComponent,
  8. onMounted,
  9. reactive,
  10. onUnmounted,
  11. TransitionGroup,
  12. ref
  13. } from 'vue'
  14. import styles from './index.module.less'
  15. import { useRoute, useRouter } from 'vue-router'
  16. import { state as baseState } from '@/state'
  17. import { orderStatus } from '@/views/order-detail/orderStatus'
  18. import {
  19. listenerMessage,
  20. postMessage,
  21. removeListenerMessage
  22. } from '@/helpers/native-message'
  23. import iconCourse from './image/icon-course.png'
  24. import iconCachePoint from './image/icon-cache-point.png'
  25. import play from './image/paly.png'
  26. import { browser } from '@/helpers/utils'
  27. import ColResult from '@/components/col-result'
  28. import { useEventListener } from '@vant/use'
  29. import TheSticky from '@/components/the-sticky'
  30. import ColHeader from '@/components/col-header'
  31. import iconList from './image/icon-list.png'
  32. // import OSticky from '@/components/o-sticky';
  33. export default defineComponent({
  34. name: 'courseList',
  35. setup() {
  36. const route = useRoute()
  37. const router = useRouter()
  38. const browserInfo = browser()
  39. const data = reactive({
  40. titleOpacity: 0,
  41. catchStatus: false,
  42. catchItem: {} as any,
  43. loading: true,
  44. detail: {} as Record<string, any>,
  45. list: [] as any,
  46. isDownloading: false // 是否在下载资源
  47. })
  48. const apiSuffix = ref(
  49. baseState.platformType === 'STUDENT' ? '/api-student' : '/api-teacher'
  50. )
  51. /** 获取课件详情 */
  52. const getDetail = async () => {
  53. try {
  54. const res: any = await request.post(
  55. apiSuffix.value + `/tenantAlbumMusic/getLessonCoursewareDetail`,
  56. {
  57. data: {
  58. lessonCoursewareId: route.query.id,
  59. albumId: route.query.albumId
  60. }
  61. }
  62. )
  63. data.detail = res?.data || {}
  64. } catch {
  65. //
  66. }
  67. }
  68. const getList = async () => {
  69. data.loading = true
  70. try {
  71. const res: any = await request.get(
  72. apiSuffix.value +
  73. '/tenantAlbumMusic/getLessonCoursewareCourseList/' +
  74. route.query.id
  75. )
  76. if (Array.isArray(res?.data)) {
  77. data.list = res.data
  78. res.data.forEach((item: any) => {
  79. const { knowledgePointList, ...res } = item
  80. const tempK = knowledgePointList || []
  81. tempK.forEach((child: any) => {
  82. child.materialList = [
  83. ...(child.materialList || []),
  84. ...getKnowledgeMaterials(child.children || [])
  85. ]
  86. child.children = null
  87. })
  88. })
  89. // 由于ios没有对应api
  90. const _list = await checkCoursewareCache(res.data)
  91. data.list = browserInfo.isApp
  92. ? res.data.map((item: any) => {
  93. const _item = _list.find(
  94. (n: any) =>
  95. n.lessonCoursewareDetailId == item.lessonCoursewareDetailId
  96. )
  97. const n = {
  98. ...item
  99. }
  100. if (_item) {
  101. n.hasCache = _item.hasCache
  102. }
  103. return n
  104. })
  105. : res.data
  106. }
  107. } catch (error) {
  108. //
  109. }
  110. data.loading = false
  111. }
  112. // 获取子节点数据
  113. const getKnowledgeMaterials = (list: any = []) => {
  114. const tempList: any = []
  115. list.forEach((item: any) => {
  116. if (item.materialList && item.materialList.length > 0) {
  117. tempList.push(...(item.materialList || []))
  118. }
  119. if (item.children && item.children.length > 0) {
  120. tempList.push(...getKnowledgeMaterials(item.children || []))
  121. }
  122. })
  123. return tempList
  124. }
  125. onMounted(() => {
  126. getDetail()
  127. getList()
  128. listenerMessage('downloadCoursewareToCache', getProgress)
  129. })
  130. onUnmounted(() => {
  131. removeListenerMessage('downloadCoursewareToCache', getProgress)
  132. })
  133. const handleClick = async (item: any) => {
  134. if (!data.detail?.play) {
  135. if (!browser().isApp) {
  136. onDownloadApp()
  137. return
  138. }
  139. onSubmit()
  140. return
  141. }
  142. if (!item.knowledgePointList) {
  143. Dialog.confirm({
  144. message: '该课件暂无知识点'
  145. })
  146. return
  147. }
  148. if (!item.hasCache) {
  149. // const hasFree = String(item.accessScope) === '0';
  150. // if (!hasFree) {
  151. // 下载中不提示
  152. if (item.downloadStatus == 1) {
  153. // 取消下载
  154. postMessage({ api: 'cancelDownloadCourseware' })
  155. setTimeout(() => {
  156. postMessage({ api: 'cancelDownloadCourseware' })
  157. item.downloadStatus = 0
  158. data.isDownloading = false
  159. }, 1000)
  160. Toast.loading({
  161. message: '取消中...',
  162. forbidClick: false,
  163. loadingType: 'spinner',
  164. duration: 1000
  165. })
  166. return
  167. }
  168. // 重新下载
  169. if (item.downloadStatus == 3) {
  170. downCatch(item)
  171. return
  172. }
  173. data.catchStatus = true
  174. data.catchItem = item
  175. return
  176. }
  177. gotoPlay(item)
  178. }
  179. // 去课件播放
  180. const gotoPlay = (item: any) => {
  181. data.catchStatus = false
  182. if (browser().isApp) {
  183. postMessage({
  184. api: 'openWebView',
  185. content: {
  186. url: `${location.origin}${location.pathname}#/coursewarePlay?id=${item.coursewareDetailId}&source=my-course`,
  187. orientation: 0,
  188. isHideTitle: true,
  189. statusBarTextColor: false,
  190. isOpenLight: true,
  191. showLoadingAnim: true
  192. }
  193. })
  194. } else {
  195. router.push({
  196. path: '/coursewarePlay',
  197. query: {
  198. id: item.coursewareDetailId,
  199. source: 'my-course'
  200. }
  201. })
  202. }
  203. }
  204. // 检查数据的缓存状态
  205. const checkCoursewareCache = (list: []): Promise<any[]> => {
  206. if (!browser().isApp) {
  207. return Promise.resolve(list)
  208. }
  209. return new Promise(resolve => {
  210. postMessage(
  211. {
  212. api: 'checkCoursewareCache',
  213. content: {
  214. data: list
  215. }
  216. },
  217. res => {
  218. if (res?.content?.data) {
  219. resolve(res.content.data)
  220. return
  221. }
  222. return []
  223. }
  224. )
  225. })
  226. }
  227. // 下载缓存
  228. const downCatch = async (item: any) => {
  229. console.log(item)
  230. if (browserInfo.isApp) {
  231. data.catchStatus = false
  232. data.isDownloading = true
  233. const res = await postMessage({
  234. api: 'downloadCoursewareToCache',
  235. content: {
  236. data: item
  237. }
  238. })
  239. return res
  240. }
  241. return true
  242. }
  243. // 下载缓存进度
  244. const getProgress = (res: any) => {
  245. //console.log('🚀 ~ res', res)
  246. // if (!data.isDownloading) {
  247. // return
  248. // }
  249. if (res?.content?.lessonCoursewareDetailId) {
  250. const { lessonCoursewareDetailId, downloadStatus, progress } =
  251. res.content
  252. const course = data.list.find(
  253. (n: any) => n.lessonCoursewareDetailId == lessonCoursewareDetailId
  254. )
  255. if (course) {
  256. course.downloadStatus = downloadStatus
  257. course.progress = progress
  258. if (downloadStatus == 2) {
  259. course.hasCache = 1
  260. course.progress = 100
  261. // 下载完成
  262. data.isDownloading = false
  263. }
  264. }
  265. }
  266. }
  267. useEventListener('scroll', () => {
  268. const height =
  269. window.scrollY ||
  270. window.pageYOffset ||
  271. document.documentElement.scrollTop
  272. data.titleOpacity = height > 100 ? 1 : height / 100
  273. })
  274. // 购买
  275. const onSubmit = async () => {
  276. const url =
  277. apiSuffix.value +
  278. '/tenantGroupAlbum/buyAlbumInfo?tenantGroupAlbumId=' +
  279. (route.query.taId || '')
  280. // if (state.albumId) {
  281. // url = url + '?albumId=' + state.albumId
  282. // }
  283. const { data } = await request.get(url)
  284. const details = data[0]
  285. orderStatus.orderObject.orderType = 'TENANT_ALBUM'
  286. orderStatus.orderObject.orderName = details.name
  287. orderStatus.orderObject.orderDesc = details.name
  288. orderStatus.orderObject.actualPrice = details.actualPrice
  289. // orderStatus.orderObject.recomUserId = route.query.recomUserId || 0
  290. // orderStatus.orderObject.activityId = route.query.activityId || 0
  291. orderStatus.orderObject.orderNo = ''
  292. orderStatus.orderObject.orderList = [
  293. {
  294. orderType: 'TENANT_ALBUM',
  295. goodsName: details.name,
  296. actualPrice: details.actualPrice,
  297. price: details.actualPrice,
  298. ...details
  299. }
  300. ]
  301. const res = await request.post('/api-student/userOrder/getPendingOrder', {
  302. data: {
  303. goodType: 'TENANT_ALBUM',
  304. bizId: details.id
  305. }
  306. })
  307. const result = res.data
  308. if (result) {
  309. Dialog.confirm({
  310. title: '提示',
  311. message: '您有一个未支付的订单,是否继续支付?',
  312. theme: 'round-button',
  313. className: 'confirm-button-group',
  314. cancelButtonText: '取消订单',
  315. confirmButtonText: '继续支付'
  316. })
  317. .then(async () => {
  318. orderStatus.orderObject.orderNo = result.orderNo
  319. orderStatus.orderObject.actualPrice = result.actualPrice
  320. orderStatus.orderObject.discountPrice = result.discountPrice
  321. orderStatus.orderObject.paymentConfig = {
  322. ...result.paymentConfig,
  323. paymentVendor: result.paymentVendor,
  324. paymentVersion: result.paymentVersion
  325. }
  326. routerToALBUM(details.id)
  327. })
  328. .catch(() => {
  329. Dialog.close()
  330. // 只用取消订单,不用做其它处理
  331. cancelPaymentALBUM(result.orderNo)
  332. })
  333. } else {
  334. routerToALBUM(details.id)
  335. }
  336. }
  337. const cancelPaymentALBUM = async (orderNo: string) => {
  338. try {
  339. await request.post('/api-student/userOrder/orderCancel/v2', {
  340. data: {
  341. orderNo
  342. }
  343. })
  344. } catch {
  345. //
  346. }
  347. }
  348. const routerToALBUM = (id: string) => {
  349. router.push({
  350. path: '/orderDetail',
  351. query: {
  352. orderType: 'ALBUM',
  353. album: id
  354. }
  355. })
  356. }
  357. const onDownloadApp = () => {
  358. Dialog.alert({
  359. title: '提示',
  360. message: '请在酷乐秀APP中使用',
  361. confirmButtonColor: '#2dc7aa'
  362. }).then(() => {
  363. window.location.href = location.origin + '/student/#/download'
  364. })
  365. }
  366. return () => (
  367. <div class={styles.courseList}>
  368. <TheSticky position="top">
  369. <ColHeader
  370. hideHeader={false}
  371. background={`rgba(255,255,255, ${data.titleOpacity})`}
  372. isFixed={false}
  373. border={false}
  374. title={'教程详情'}
  375. color="#131415"
  376. />
  377. </TheSticky>
  378. <div class={styles.periodContent}>
  379. <div class={styles.cover}>
  380. <img
  381. src={data.detail.coverImg}
  382. onLoad={(e: Event) => {
  383. if (e.target) {
  384. ;(e.target as any).style.opacity = 1
  385. }
  386. }}
  387. />
  388. </div>
  389. <div>
  390. <div class={styles.contentTitle}>{data.detail.name}</div>
  391. <div class={styles.contentLabel}>
  392. 教学目标:{data.detail.lessonTargetDesc}
  393. </div>
  394. </div>
  395. </div>
  396. <TransitionGroup name="van-fade">
  397. {!data.loading && (
  398. <>
  399. <div key="periodTitle" class={styles.periodTitle}>
  400. <img class={styles.pIcon} src={iconList} />
  401. <div class={styles.pTitle}>课程列表</div>
  402. <div class={styles.pNum}>共{data.list.length}课</div>
  403. </div>
  404. <div key="list" class={styles.periodList}>
  405. <CellGroup inset>
  406. {data.list.map((item: any) => {
  407. // const isLock =
  408. // item.lockFlag ||
  409. // ((route.query.code == 'select' ||
  410. // state.platformType == 'STUDENT') &&
  411. // !item.unlock);
  412. // const isSelect = route.query.code === 'select';
  413. return (
  414. <Cell
  415. border
  416. center
  417. title={item.coursewareDetailName}
  418. // label={
  419. // !browserInfo.isStudent
  420. // ? `已使用${item.useNum || 0}次`
  421. // : ''
  422. // }
  423. onClick={() => handleClick(item)}
  424. >
  425. {{
  426. icon: () => (
  427. <div class={styles.periodItem}>
  428. <div class={styles.periodItemModel}>
  429. <img src={iconCourse} />
  430. {item.hasCache ? (
  431. <img
  432. class={styles.iconCachePoint}
  433. src={iconCachePoint}
  434. />
  435. ) : (
  436. ''
  437. )}
  438. {item.downloadStatus == 1 && (
  439. <div class={styles.downloading}>{`${
  440. item.progress || 0
  441. }%`}</div>
  442. )}
  443. </div>
  444. </div>
  445. ),
  446. value: () => (
  447. <>
  448. {item.knowledgePointList ? (
  449. <>
  450. {item.hasCache ||
  451. item.downloadStatus !== 1 ? (
  452. <img class={styles.basePlay} src={play} />
  453. ) : (
  454. <div class={styles.circleProgress}>
  455. <div class={styles.tips}></div>
  456. <Circle
  457. v-model:current-rate={item.progress}
  458. rate={item.progress}
  459. speed={10}
  460. stroke-width={80}
  461. layer-color={'#B3B3B3'}
  462. color={'#FE2451'}
  463. />
  464. </div>
  465. )}
  466. </>
  467. ) : (
  468. ''
  469. )}
  470. </>
  471. )
  472. }}
  473. </Cell>
  474. )
  475. })}
  476. </CellGroup>
  477. </div>
  478. </>
  479. )}
  480. </TransitionGroup>
  481. {data.loading && (
  482. <div>
  483. <Vue3Lottie
  484. animationData={AstronautJSON}
  485. class={styles.finch}
  486. ></Vue3Lottie>
  487. {/* <p class={styles.finchLoad}>加载中...</p> */}
  488. </div>
  489. )}
  490. {!data.loading && !data.list.length && (
  491. <ColResult tips="暂无内容" classImgSize="SMALL" btnStatus={false} />
  492. )}
  493. <TheSticky position="bottom">
  494. {data.detail.id && !data.detail.play && (
  495. <div class={styles.footers}>
  496. <Button
  497. round
  498. block
  499. type="primary"
  500. color="linear-gradient(270deg, #FF204B 0%, #FE5B71 100%)"
  501. onClick={() => {
  502. if (!browser().isApp) {
  503. onDownloadApp()
  504. return
  505. }
  506. onSubmit()
  507. }}
  508. >
  509. 开通训练教程
  510. </Button>
  511. </div>
  512. )}
  513. </TheSticky>
  514. <Popup
  515. v-model:show={data.catchStatus}
  516. round
  517. class={styles.courseDialog}
  518. >
  519. <i
  520. class={styles.iconClose}
  521. onClick={() => (data.catchStatus = false)}
  522. ></i>
  523. <div class={styles.title}>下载提醒</div>
  524. <div class={styles.content}>
  525. 您尚未下载课件,为了更加流畅的学习体验,推荐您下载后观看课件。
  526. </div>
  527. <div class={styles.popupBtnGroup}>
  528. <Button
  529. class={styles.btnLeft}
  530. round
  531. onClick={() => gotoPlay(data.catchItem)}
  532. >
  533. 直接观看
  534. </Button>
  535. <Button
  536. class={styles.btnRight}
  537. round
  538. type="primary"
  539. onClick={() => downCatch(data.catchItem)}
  540. >
  541. 下载课件
  542. </Button>
  543. </div>
  544. </Popup>
  545. </div>
  546. )
  547. }
  548. })