index.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import { Button, Icon, Popup, Swipe, SwipeItem, Tab, Tabs } from 'vant'
  2. import {
  3. defineComponent,
  4. onMounted,
  5. reactive,
  6. nextTick,
  7. onUnmounted,
  8. ref,
  9. watch,
  10. Transition
  11. } from 'vue'
  12. import iconBack from './image/back.svg'
  13. import styles from './index.module.less'
  14. import Plyr from 'plyr'
  15. import 'plyr/dist/plyr.css'
  16. import request from '@/helpers/request'
  17. import { state } from '@/state'
  18. import { useRoute } from 'vue-router'
  19. import { postMessage } from '@/helpers/native-message'
  20. import OHeader from '@/components/o-header'
  21. import MusicScore from './component/musicScore'
  22. import iconMenu from './image/icon-menu.svg'
  23. import Points from './component/points'
  24. export default defineComponent({
  25. name: 'CoursewarePlay',
  26. setup() {
  27. const handleInit = () => {
  28. postMessage({
  29. api: 'setRequestedOrientation',
  30. content: {
  31. orientation: 0
  32. }
  33. })
  34. postMessage({
  35. api: 'setBarStatus',
  36. content: {
  37. status: 0
  38. }
  39. })
  40. postMessage({
  41. api: 'setStatusBarVisibility',
  42. content: {
  43. isVisibility: 0
  44. }
  45. })
  46. // window.addEventListener('message', (res: any) => {
  47. // // console.log(res)
  48. // if (res?.data?.api) {
  49. // const { api } = res.data
  50. // if (api === 'touchstart') {
  51. // isTouch.value = true
  52. // console.log('🚀 ~ 父页面touchstart')
  53. // }
  54. // if (api === 'touchend') {
  55. // isTouch.value = false
  56. // console.log('🚀 ~ 父页面touchend')
  57. // }
  58. // }
  59. // })
  60. }
  61. const route = useRoute()
  62. const data = reactive({
  63. detail: null,
  64. active: '',
  65. knowledgePointList: [] as any,
  66. showHead: true,
  67. players: [] as any
  68. })
  69. const getDetail = async () => {
  70. try {
  71. const res: any = await request.get(
  72. state.platformApi + `/lessonCoursewareDetail/detail/${route.query.id}`
  73. )
  74. if (Array.isArray(res?.data)) {
  75. data.detail = res.data
  76. }
  77. if (Array.isArray(res?.data?.knowledgePointList)) {
  78. data.knowledgePointList = res.data.knowledgePointList.map((n: any) => {
  79. n.index = 0
  80. return n
  81. })
  82. }
  83. console.log('数据加载完成')
  84. } catch (error) {}
  85. nextTick(() => {
  86. console.log('开始加载视频')
  87. videoInit()
  88. })
  89. }
  90. const videoInit = () => {
  91. // console.log(document.querySelectorAll('.player'))
  92. data.players = Plyr.setup('.player', {
  93. debug: false,
  94. ratio: '16:9',
  95. controls: ['play-large', 'play', 'progress', 'current-time', 'mute', 'volume', 'captions']
  96. })
  97. data.players.forEach((p: Plyr) => {
  98. console.log(p)
  99. p.on('play', () => {
  100. console.log('开始播放了')
  101. data.showHead = false
  102. })
  103. p.on('pause', () => {
  104. console.log('暂停了')
  105. data.showHead = true
  106. })
  107. })
  108. console.log('🚀 ~ player', data.players)
  109. }
  110. onMounted(() => {
  111. handleInit()
  112. getDetail()
  113. })
  114. // 返回
  115. const goback = () => {
  116. history.go(-1)
  117. }
  118. // 所有的切换
  119. const handleChange = () => {
  120. // console.log('切换了')
  121. const iframes = document.querySelectorAll('.musicIframe')
  122. Array.from(iframes).map((f: any) => {
  123. f.contentWindow.postMessage({ api: 'setPlayState' }, '*')
  124. })
  125. data.players.forEach((p: any) => {
  126. p.stop()
  127. })
  128. }
  129. onUnmounted(() => {
  130. postMessage({
  131. api: 'setRequestedOrientation',
  132. content: {
  133. orientation: 1
  134. }
  135. })
  136. postMessage({
  137. api: 'setBarStatus',
  138. content: {
  139. status: 1
  140. }
  141. })
  142. postMessage({
  143. api: 'setStatusBarVisibility',
  144. content: {
  145. isVisibility: 1
  146. }
  147. })
  148. })
  149. const popupData = reactive({
  150. open: false,
  151. active: ''
  152. })
  153. // 监听切换
  154. watch(
  155. () => popupData.active,
  156. () => {
  157. console.log(data.active, '监听切换')
  158. handleChange()
  159. const knowledge = data.knowledgePointList.find((n: any) => n.id === data.active)
  160. if (knowledge && knowledge?.materialList[knowledge.index]) {
  161. // 如果是曲谱,隐藏头部
  162. if (knowledge.materialList[knowledge.index]?.type === 'SONG') {
  163. data.showHead = false
  164. return
  165. }
  166. }
  167. data.showHead = true
  168. }
  169. )
  170. return () => (
  171. <div class={styles.coursewarePlay}>
  172. <Transition name='top'>
  173. {data.showHead && (
  174. <div class={styles.headerContainer}>
  175. <div class={styles.backBtn} onClick={() => goback()}>
  176. <Icon name={iconBack} />
  177. 返回
  178. </div>
  179. <div class={styles.menu}>
  180. <Tabs
  181. v-model:active={data.active}
  182. ellipsis={false}
  183. onChange={() => {
  184. const knowledge = data.knowledgePointList.find((_: any) => _.id === data.active)
  185. popupData.active = `${data.active}-${
  186. knowledge?.materialList?.[knowledge.index]?.id
  187. }`
  188. }}
  189. >
  190. {{
  191. default: () => {
  192. return data.knowledgePointList.map((n: any) => {
  193. return <Tab title={n.name} name={n.id}></Tab>
  194. })
  195. }
  196. // 'nav-right': () => <div style={{width: '40%'}} class={styles.menuLine}></div>
  197. }}
  198. </Tabs>
  199. </div>
  200. </div>
  201. )}
  202. </Transition>
  203. <Tabs class={styles.tabsContent} animated lazyRender={false} v-model:active={data.active}>
  204. {data.knowledgePointList.map((item: any) => {
  205. return (
  206. <Tab name={item.id}>
  207. <Swipe
  208. style={{ height: '100vh' }}
  209. showIndicators={false}
  210. loop={false}
  211. initialSwipe={item.index}
  212. vertical
  213. lazyRender={false}
  214. onChange={(val: any) => {
  215. item.index = val
  216. popupData.active = `${item.id}-${item?.materialList?.[item.index]?.id}`
  217. }}
  218. >
  219. {Array.isArray(item?.materialList) &&
  220. item.materialList.map((m: any) => {
  221. if (popupData.active === '') {
  222. popupData.active = `${item.id}-${m.id}`
  223. console.log('🚀 ~ popupData', popupData)
  224. }
  225. return (
  226. <SwipeItem>
  227. {m.type === 'VIDEO' ? (
  228. <div class={styles.videoItem}>
  229. <video class="player" controls>
  230. <source src={m.content} type="video/mp4" />
  231. </video>
  232. </div>
  233. ) : m.type === 'IMG' ? (
  234. <div class={styles.imgItem}>
  235. <img src={m.content} />
  236. </div>
  237. ) : (
  238. <div class={styles.songItem}>
  239. <MusicScore music={m} />
  240. </div>
  241. )}
  242. </SwipeItem>
  243. )
  244. })}
  245. </Swipe>
  246. </Tab>
  247. )
  248. })}
  249. </Tabs>
  250. <div class={styles.fullBtn} onClick={() => (popupData.open = true)}>
  251. <img src={iconMenu} />
  252. <span>列表</span>
  253. </div>
  254. <Popup
  255. class={styles.popup}
  256. overlayClass={styles.overlayClass}
  257. position="left"
  258. v-model:show={popupData.open}
  259. >
  260. <Points
  261. data={data.knowledgePointList}
  262. active={popupData.active}
  263. onHandleSelect={(res: any) => {
  264. data.active = res.id
  265. const knowledge = data.knowledgePointList.find((n: any) => n.id === res.id)
  266. // console.log(res,knowledge)
  267. knowledge && (knowledge.index = res.index)
  268. popupData.active = res.active
  269. popupData.open = false
  270. }}
  271. />
  272. </Popup>
  273. </div>
  274. )
  275. }
  276. })