index.tsx 23 KB

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