detail.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <template>
  2. <view>
  3. <view class="video_play" v-if="videoInfo.video_src">
  4. <video
  5. id="myVideo"
  6. class="video_control"
  7. @play="_videoPlay"
  8. :src="videoInfo.video_src"
  9. @timeupdate="timeUpdate"
  10. :enable-progress-gesture="false"
  11. :enable-play-gesture="true"
  12. :enable-auto-rotation="true"
  13. :initial-time="videoInfo.inittime || 0"
  14. :show-bottom-progress="videoInfo.learn_status == 1"
  15. @ended="_videoEnd"
  16. @fullscreenchange="screenChange"
  17. ></video>
  18. <cover-view
  19. v-if="videoInfo.learn_status == 0"
  20. :class="bigsScreen ? 'bigScreen' : 'cover'"
  21. ></cover-view>
  22. </view>
  23. <view class="video_title">{{ videoInfo.name }}</view>
  24. <view class="rich_text">
  25. <rich-text :nodes="videoInfo.content"></rich-text>
  26. </view>
  27. <uni-popup ref="questionRef" type="center" :is-mask-click="false">
  28. <view class="popup_content">
  29. <!-- 答题区域 -->
  30. <view class="content" v-if="!show_answer">
  31. <!-- 问题内容 -->
  32. <view class="question_content">{{ questinInfo.question_title }}</view>
  33. <!-- 问题选项 -->
  34. <view class="question_options">
  35. <view
  36. v-for="(option, index) in questinInfo.answer_list"
  37. :key="index"
  38. :class="['question_answer', { active: answer_id == option.id }]"
  39. @click="_selectAnswer(option.id)"
  40. >
  41. {{ option.value }}
  42. </view>
  43. </view>
  44. <view
  45. :class="['submit_btn', { active: answer_id }]"
  46. @click="_submitAnswer"
  47. >提交</view
  48. >
  49. </view>
  50. <!-- 结果区域 -->
  51. <view class="content" v-else>
  52. <icon
  53. :type="answerInfo.is_answer == 1 ? 'success' : 'cancel'"
  54. size="64"
  55. />
  56. <view class="title">{{
  57. answerInfo.is_answer == 1 ? "恭喜您,答对啦!" : "很遗憾,答错了"
  58. }}</view>
  59. <view class="tip">{{
  60. answerInfo.is_answer == 1
  61. ? "继续保持,你正在进步!"
  62. : "别灰心,再接再厉!"
  63. }}</view>
  64. <view class="score_content">
  65. <view>得分</view>
  66. <view
  67. :style="answerInfo.get_score == 0 ? 'color:red' : 'color:green'"
  68. >+{{ answerInfo.get_score }}</view
  69. >
  70. </view>
  71. <view class="submit_btn active" @click="_goPlayVedio"
  72. >继续学习视频</view
  73. >
  74. </view>
  75. </view>
  76. </uni-popup>
  77. </view>
  78. </template>
  79. <script>
  80. export default {
  81. data() {
  82. return {
  83. videoInfo: {
  84. id: 0,
  85. name: "",
  86. content: "",
  87. video_src: "",
  88. },
  89. // 请求参数
  90. requestParam: {
  91. id: 0,
  92. },
  93. isReqing: false,
  94. videoContext: null,
  95. isAnswerQuestion: false,
  96. timer: null,
  97. currentTime: 0, // 新增变量用于存储当前播放时间
  98. questionTime_list: [],
  99. questinInfo: {},
  100. answer_id: null,
  101. is_correct: false,
  102. show_answer: false,
  103. answeredQuestions: new Set(), // 新增变量用于存储已回答的问题
  104. countdown: 0, // 新增变量用于存储倒计时时间
  105. bigsScreen: false,
  106. };
  107. },
  108. onLoad(param) {
  109. // 参数接收
  110. this.requestParam.id = param.id;
  111. // #ifdef MP-WEIXIN
  112. //分享按钮
  113. uni.showShareMenu({
  114. withShareTicket: true,
  115. menus: ["shareAppMessage", "shareTimeline"],
  116. });
  117. // #endif
  118. uni.enableAlertBeforeUnload({
  119. message: "您确定要退出学习吗?",
  120. success: function (res) {
  121. console.log("方法注册成功:", res);
  122. },
  123. fail: function (errMsg) {
  124. console.log("方法注册失败:", errMsg);
  125. },
  126. });
  127. },
  128. onUnload() {
  129. console.log("页面卸载");
  130. clearInterval(this.timer);
  131. },
  132. onShareAppMessage(obj) {
  133. return {
  134. title: `999智控终端平台\n${this.videoInfo.title}`,
  135. path: "/pages/video/detail?id=" + this.videoInfo.id,
  136. promise: new Promise((resolve, reject) => {
  137. this.$http
  138. .request("api/share_message/get_item", {
  139. item_id: this.videoInfo.id,
  140. pages: "/pages/video/detail",
  141. })
  142. .then((callback) => {
  143. console.log(callback, "api/share_message/get_item");
  144. let obj = {
  145. title:
  146. callback.data?.title == ""
  147. ? `999智控终端平台\n${this.videoInfo.title}`
  148. : callback.data.title,
  149. path: "/pages/video/detail?id=" + this.videoInfo.id,
  150. };
  151. if (callback.data?.image_url !== "") {
  152. obj.imageUrl = callback.data.image_url;
  153. }
  154. resolve(obj);
  155. });
  156. }),
  157. };
  158. },
  159. onReady: function (res) {},
  160. onShow() {
  161. // 如果存在产品ID的话
  162. if (this.requestParam.id > 0) {
  163. // 请求详情
  164. this.$http
  165. .request("api/video_course/get_detail", this.requestParam)
  166. .then((re) => {
  167. // 成功渲染数据
  168. if (re.code == "success") {
  169. // 刷新数据
  170. this.videoInfo = re.data;
  171. if (re.data.question_list) {
  172. this.questionTime_list = re.data?.question_list.map(
  173. (item) => item.play_time
  174. );
  175. }
  176. // 获取视频容器的上下文
  177. this.videoContext = uni.createVideoContext("myVideo");
  178. // 暂停播放
  179. this.videoContext.stop();
  180. } else {
  181. if (re.code != "no_login") {
  182. uni.showModal({
  183. content: re.msg,
  184. showCancel: false,
  185. });
  186. }
  187. }
  188. });
  189. }
  190. },
  191. methods: {
  192. timeUpdate(event) {
  193. this.currentTime = event.detail.currentTime; // 更新当前播放时间
  194. // 播放到对应的
  195. if (this.currentTime) {
  196. if (!this.timer) {
  197. this.timer = setInterval(() => {
  198. this._postVideoDuration(this.currentTime, 0);
  199. }, 3000);
  200. }
  201. if (this.currentTime == event.detail.duration) {
  202. this._postVideoDuration(this.currentTime, 1);
  203. }
  204. // 判断当前时间是否接近答题时间,并且该问题尚未回答
  205. if (
  206. this.questionTime_list.includes(parseInt(this.currentTime) + 3) &&
  207. !this.answeredQuestions.has(parseInt(this.currentTime) + 3)
  208. ) {
  209. this.countdown = 3;
  210. this._startCountdown();
  211. }
  212. if (
  213. this.questionTime_list.includes(parseInt(this.currentTime)) &&
  214. !this.answeredQuestions.has(parseInt(this.currentTime))
  215. ) {
  216. this.questinInfo = this.videoInfo.question_list.find(
  217. (item) => item.play_time == parseInt(this.currentTime)
  218. );
  219. this.videoContext.pause();
  220. this.videoContext.exitFullScreen();
  221. this.$refs.questionRef.open("center");
  222. //答题时间停止上报
  223. clearInterval(this.timer);
  224. this.timer = null;
  225. }
  226. }
  227. },
  228. _videoPlay() {
  229. console.log("开始播放");
  230. this.videoContext.requestFullScreen();
  231. },
  232. //上报视频播放时间
  233. _postVideoDuration(video_playtime, status) {
  234. if (status == 1) {
  235. clearInterval(this.timer);
  236. this.timer = null;
  237. }
  238. this.$http
  239. .request("api/video_learn_record/update_playtime", {
  240. record_id: this.videoInfo.record_id,
  241. video_playtime,
  242. status,
  243. })
  244. .then((re) => {
  245. if (re.code == "success") {
  246. console.log("上报成功", video_playtime, status);
  247. }
  248. });
  249. },
  250. _selectAnswer(answer_id) {
  251. this.answer_id = answer_id;
  252. },
  253. _submitAnswer() {
  254. this.$http
  255. .request("api/video_learn_answer/play_exam", {
  256. record_id: this.videoInfo.record_id,
  257. course_id: this.questinInfo.course_id,
  258. question_id: this.questinInfo.question_id,
  259. answer_id: this.answer_id,
  260. })
  261. .then((re) => {
  262. if (re.code == "success") {
  263. this.show_answer = true;
  264. this.answerInfo = re.data;
  265. // 将当前问题标记为已回答
  266. this.answeredQuestions.add(this.questinInfo.play_time);
  267. }
  268. });
  269. },
  270. _goPlayVedio() {
  271. this.questinInfo.isAnswer = true;
  272. this.show_answer = false;
  273. this.$refs.questionRef.close();
  274. uni.showToast({
  275. title: "2秒后切换横屏",
  276. icon: "none",
  277. duration: 2000,
  278. });
  279. setTimeout(() => {
  280. this.videoContext.play();
  281. }, 2000);
  282. },
  283. _startCountdown() {
  284. const countdownInterval = setInterval(() => {
  285. if (this.countdown > 0) {
  286. uni.showToast({
  287. title: `${this.countdown}秒后进入答题`,
  288. icon: "none",
  289. duration: 1000,
  290. });
  291. this.countdown--;
  292. } else {
  293. clearInterval(countdownInterval);
  294. }
  295. }, 1000);
  296. },
  297. _videoEnd() {
  298. const _this = this;
  299. this.videoContext.pause();
  300. this.videoContext.exitFullScreen();
  301. this._postVideoDuration(this.currentTime, 1);
  302. uni.showModal({
  303. title: "学习结束",
  304. content: "恭喜您学习结束,是否查看报告?",
  305. cancelText: "重新学习",
  306. success: (res) => {
  307. if (res.confirm) {
  308. uni.redirectTo({
  309. url: `/pages/video/record?type=learn&record_id=${this.videoInfo.record_id}`,
  310. });
  311. } else {
  312. //用户重新学习,将播放进度重新设为0
  313. setTimeout(() => {
  314. // 跳转到指定的时间位置
  315. _this.videoContext.seek(0);
  316. // _this.videoContext.play();
  317. }, 200);
  318. }
  319. },
  320. });
  321. },
  322. screenChange(event) {
  323. let fullScreen = event.detail.fullScreen;
  324. console.log("fullScreen", fullScreen);
  325. this.bigsScreen = fullScreen;
  326. },
  327. },
  328. };
  329. </script>
  330. <style lang="less">
  331. .video_play {
  332. width: 750rpx;
  333. display: block;
  334. font-size: 36rpx;
  335. overflow: hidden;
  336. font-weight: bold;
  337. line-height: 60rpx;
  338. background-color: #ffffff;
  339. position: relative;
  340. .video_control {
  341. width: 750rpx;
  342. display: block;
  343. font-size: 36rpx;
  344. overflow: hidden;
  345. font-weight: bold;
  346. line-height: 60rpx;
  347. background-color: #ffffff;
  348. }
  349. .cover {
  350. position: absolute;
  351. bottom: 0;
  352. z-index: 998;
  353. height: 20%;
  354. /* background-color: red; */
  355. background-color: rgba(255, 255, 255, 0.007);
  356. margin-left: 60px;
  357. width: 70%;
  358. }
  359. .bigScreen {
  360. position: absolute;
  361. bottom: -65%;
  362. z-index: 998;
  363. height: 20%;
  364. /* background-color: red; */
  365. background-color: rgba(255, 255, 255, 0.007);
  366. margin-left: 140px;
  367. width: 65%;
  368. }
  369. }
  370. .video_title {
  371. width: 700rpx;
  372. display: block;
  373. font-size: 36rpx;
  374. overflow: hidden;
  375. font-weight: bold;
  376. line-height: 60rpx;
  377. padding: 0rpx 25rpx;
  378. padding-top: 60rpx;
  379. background-color: #ffffff;
  380. }
  381. .video_time {
  382. width: 700rpx;
  383. color: #999999;
  384. display: block;
  385. font-size: 26rpx;
  386. overflow: hidden;
  387. line-height: 40rpx;
  388. padding: 0rpx 25rpx;
  389. background-color: #ffffff;
  390. }
  391. .rich_text {
  392. width: 700rpx;
  393. display: block;
  394. overflow: hidden;
  395. font-size: 26rpx;
  396. margin: 0rpx auto;
  397. min-height: 800rpx;
  398. line-height: 50rpx;
  399. padding: 10rpx 25rpx;
  400. background-color: #ffffff;
  401. [alt] {
  402. //web_view图片
  403. max-width: 100%; // 避免图片超宽
  404. vertical-align: bottom; // 避免图片之间间隙
  405. }
  406. }
  407. .video_poster {
  408. width: 700rpx;
  409. display: block;
  410. overflow: hidden;
  411. margin: 6rpx auto;
  412. padding: 10rpx 25rpx;
  413. background-color: #ffffff;
  414. .poster_img {
  415. width: 700rpx;
  416. display: block;
  417. }
  418. }
  419. .read_total {
  420. width: 700rpx;
  421. color: #999999;
  422. display: block;
  423. font-size: 26rpx;
  424. overflow: hidden;
  425. line-height: 60rpx;
  426. padding: 0rpx 25rpx;
  427. margin-bottom: 122rpx;
  428. background-color: #ffffff;
  429. }
  430. .handle_box {
  431. left: 0rpx;
  432. width: 700rpx;
  433. height: 120rpx;
  434. display: block;
  435. position: fixed;
  436. overflow: hidden;
  437. padding: 20rpx 25rpx;
  438. background-color: #ffffff;
  439. bottom: var(--window-bottom);
  440. border-top: 2rpx solid #dddddd;
  441. .click_box {
  442. border: none;
  443. float: right;
  444. display: block;
  445. height: 120rpx;
  446. padding: 0rpx 0rpx;
  447. line-height: 120rpx;
  448. margin-right: 20rpx;
  449. font-size: 24rpx !important;
  450. background-color: transparent;
  451. .uni-icons {
  452. font-size: 36rpx !important;
  453. }
  454. }
  455. .click_box::after {
  456. border: none;
  457. background-color: transparent;
  458. }
  459. }
  460. .popup_content {
  461. width: 600rpx;
  462. height: 800rpx;
  463. background-color: #ffffff;
  464. padding: 40rpx;
  465. border-radius: 10rpx;
  466. overflow: auto;
  467. .content {
  468. display: flex;
  469. flex-direction: column;
  470. gap: 20rpx;
  471. justify-content: space-around;
  472. height: 100%;
  473. align-items: center;
  474. .question_content {
  475. overflow-y: auto;
  476. font-size: 30rpx;
  477. font-weight: bold;
  478. }
  479. .question_options {
  480. display: flex;
  481. flex-direction: column;
  482. gap: 40rpx;
  483. width: 100%;
  484. .question_answer {
  485. height: 80rpx;
  486. width: 100%;
  487. border: 4rpx solid #e3e3e3;
  488. border-radius: 20rpx;
  489. display: flex;
  490. align-items: center;
  491. justify-content: center;
  492. &.active {
  493. border-color: #3c7aff;
  494. background-color: #dbeafe;
  495. }
  496. }
  497. }
  498. .submit_btn {
  499. width: 100%;
  500. display: flex;
  501. align-items: center;
  502. justify-content: center;
  503. color: #fff;
  504. height: 100rpx;
  505. background-color: #dddddd;
  506. border-radius: 20rpx;
  507. &.active {
  508. background-color: #5045e6;
  509. }
  510. }
  511. .title {
  512. font-size: 64rpx;
  513. font-weight: bold;
  514. }
  515. .score_content {
  516. background-color: #f3f5f7;
  517. padding: 40rpx;
  518. display: flex;
  519. align-items: center;
  520. justify-content: space-between;
  521. width: 100%;
  522. border-radius: 20rpx;
  523. box-sizing: border-box;
  524. }
  525. }
  526. }
  527. </style>