index.tsx 17 KB

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