index.tsx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. import {
  2. computed,
  3. defineComponent,
  4. onMounted,
  5. reactive,
  6. ref,
  7. watch,
  8. nextTick
  9. } from 'vue'
  10. import {
  11. Image,
  12. Tabs,
  13. Tab,
  14. List,
  15. Button,
  16. Popup,
  17. Dialog,
  18. Sticky,
  19. Swipe,
  20. SwipeItem
  21. } from 'vant'
  22. import styles from './index.module.less'
  23. import TheSticky from '@/components/the-sticky'
  24. import ColHeader from '@/components/col-header'
  25. import { useWindowScroll, useEventListener } from '@vueuse/core'
  26. import request from '@/helpers/request'
  27. import iconMenu from './images/icon-menu.png'
  28. import iconRightTop from './images/icon-right-top.png'
  29. import iconAlbumCover from '../../images/icon-album-cover.png'
  30. import iconTimer from './images/icon-timer.png'
  31. import { state as baseState, setLogout } from '@/state'
  32. import Song from '../component/song'
  33. import { useRoute, useRouter } from 'vue-router'
  34. import ColResult from '@/components/col-result'
  35. import { moneyFormat } from '@/helpers/utils'
  36. import { orderStatus } from '@/views/order-detail/orderStatus'
  37. import { postMessage } from '@/helpers/native-message'
  38. import { browser } from '@/helpers/utils'
  39. // Import Swiper Vue.js components
  40. // import Swiper core and required modules
  41. import { Pagination } from 'swiper/modules'
  42. import { Swiper, SwiperSlide } from 'swiper/vue'
  43. // Import Swiper styles
  44. import 'swiper/css'
  45. import 'swiper/css/pagination'
  46. import CourseItem from '../lessonCourseware/component/CourseItem'
  47. export default defineComponent({
  48. name: 'train-tool',
  49. setup() {
  50. const sessionStorageToolSubject =
  51. sessionStorage.getItem('tool-subject-type')
  52. const toolSubject =
  53. sessionStorageToolSubject && JSON.parse(sessionStorageToolSubject)
  54. sessionStorage.removeItem('tool-subject-type')
  55. const route = useRoute()
  56. const router = useRouter()
  57. const background = ref<string>('rgba(55, 205, 177, 0)')
  58. const color = ref<string>('#fff')
  59. const state = reactive({
  60. details: {} as any,
  61. buy: route.query.buy as any,
  62. albumId: route.query.albumId || null,
  63. activeTab:
  64. toolSubject?.activeTab || route.query.subjectType || 'COURSEWARE', // 有缓存 默认用缓存,之后用请求头,最后默认
  65. loadingAlbum: false,
  66. loading: false,
  67. finished: false,
  68. isError: false,
  69. list: [] as any,
  70. popupStatus: false,
  71. selectMember: {} as any, // 购买的月份
  72. ensembleCounts: false,
  73. musicCounts: false,
  74. subjectCounts: false,
  75. coursewareCounts: false,
  76. tenantAlbumStatus: 0 as any,
  77. ablumStatus: false,
  78. heightV: 0,
  79. hasBuyStatus: true, // 是否能继续购买
  80. albumList: [] as any, // 专辑列表
  81. initialSlide: 0
  82. })
  83. const params = reactive({
  84. page: 1,
  85. rows: 20
  86. })
  87. const apiSuffix = ref(
  88. baseState.platformType === 'STUDENT' ? '/api-student' : '/api-teacher'
  89. )
  90. const isSingleAlbum = computed(() => {
  91. const query = route.query
  92. if (query.taId || (query.albumId && state.buy === '1')) {
  93. return true
  94. } else {
  95. return false
  96. }
  97. })
  98. const getDetails = async () => {
  99. state.loadingAlbum = true
  100. try {
  101. // tenantGroupAlbum/buyAlbumInfo
  102. // 当我的曲目过来的时候才走单个查询
  103. if (state.albumId && state.buy === '1') {
  104. let url = apiSuffix.value + '/userTenantAlbumRecord/detail'
  105. if (state.albumId) {
  106. url = url + '?albumId=' + state.albumId
  107. }
  108. const { data } = await request.post(url)
  109. state.albumList = [data || {}]
  110. state.details = data || {}
  111. } else {
  112. const url =
  113. apiSuffix.value +
  114. `/tenantGroupAlbum/buyAlbumInfo?tenantGroupAlbumId=${
  115. route.query.taId || ''
  116. }`
  117. //&tenantAlbumId=${state.albumId || ''}
  118. // if (state.albumId) {
  119. // url = url + '?albumId=' + state.albumId
  120. // }
  121. const { data } = await request.get(url)
  122. state.albumList = data || []
  123. if (state.albumList.length > 0) {
  124. let index = 0
  125. // 以缓存为优先 其次 请求头 state.albumId
  126. if (toolSubject?.tenantGroupAlbumId || state.albumId) {
  127. index = state.albumList.findIndex(item => {
  128. return toolSubject?.tenantGroupAlbumId
  129. ? item.tenantGroupAlbumId === toolSubject?.tenantGroupAlbumId
  130. : item.id == state.albumId // 这里不全等 因为state.albumId为字符串 id为number
  131. })
  132. index < 0 && (index = 0)
  133. }
  134. state.initialSlide = index //默认展示第几个
  135. state.details = state.albumList[index] // 有缓存 就用缓存里面的数据
  136. } else {
  137. // state.albumList
  138. if (!browser().isApp) {
  139. Dialog.alert({
  140. title: '提示',
  141. message: '该教程不可购买',
  142. confirmButtonText: '确定',
  143. confirmButtonColor: '#2dc7aa'
  144. }).then(() => {
  145. if (browser().isApp) {
  146. postMessage({ api: 'back' })
  147. } else {
  148. setLogout()
  149. router.replace({
  150. path: '/login' as any,
  151. query: {
  152. returnUrl: '/train-tool',
  153. ...route.query
  154. }
  155. })
  156. }
  157. })
  158. }
  159. }
  160. }
  161. } catch {
  162. //
  163. }
  164. state.loadingAlbum = false
  165. }
  166. watch(
  167. () => state.details,
  168. () => {
  169. state.ensembleCounts = state.details?.ensembleCounts ? true : false
  170. state.subjectCounts = state.details?.subjectCounts ? true : false
  171. state.musicCounts = state.details?.musicCounts ? true : false
  172. state.coursewareCounts = state.details?.coursewareCounts ? true : false
  173. if (state.details.buyTimesFlag) {
  174. if (state.details.buyedTimes >= state.details.buyTimes) {
  175. state.hasBuyStatus = false
  176. } else {
  177. state.hasBuyStatus = true
  178. }
  179. } else {
  180. state.hasBuyStatus = true
  181. }
  182. }
  183. )
  184. let listController
  185. const FetchList = async (hideLoading = false) => {
  186. if (listController) {
  187. listController.abort()
  188. }
  189. state.loading = true
  190. state.isError = false
  191. const tempParams = {
  192. albumId: state.details.id || null,
  193. subjectType: state.activeTab,
  194. ...params
  195. }
  196. try {
  197. listController = new AbortController()
  198. const { signal } = listController
  199. const { data } = await request.post(
  200. `${apiSuffix.value}/tenantAlbumMusic/page`,
  201. {
  202. hideLoading,
  203. data: tempParams,
  204. signal
  205. }
  206. )
  207. if (state.list.length > 0 && data.pageNo === 1) {
  208. return
  209. }
  210. state.list = state.list.concat(data.rows || [])
  211. params.page = data.pageNo + 1
  212. // showContact.value = state.list.length > 0
  213. state.loading = false
  214. state.finished = data.pageNo >= data.totalPage
  215. params.page = data.pageNo + 1
  216. } catch (error) {
  217. state.isError = true
  218. }
  219. state.loading = false
  220. }
  221. onMounted(async () => {
  222. // useEventListener(document, 'scroll', evt => {
  223. // const { y } = useWindowScroll()
  224. // if (y.value > 20) {
  225. // background.value = `rgba(255, 255, 255)`
  226. // } else {
  227. // background.value = 'transparent'
  228. // }
  229. // })
  230. state.loading = true
  231. state.loadingAlbum = true
  232. await getDetails()
  233. await FetchList()
  234. state.loadingAlbum = false
  235. state.loading = false
  236. // 为了处理 swiper 会不显示的问题
  237. document.body.scrollIntoView()
  238. window.scrollTo(1, 0)
  239. })
  240. function handleChangeActiveTab() {
  241. state.activeTab = state.details?.coursewareCounts
  242. ? 'COURSEWARE'
  243. : state.details?.subjectCounts
  244. ? 'SUBJECT'
  245. : state.details?.musicCounts
  246. ? 'MUSIC'
  247. : 'ENSEMBLE'
  248. }
  249. const onSubmit = async () => {
  250. const album = state.details
  251. const details = state.details
  252. orderStatus.orderObject.orderType = 'TENANT_ALBUM'
  253. orderStatus.orderObject.orderName = details.name
  254. orderStatus.orderObject.orderDesc = details.name
  255. orderStatus.orderObject.actualPrice = album.actualPrice
  256. // orderStatus.orderObject.recomUserId = route.query.recomUserId || 0
  257. // orderStatus.orderObject.activityId = route.query.activityId || 0
  258. orderStatus.orderObject.orderNo = ''
  259. orderStatus.orderObject.orderList = [
  260. {
  261. orderType: 'TENANT_ALBUM',
  262. goodsName: details.name,
  263. actualPrice: album.actualPrice,
  264. price: album.actualPrice,
  265. ...details,
  266. ...album
  267. }
  268. ]
  269. const res = await request.post('/api-student/userOrder/getPendingOrder', {
  270. data: {
  271. goodType: 'TENANT_ALBUM',
  272. bizId: details.id
  273. }
  274. })
  275. const result = res.data
  276. if (result) {
  277. state.popupStatus = false
  278. Dialog.confirm({
  279. title: '提示',
  280. message: '您有一个未支付的订单,是否继续支付?',
  281. theme: 'round-button',
  282. className: 'confirm-button-group',
  283. cancelButtonText: '取消订单',
  284. confirmButtonText: '继续支付'
  285. })
  286. .then(async () => {
  287. orderStatus.orderObject.orderNo = result.orderNo
  288. orderStatus.orderObject.actualPrice = result.actualPrice
  289. orderStatus.orderObject.discountPrice = result.discountPrice
  290. orderStatus.orderObject.paymentConfig = {
  291. ...result.paymentConfig,
  292. paymentVendor: result.paymentVendor,
  293. paymentVersion: result.paymentVersion
  294. }
  295. routerTo()
  296. })
  297. .catch(() => {
  298. Dialog.close()
  299. // 只用取消订单,不用做其它处理
  300. cancelPayment(result.orderNo)
  301. })
  302. } else {
  303. routerTo()
  304. }
  305. }
  306. const routerTo = () => {
  307. const album = state.details
  308. router.push({
  309. path: '/orderDetail',
  310. query: {
  311. orderType: 'ALBUM',
  312. album: album.id
  313. }
  314. })
  315. }
  316. const cancelPayment = async (orderNo: string) => {
  317. try {
  318. await request.post('/api-student/userOrder/orderCancel/v2', {
  319. data: {
  320. orderNo
  321. }
  322. })
  323. } catch {
  324. //
  325. }
  326. }
  327. return () => (
  328. <div class={styles.trainTool}>
  329. {!state.loading && !state.details.id && state.buy != '1' ? (
  330. <>
  331. <TheSticky
  332. class={styles.theSticky}
  333. position="top"
  334. onBarHeight={(height: any) => {
  335. console.log(height, 'height', height)
  336. state.heightV = height
  337. }}
  338. >
  339. <ColHeader border={false} isFixed={false} />
  340. </TheSticky>
  341. {!state.loading && (
  342. <div
  343. style={{
  344. height: 'calc(100vh - var(--header-height))',
  345. display: 'flex',
  346. alignItems: 'center'
  347. }}
  348. >
  349. <ColResult
  350. tips="暂无教程"
  351. classImgSize="SMALL"
  352. btnStatus={false}
  353. />
  354. </div>
  355. )}
  356. </>
  357. ) : (
  358. !state.loadingAlbum && (
  359. <>
  360. <TheSticky
  361. class={styles.theSticky}
  362. position="top"
  363. onBarHeight={(height: any) => {
  364. state.heightV = height
  365. }}
  366. >
  367. <ColHeader
  368. background={background.value}
  369. border={false}
  370. isFixed={false}
  371. hideHeader={route.query.taId ? true : false}
  372. // color={color.value}
  373. // backIconColor="white"
  374. />
  375. </TheSticky>
  376. {/* <img class={styles.bgImg} src={state.details?.coverImg} /> */}
  377. <div class={styles.musicContent}></div>
  378. <div class={styles.bg}>
  379. <div class={styles.alumWrap}>
  380. {isSingleAlbum.value ? (
  381. <div class={styles.singleAlbum}>
  382. <div class={styles.img}>
  383. {state.details?.buyTimesFlag && (
  384. <span class={styles.quota}>
  385. 限购:{state.details?.buyedTimes}/
  386. {state.details?.buyTimes}次
  387. </span>
  388. )}
  389. <Image
  390. class={styles.image}
  391. width="100%"
  392. height="100%"
  393. fit="cover"
  394. src={state.details?.coverImg || iconAlbumCover}
  395. errorIcon={iconAlbumCover}
  396. />
  397. <div class={styles.iconPian}></div>
  398. </div>
  399. </div>
  400. ) : (
  401. state.albumList &&
  402. state.albumList.length > 0 && (
  403. <Swiper
  404. initialSlide={state.initialSlide}
  405. watchSlidesProgress={true}
  406. slidesPerView={'auto'}
  407. centeredSlides={true}
  408. modules={[Pagination]}
  409. pagination={{ clickable: true }}
  410. // onTransitionEnd={(swiper: any) => {}} onSlideChange
  411. onSlideChange={(swiper: any) => {
  412. state.details = state.albumList[swiper.activeIndex]
  413. // 等tab渲染完了之后再切换 不然tab会自动重新赋值
  414. nextTick(() => {
  415. // 当有初始值的时候不刷新
  416. if (state.initialSlide) {
  417. state.initialSlide = 0
  418. return
  419. }
  420. handleChangeActiveTab()
  421. params.page = 1
  422. state.list = []
  423. FetchList(true)
  424. })
  425. }}
  426. >
  427. {state.albumList.map((album: any) => (
  428. <SwiperSlide>
  429. <div class={styles.img}>
  430. {album.buyTimesFlag && (
  431. <span class={styles.quota}>
  432. 限购{album.buyedTimes}/{album.buyTimes}次
  433. </span>
  434. )}
  435. <Image
  436. class={styles.image}
  437. width="100%"
  438. height="100%"
  439. fit="cover"
  440. src={album?.coverImg || iconAlbumCover}
  441. errorIcon={iconAlbumCover}
  442. />
  443. <div class={styles.iconPian}></div>
  444. </div>
  445. </SwiperSlide>
  446. ))}
  447. </Swiper>
  448. )
  449. )}
  450. <div class={styles.alumDes}>
  451. <div class={[styles.alumTitle, 'van-ellipsis']}>
  452. {state.details?.name}
  453. </div>
  454. <div
  455. class={[styles.des, 'van-multi-ellipsis--l2']}
  456. style={{
  457. height: '32px',
  458. lineHeight: '16px'
  459. }}
  460. >
  461. {state.details?.describe}
  462. </div>
  463. </div>
  464. {state.buy != '1' && baseState.platformType === 'STUDENT' && (
  465. <div class={styles.albumPriceGroup}>
  466. <div class={styles.albumTimer}>
  467. <img src={iconTimer} class={styles.iconTimer} />
  468. <span>有效期:{state.details?.purchaseNum || 0}天</span>
  469. </div>
  470. <div class={styles.albumPriceList}>
  471. {(state.details?.originalPrice || 0) >
  472. (state.details?.actualPrice || 0) && (
  473. <del class={styles.originPrice}>
  474. 原价:¥
  475. {moneyFormat(state.details?.originalPrice || 0)}
  476. </del>
  477. )}
  478. <span class={styles.currentPrice}>
  479. <span>
  480. ¥{moneyFormat(state.details?.actualPrice || 0)}
  481. </span>
  482. </span>
  483. </div>
  484. </div>
  485. )}
  486. </div>
  487. </div>
  488. <div class={styles.musicList}>
  489. <Sticky position="top" offsetTop={state.heightV}>
  490. <Tabs
  491. color="var(--van-primary)"
  492. background="transparent"
  493. lineWidth={20}
  494. shrink
  495. v-model:active={state.activeTab}
  496. onClick-tab={val => {
  497. state.activeTab = val.name
  498. params.page = 1
  499. state.list = []
  500. FetchList()
  501. }}
  502. >
  503. {state.coursewareCounts && (
  504. <Tab title="云教程" name="COURSEWARE"></Tab>
  505. )}
  506. {state.subjectCounts && (
  507. <Tab title="声部练习" name="SUBJECT"></Tab>
  508. )}
  509. {state.musicCounts && (
  510. <Tab title="独奏曲目" name="MUSIC"></Tab>
  511. )}
  512. {state.ensembleCounts && (
  513. <Tab title="合奏练习" name="ENSEMBLE"></Tab>
  514. )}
  515. </Tabs>
  516. </Sticky>
  517. <div
  518. class={[
  519. styles.alumnList,
  520. state.activeTab === 'COURSEWARE'
  521. ? styles.alumnListCourseware
  522. : ''
  523. ]}
  524. >
  525. <List
  526. loading={state.loading}
  527. finished={state.finished}
  528. finished-text={' '}
  529. onLoad={FetchList}
  530. immediateCheck={false}
  531. error={state.isError}
  532. >
  533. {state.list && state.list.length ? (
  534. state.activeTab === 'COURSEWARE' ? (
  535. <CourseItem
  536. list={state.list.map(item => {
  537. return {
  538. name: item.musicSheetName,
  539. coverImg: item.titleImg,
  540. id: item.id
  541. }
  542. })}
  543. onItemClick={row => {
  544. sessionStorage.setItem(
  545. 'tool-subject-type',
  546. JSON.stringify({
  547. activeTab: state.activeTab,
  548. tenantGroupAlbumId:
  549. state.details.tenantGroupAlbumId
  550. })
  551. )
  552. router.push({
  553. path: '/courseList',
  554. query: {
  555. id: row.id,
  556. albumId: state.details.id,
  557. taId: state.details.tenantGroupAlbumId // 当通过我的曲目进来的时候 这个值为空
  558. }
  559. })
  560. }}
  561. />
  562. ) : (
  563. <Song
  564. showNumber
  565. list={state.list}
  566. onDetail={(item: any) => {
  567. sessionStorage.setItem(
  568. 'tool-subject-type',
  569. JSON.stringify({
  570. activeTab: state.activeTab,
  571. tenantGroupAlbumId:
  572. state.details.tenantGroupAlbumId
  573. })
  574. )
  575. router.push({
  576. path: '/music-detail',
  577. query: {
  578. id: item.id,
  579. tenantAlbumId: item.tenantAlbumId,
  580. taId: state.details.tenantGroupAlbumId // 当通过我的曲目进来的时候 这个值为空
  581. }
  582. })
  583. }}
  584. />
  585. )
  586. ) : (
  587. !state.loading && (
  588. <ColResult
  589. tips={
  590. state.activeTab === 'COURSEWARE'
  591. ? '暂无教材'
  592. : '暂无曲目'
  593. }
  594. classImgSize="SMALL"
  595. btnStatus={false}
  596. />
  597. )
  598. )}
  599. </List>
  600. </div>
  601. </div>
  602. {baseState.platformType === 'STUDENT' && state.buy != '1' && (
  603. <TheSticky position="bottom">
  604. <div class={styles.btnGroup}>
  605. <Button
  606. round
  607. block
  608. disabled={!state.hasBuyStatus}
  609. color="linear-gradient(270deg, #FF204B 0%, #FE5B71 100%)"
  610. onClick={onSubmit}
  611. >
  612. 开通训练教程
  613. </Button>
  614. </div>
  615. </TheSticky>
  616. )}
  617. </>
  618. )
  619. )}
  620. </div>
  621. )
  622. }
  623. })