index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. import { Icon, showConfirmDialog, showToast, Swipe, SwipeItem } from 'vant';
  2. import {
  3. defineComponent,
  4. onMounted,
  5. reactive,
  6. onUnmounted,
  7. ref,
  8. Transition,
  9. watch
  10. } from 'vue';
  11. import styles from './index.module.less';
  12. import 'plyr/dist/plyr.css';
  13. import request from '@/helpers/request';
  14. import { state } from '@/state';
  15. import { useRoute, useRouter } from 'vue-router';
  16. import iconBack from '../coursewarePlay/image/back.svg';
  17. import { postMessage } from '@/helpers/native-message';
  18. import { browser } from '@/helpers/utils';
  19. import qs from 'query-string';
  20. import { Vue3Lottie } from 'vue3-lottie';
  21. import playLoadData from '../coursewarePlay/datas/data.json';
  22. // import { handleCheckVip } from '../hook/useFee';
  23. import VideoClass from './video-class';
  24. import { usePageVisibility } from '@vant/use';
  25. const materialType = {
  26. 视频: 'VIDEO',
  27. 图片: 'IMG',
  28. 曲目: 'SONG'
  29. };
  30. export default defineComponent({
  31. name: 'exercise-after-class',
  32. setup() {
  33. const pageVisibility = usePageVisibility();
  34. /** 设置播放容器 16:9 */
  35. const parentContainer = reactive({
  36. width: '100vw'
  37. });
  38. const setContainer = () => {
  39. const min = Math.min(screen.width, screen.height);
  40. const max = Math.max(screen.width, screen.height);
  41. const width = min * (16 / 9);
  42. if (width > max) {
  43. parentContainer.width = '100vw';
  44. return;
  45. } else {
  46. parentContainer.width = width + 'px';
  47. }
  48. };
  49. const handleInit = (type = 0) => {
  50. setContainer();
  51. // 横屏
  52. postMessage({
  53. api: 'setRequestedOrientation',
  54. content: {
  55. orientation: type
  56. }
  57. });
  58. // 安卓的状态栏
  59. postMessage({
  60. api: 'setStatusBarVisibility',
  61. content: {
  62. isVisibility: type
  63. }
  64. });
  65. };
  66. handleInit();
  67. onUnmounted(() => {
  68. handleInit(1);
  69. });
  70. const route = useRoute();
  71. const query = route.query;
  72. const browserInfo = browser();
  73. const headeRef = ref();
  74. const data = reactive({
  75. isMember: false, // 是否为会员
  76. videoData: null as any,
  77. trainings: [] as any[],
  78. expireTimeFlag: false, // 作业是否结束
  79. trainingTimes: 0,
  80. itemList: [] as any,
  81. showHead: true,
  82. loading: true,
  83. recordLoading: false,
  84. isPlayBaseStatus: true, // 初始状态是否播放完
  85. isPlayAll: true // 是否全部做完
  86. });
  87. const activeData = reactive({
  88. nowTime: 0,
  89. model: true, // 遮罩
  90. timer: null as any,
  91. item: null as any
  92. });
  93. // 获取课后练习记录
  94. const getTrainingRecord = async () => {
  95. try {
  96. const res: any = await request.post(
  97. state.platformApi +
  98. `/studentLessonTraining/trainingRecord/${query.courseScheduleId}?userId=${state.user?.data?.id}`,
  99. {
  100. hideLoading: true
  101. }
  102. );
  103. data.expireTimeFlag = res.data?.expireTimeFlag || false;
  104. if (Array.isArray(res?.data?.trainings)) {
  105. const trainings = res?.data?.trainings || [];
  106. const tempLessonTraining: any = [];
  107. trainings.forEach((item: any) => {
  108. tempLessonTraining.push(
  109. ...(item.studentLessonTrainingDetails || [])
  110. );
  111. });
  112. // 没有播放完
  113. tempLessonTraining.forEach((item: any) => {
  114. let trainingContent: any = {};
  115. try {
  116. trainingContent = JSON.parse(item.trainingContent);
  117. } catch (error) {
  118. trainingContent = '';
  119. }
  120. if (trainingContent.practiceTimes !== item.trainingTimes + '') {
  121. data.isPlayAll = false;
  122. }
  123. if (item.materialId == route.query.materialId) {
  124. popupData.tabName = item.knowledgePointName;
  125. }
  126. });
  127. return tempLessonTraining;
  128. }
  129. } catch (error) {
  130. //
  131. }
  132. return [];
  133. };
  134. const setRecord = async (trainings: any[]) => {
  135. if (Array.isArray(trainings)) {
  136. data.trainings = trainings.map((n: any) => {
  137. const materialRefs = n.materialRefs ? n.materialRefs : [];
  138. const materialMusicId =
  139. materialRefs.length > 0 ? materialRefs[0].resourceId : null;
  140. try {
  141. n.trainingContent = JSON.parse(n.trainingContent);
  142. } catch (error) {
  143. n.trainingContent = '';
  144. }
  145. return {
  146. ...n,
  147. materialMusicId,
  148. currentTime: 0,
  149. duration: 100,
  150. paused: true,
  151. loop: false,
  152. videoEle: null,
  153. timer: null,
  154. // muted: state.user.data?.vipMember ? false : true, // 静音
  155. muted: true,
  156. autoplay: state.user.data?.vipMember ? true : false //自动播放
  157. };
  158. });
  159. console.log(data.trainings, 'trainings');
  160. data.itemList = data.trainings.filter(
  161. (n: any) => n.materialId == route.query.materialId
  162. );
  163. data.videoData = data.itemList[0];
  164. handleExerciseCompleted();
  165. }
  166. };
  167. onMounted(async () => {
  168. const trainings = await getTrainingRecord();
  169. // 初始化状态
  170. trainings.forEach((record: any) => {
  171. let trainingContent: any = {};
  172. try {
  173. trainingContent = JSON.parse(record.trainingContent);
  174. } catch (error) {
  175. trainingContent = '';
  176. }
  177. if (trainingContent.practiceTimes !== record.trainingTimes + '') {
  178. data.isPlayBaseStatus = false;
  179. }
  180. });
  181. setRecord(trainings);
  182. // 是否为会员
  183. // data.isMember = handleCheckVip();
  184. });
  185. // 返回
  186. const goback = () => {
  187. postMessage({ api: 'back' });
  188. };
  189. const swipeRef = ref();
  190. const popupData = reactive({
  191. firstIndex: 0,
  192. open: false,
  193. activeIndex: -1,
  194. tabActive: '',
  195. tabName: '',
  196. itemActive: '',
  197. itemName: ''
  198. });
  199. // 达到指标,记录
  200. const addTrainingRecord = async (m: any) => {
  201. if (data.recordLoading || data.expireTimeFlag) return;
  202. console.log('记录观看次数');
  203. data.recordLoading = true;
  204. const query = route.query;
  205. const body = {
  206. materialType: 'VIDEO',
  207. record: {
  208. sourceTime: m.duration,
  209. clientType: state.platformType,
  210. feature: 'LESSON_TRAINING',
  211. deviceType: browserInfo.android
  212. ? 'ANDROID'
  213. : browserInfo.isApp
  214. ? 'IOS'
  215. : 'WEB'
  216. },
  217. courseScheduleId: query.courseScheduleId,
  218. lessonTrainingId: query.lessonTrainingId,
  219. materialId: data.videoData?.materialId || ''
  220. };
  221. try {
  222. await request.post(
  223. state.platformApi + '/studentLessonTraining/lessonTrainingRecord',
  224. {
  225. data: body,
  226. hideLoading: true
  227. }
  228. );
  229. } catch (error) {
  230. //
  231. }
  232. data.recordLoading = false;
  233. try {
  234. const trainings: any[] = await getTrainingRecord();
  235. if (Array.isArray(trainings)) {
  236. const item = trainings.find(
  237. (n: any) => n.materialId == data.videoData?.materialId
  238. );
  239. if (item) {
  240. data.videoData.trainingTimes = item.trainingTimes;
  241. handleExerciseCompleted();
  242. }
  243. }
  244. } catch (error) {
  245. //
  246. }
  247. };
  248. // 停止所有的播放
  249. const handleStopVideo = () => {
  250. data.itemList.forEach((m: any) => {
  251. m.videoEle?.pause();
  252. });
  253. };
  254. // 判断练习是否完成
  255. const handleExerciseCompleted = () => {
  256. if (
  257. data?.videoData?.trainingTimes != 0 &&
  258. data?.videoData?.trainingTimes + '' ===
  259. data.videoData?.trainingContent?.practiceTimes
  260. ) {
  261. let isLastIndex = false;
  262. let itemIndex = 0;
  263. // console.log(data.isPlayBaseStatus, data.isPlayAll, data.trainings)
  264. if (data.isPlayBaseStatus) {
  265. itemIndex = data.trainings.findIndex(
  266. (n: any) => n.materialId == data.videoData?.materialId
  267. );
  268. isLastIndex = itemIndex === data.trainings.length - 1;
  269. } else {
  270. let i = -1;
  271. let status = true;
  272. data.trainings.forEach((item: any, index: number) => {
  273. if (
  274. item.trainingContent.practiceTimes !== item.trainingTimes + '' &&
  275. i === -1
  276. ) {
  277. // console.log(i, item.trainingContent.practiceTimes, item.trainingTimes, index)
  278. i = index;
  279. }
  280. if (
  281. item.trainingContent.practiceTimes !==
  282. item.trainingTimes + ''
  283. ) {
  284. status = false;
  285. }
  286. });
  287. itemIndex = i != -1 ? i - 1 : -1;
  288. // console.log(status)
  289. isLastIndex = status;
  290. }
  291. showConfirmDialog({
  292. title: '课后作业',
  293. message: '你已完成该练习~',
  294. confirmButtonColor: 'var(--van-primary)',
  295. confirmButtonText: isLastIndex ? '完成' : '下一题',
  296. cancelButtonText: '继续'
  297. })
  298. .then(() => {
  299. if (!isLastIndex) {
  300. const nextItem = data.trainings[itemIndex + 1];
  301. data.videoData?.expired;
  302. if (nextItem.expired) {
  303. showToast('该资源已过期');
  304. return;
  305. }
  306. if (nextItem.knowledgePointName) {
  307. popupData.tabName = nextItem.knowledgePointName;
  308. }
  309. if (nextItem?.type === materialType.视频) {
  310. data.itemList = [nextItem];
  311. data.videoData = nextItem;
  312. handleExerciseCompleted();
  313. }
  314. } else {
  315. postMessage({ api: 'goBack' });
  316. }
  317. })
  318. .catch(() => {
  319. data.trainings[itemIndex].currentTime = 0;
  320. });
  321. }
  322. };
  323. watch(pageVisibility, (value: any) => {
  324. handleStopVideo();
  325. if (value == 'visible') {
  326. // 横屏
  327. postMessage(
  328. {
  329. api: 'setRequestedOrientation',
  330. content: {
  331. orientation: 0
  332. }
  333. },
  334. () => {
  335. // console.log(234);
  336. }
  337. );
  338. }
  339. });
  340. return () => (
  341. <div class={styles.playContent}>
  342. <div
  343. class={styles.coursewarePlay}
  344. style={{ width: parentContainer.width }}>
  345. <Swipe
  346. style={{ height: '100%' }}
  347. ref={swipeRef}
  348. showIndicators={false}
  349. loop={false}
  350. vertical
  351. lazyRender={true}
  352. touchable={false}
  353. duration={0}>
  354. {data.itemList.map((m: any) => {
  355. return (
  356. <SwipeItem>
  357. <>
  358. <VideoClass
  359. item={m}
  360. isMember={data.isMember}
  361. modal={activeData.model}
  362. onEnded={(m: any) => addTrainingRecord(m)}
  363. onChangeModal={(status: boolean) => {
  364. activeData.model = status;
  365. }}
  366. />
  367. {m.muted && (
  368. <div class={styles.loadWrap}>
  369. <Vue3Lottie animationData={playLoadData}></Vue3Lottie>
  370. </div>
  371. )}
  372. </>
  373. </SwipeItem>
  374. );
  375. })}
  376. </Swipe>
  377. <Transition name="top">
  378. {activeData.model && (
  379. <div class={styles.headerContainer} ref={headeRef}>
  380. <div class={styles.backBtn} onClick={() => goback()}>
  381. <Icon name={iconBack} />
  382. 返回
  383. </div>
  384. <div class={styles.menu}>{popupData.tabName}</div>
  385. {/* 判断作业是否过期 */}
  386. {!data.expireTimeFlag && (
  387. <div class={styles.nums}>
  388. 观看视频模仿并练习:{data.videoData?.trainingTimes || 0}/
  389. {data.videoData?.trainingContent?.practiceTimes || 0}
  390. </div>
  391. )}
  392. </div>
  393. )}
  394. </Transition>
  395. </div>
  396. </div>
  397. );
  398. }
  399. });