VideoPage.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. <template>
  2. <el-row v-if="!permissionDenied">
  3. <el-row v-if="video !== null" class="movie-list">
  4. <el-col :md="15">
  5. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  6. <el-card class="box-card">
  7. <div slot="header" class="clearfix">
  8. <el-row>
  9. <h3 v-html="video.title" />
  10. </el-row>
  11. <el-row style="color: #999;font-size: 16px;padding-top: 0px;">
  12. <span><i class="el-icon-video-play">{{ video.view }}</i></span>
  13. <span v-html="'&nbsp;&nbsp;&nbsp;&nbsp;'" />
  14. <span><i class="el-icon-s-comment">{{ video.comment }}</i></span>
  15. <span v-html="'&nbsp;&nbsp;&nbsp;&nbsp;'" />
  16. <span><i class="el-icon-watch">{{ video.pubDate }}</i></span>
  17. <span v-html="'&nbsp;&nbsp;&nbsp;&nbsp;'" />
  18. <span v-if="videoId !== null && videoId.includes('BV')"><i class="el-icon-apple">
  19. <a target="_blank" :href="`https://bilibili.com/` + videoId">bili</a>
  20. </i></span>
  21. </el-row>
  22. </div>
  23. <div class="text item">
  24. <video-player :video-prop="video" />
  25. </div>
  26. </el-card>
  27. </el-row>
  28. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  29. <el-card class="box-card">
  30. <div slot="header" class="clearfix">
  31. <div class="video-data-row">
  32. <el-button
  33. type="danger"
  34. size="mini"
  35. icon="el-icon-plus"
  36. :disabled="isCollected"
  37. @click="collection(video.videoId)"
  38. >
  39. <span>收藏 {{ video.favorite }}</span>
  40. </el-button>
  41. <el-button
  42. type="danger"
  43. size="mini"
  44. icon="el-icon-thumb"
  45. :disabled="isCollected"
  46. @click="collection(video.videoId)"
  47. >
  48. <span>喜欢 {{ video.thumbUp }}</span>
  49. </el-button>
  50. <el-button
  51. type="danger"
  52. size="mini"
  53. icon="el-icon-share"
  54. :disabled="isCollected"
  55. @click="getShareUrl(video.videoId)"
  56. >
  57. <span>分享 {{ video.share }}</span>
  58. </el-button>
  59. <el-button
  60. type="danger"
  61. size="mini"
  62. icon="el-icon-download"
  63. @click="getDownloadUrl(video.videoId)"
  64. >
  65. <span>下载</span>
  66. </el-button>
  67. <el-button
  68. type="danger"
  69. size="mini"
  70. icon="el-icon-help"
  71. @click="displayErrorReportDialog"
  72. >
  73. <span>报错</span>
  74. </el-button>
  75. </div>
  76. </div>
  77. <div class="text item">
  78. <!--视频描述行-->
  79. <span class="description" v-html="video.description" />
  80. <el-divider />
  81. <!--视频标签行-->
  82. <div class="v-tag">
  83. <el-tag
  84. v-for="(tag,index) in video.tags"
  85. :key="index"
  86. class="tag"
  87. size="medium"
  88. effect="plain"
  89. >
  90. <router-link target="_blank" :to="`/video/tag/` + tag">
  91. {{ tag }}
  92. </router-link>
  93. </el-tag>
  94. </div>
  95. </div>
  96. </el-card>
  97. </el-row>
  98. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  99. <el-card class="box-card">
  100. <div slot="header" class="clearfix">
  101. <el-row>
  102. <h3>视频评论</h3>
  103. </el-row>
  104. </div>
  105. <div class="text item">
  106. <div ref="comment" :style="wrapStyle" class="comment-wrap">
  107. <comment
  108. v-model="dataList"
  109. :user="currentUser"
  110. :props="props"
  111. :before-submit="submit"
  112. :before-like="like"
  113. :before-delete="deleteComment"
  114. :upload-img="uploadImg"
  115. />
  116. <el-pagination
  117. :small="screenWidth <= 768"
  118. hide-on-single-page
  119. layout="prev, pager, next"
  120. :page-size="pageSize"
  121. :current-page="currentPage"
  122. :total="totalSize"
  123. @current-change="handleCurrentChange"
  124. />
  125. </div>
  126. </div>
  127. </el-card>
  128. </el-row>
  129. </el-col>
  130. <el-col :md="9">
  131. <el-row>
  132. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  133. <user-avatar-card v-if="user !== null" :user-avatar="user" />
  134. </el-row>
  135. </el-row>
  136. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  137. <el-card class="box-card">
  138. <div slot="header" class="clearfix">
  139. <el-row>
  140. <h3>接下来播放</h3>
  141. </el-row>
  142. <el-row>
  143. <span>自动播放 <el-switch v-model="autoPlay" /></span>
  144. </el-row>
  145. </div>
  146. </el-card>
  147. </el-row>
  148. <el-row v-for="(item,index) in similarVideos" :key="index" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  149. <side-video-card :video="item" />
  150. </el-row>
  151. </el-col>
  152. <!-- 添加到播放列表对话框 -->
  153. <el-dialog
  154. append-to-body
  155. :visible.sync="showPlaylistDialog"
  156. width="30%"
  157. center
  158. >
  159. <el-card class="box-card">
  160. <div slot="header" class="clearfix">
  161. <el-button style="float: right; padding: 3px 0" type="text" @click="submitAccessCodeWrapper">
  162. 创建播放列表
  163. </el-button>
  164. </div>
  165. <div class="text item">
  166. <el-table
  167. :data="playlist"
  168. border
  169. style="width: 100%"
  170. >
  171. <el-table-column
  172. prop="albumName"
  173. label="收藏夹"
  174. />
  175. <el-table-column
  176. prop="total"
  177. label="数量"
  178. />
  179. <el-table-column label="收藏" width="80" align="center">
  180. <template slot-scope="scope">
  181. <el-button
  182. type="danger"
  183. size="mini"
  184. icon="el-icon-plus"
  185. @click="addToPlaylist(scope.row)"
  186. />
  187. </template>
  188. </el-table-column>
  189. </el-table>
  190. </div>
  191. </el-card>
  192. </el-dialog>
  193. <!-- 视频报错对话框 -->
  194. <el-dialog
  195. append-to-body
  196. :visible.sync="showErrorReportDialog"
  197. width="30%"
  198. center
  199. >
  200. <el-card class="box-card">
  201. <div slot="header" class="clearfix">
  202. <span>视频报错</span>
  203. <el-button style="float: right; padding: 3px 0" type="text" @click="submitErrorReport">提交错误</el-button>
  204. </div>
  205. <div class="text item">
  206. <el-form ref="form" :model="errorReportForm" label-width="80px">
  207. <el-form-item label="错误类型">
  208. <el-select v-model="errorReportForm.errorCode" placeholder="选择视频错误类型">
  209. <el-option label="视频无封面" value="8" />
  210. <el-option label="视频无资源" value="9" />
  211. <el-option label="视频有广告" value="10" />
  212. </el-select>
  213. </el-form-item>
  214. </el-form>
  215. </div>
  216. </el-card>
  217. </el-dialog>
  218. </el-row>
  219. </el-row>
  220. <el-row v-else>
  221. <permission-denied-card :text-object="textObject" />
  222. </el-row>
  223. </template>
  224. <script>
  225. import PermissionDeniedCard from '@/components/card/PermissionDeniedCard'
  226. import VideoPlayer from 'components/VideoPlayer'
  227. import SideVideoCard from 'components/card/SideVideoCard'
  228. import UserAvatarCard from '@/components/card/UserAvatarCard'
  229. import comment from '@/components/comment'
  230. import { similarVideo, videoInfo, videoErrorReport, downloadVideo, getShortUrl } from '@/api/video'
  231. import { collectItem, getUserFavlist } from '@/api/collect'
  232. import { getUserInfo } from '@/api/user'
  233. import { publishComment, getComment } from '@/api/comment'
  234. import { getAuthedUser } from '@/utils/auth'
  235. export default {
  236. name: 'VideoPage',
  237. components: { SideVideoCard, VideoPlayer, UserAvatarCard, PermissionDeniedCard, comment },
  238. data() {
  239. return {
  240. // 屏幕宽度, 为了控制分页条的大小
  241. screenWidth: document.body.clientWidth,
  242. currentPage: 1,
  243. pageSize: 20,
  244. totalSize: 0,
  245. dataList: [],
  246. // ********************************************************************/
  247. wrapStyle: '',
  248. videoComments: [
  249. {
  250. commentId: 114511,
  251. content: 'this is comment content',
  252. imageUrl: '',
  253. children: [],
  254. likes: 0,
  255. liked: false,
  256. reply: null,
  257. createAt: 1700271326393,
  258. user: {
  259. userId: 1,
  260. name: '西瓜',
  261. avatar: ''
  262. }
  263. }
  264. ],
  265. currentUser: {
  266. userId: 9999,
  267. name: '芒果',
  268. avatar: '//picx.zhimg.com/v2-a2c89378a6332cbfed3e28b5ab84feb7.jpg',
  269. author: true
  270. },
  271. // 自定义组件中 comment 对象的字段名
  272. props: {
  273. id: 'commentId',
  274. content: 'content',
  275. imgSrc: 'imageUrl',
  276. children: 'children',
  277. likes: 'likes',
  278. liked: 'liked',
  279. reply: 'reply',
  280. createAt: 'createAt',
  281. total: 'total',
  282. user: 'user'
  283. },
  284. // ********************************************************************/
  285. videoId: null,
  286. video: null,
  287. user: null,
  288. similarVideos: [],
  289. isCollected: false,
  290. showPlaylistDialog: false,
  291. playlist: [],
  292. accessCodeForm: {
  293. contentId: null,
  294. contentType: 1002,
  295. accessCode: null
  296. },
  297. showErrorReportDialog: false,
  298. errorReportForm: {
  299. videoId: null,
  300. errorCode: null
  301. },
  302. permissionDenied: false,
  303. textObject: {
  304. content: '视频',
  305. route: '/video'
  306. },
  307. autoPlay: false,
  308. added: false
  309. }
  310. },
  311. watch: {
  312. // 地址栏 url 发生变化时重新加载本页面
  313. $route() {
  314. this.$router.go()
  315. }
  316. },
  317. created() {
  318. const loginUser = getAuthedUser()
  319. if (loginUser != null) {
  320. this.currentUser = {
  321. userId: loginUser.userId,
  322. name: loginUser.screenName,
  323. avatar: loginUser.avatarUrl,
  324. author: true
  325. }
  326. }
  327. this.videoId = this.$route.params.id
  328. this.accessCodeForm.contentId = this.videoId
  329. this.getVideoInfo(this.videoId)
  330. this.getSimilarVideos(this.videoId)
  331. this.getCommentWrapper(this.currentPage)
  332. },
  333. mounted() {
  334. const header = this.$refs.header
  335. if (header !== undefined && header !== null) {
  336. this.wrapStyle = `height: calc(100vh - ${header.clientHeight + 20}px)`
  337. }
  338. },
  339. methods: {
  340. // ****************************************************************************************************************
  341. handleCurrentChange(currentPage) {
  342. this.currentPage = currentPage
  343. this.getCommentWrapper(currentPage)
  344. // 回到顶部
  345. scrollTo(0, 0)
  346. },
  347. getCommentWrapper(pageNumber) {
  348. getComment(this.videoId, pageNumber).then(resp => {
  349. if (resp.code === 0) {
  350. const respData = resp.data
  351. this.dataList = respData.list
  352. this.totalSize = respData.totalSize
  353. } else {
  354. this.$notify({
  355. title: '提示',
  356. message: resp.msg,
  357. type: 'error',
  358. duration: 3000
  359. })
  360. }
  361. }).catch(error => {
  362. this.$notify({
  363. title: '提示',
  364. message: error.message,
  365. type: 'warning',
  366. duration: 3000
  367. })
  368. })
  369. },
  370. // ****************************************************************************************************************
  371. // 获取视频的详细信息
  372. getVideoInfo(videoId) {
  373. videoInfo(videoId).then(resp => {
  374. if (resp.code === 0) {
  375. this.video = resp.data
  376. document.title = resp.data.title
  377. this.userId = resp.data.userId
  378. getUserInfo(this.userId).then(resp => {
  379. if (resp.code === 0) {
  380. this.user = resp.data
  381. } else {
  382. this.$notify.error({
  383. message: '用户数据获取失败',
  384. type: 'warning',
  385. duration: 3000
  386. })
  387. }
  388. })
  389. } else if (resp.code === 2) {
  390. this.$router.push('/404')
  391. } else {
  392. this.permissionDenied = true
  393. }
  394. }).catch(error => {
  395. this.$notify.error({
  396. message: error.message,
  397. type: 'warning',
  398. duration: 3000
  399. })
  400. })
  401. },
  402. // 获取和当前视频类似的其他视频
  403. getSimilarVideos(videoId) {
  404. similarVideo(videoId).then(resp => {
  405. if (resp.code === 0) {
  406. this.similarVideos = resp.data
  407. } else {
  408. this.$notify.error({
  409. message: '推荐视频数据获取失败',
  410. type: 'warning',
  411. duration: 3000
  412. })
  413. }
  414. }).catch(error => {
  415. this.$notify.error({
  416. message: error.message,
  417. type: 'warning',
  418. duration: 3000
  419. })
  420. })
  421. },
  422. // 换一换
  423. refreshSimilar() {
  424. console.log('刷新相关推荐')
  425. },
  426. // 用户点击收藏
  427. collection(videoId) {
  428. this.showPlaylistDialog = true
  429. getUserFavlist().then(resp => {
  430. if (resp.code === 0) {
  431. this.playlist = resp.data.list
  432. }
  433. })
  434. },
  435. addToPlaylist(row) {
  436. const jsonData = {}
  437. jsonData.albumId = row.albumId
  438. jsonData.postId = this.videoId
  439. jsonData.action = 1
  440. collectItem(jsonData).then(resp => {
  441. if (resp.code === 0) {
  442. this.$notify.success({
  443. title: '视频已收藏',
  444. type: 'success',
  445. duration: 3000
  446. })
  447. } else {
  448. this.$notify.warning({
  449. title: '视频收藏失败',
  450. type: 'warning',
  451. duration: 3000
  452. })
  453. }
  454. })
  455. },
  456. getShareUrl(videoId) {
  457. getShortUrl(videoId).then(resp => {
  458. if (resp.code === 0) {
  459. console.log(resp.data)
  460. this.video.share += 1
  461. }
  462. })
  463. },
  464. getDownloadUrl(videoId) {
  465. // let filename
  466. downloadVideo(videoId).then(resp => {
  467. if (resp.code === 0) {
  468. const downloadUrl = resp.data.url
  469. window.open(downloadUrl, '_blank')
  470. /* fetch(downloadUrl.url, {
  471. headers: {
  472. Authorization: 'Bearer ' + downloadUrl.token
  473. },
  474. method: 'GET',
  475. credentials: 'include'
  476. }).then(resp => {
  477. /!*
  478. 遍历 formdata
  479. for (const key of resp.headers.keys()) {
  480. console.log(key + ' : ' + resp.headers.get(key))
  481. }*!/
  482. const header = resp.headers.get('Content-Disposition')
  483. const parts = header.split(';')
  484. const encodeFilename = parts[1].split('=')[1]
  485. filename = decodeURI(encodeFilename)
  486. return resp.blob()
  487. }).then(data => {
  488. const blobUrl = window.URL.createObjectURL(data)
  489. const a = document.createElement('a')
  490. a.download = filename
  491. a.href = blobUrl
  492. a.click()
  493. }).catch(e => {
  494. this.$notify({
  495. title: '提示',
  496. message: '视频下载失败',
  497. type: 'warning',
  498. duration: 3000
  499. })
  500. })*/
  501. } else {
  502. this.$notify({
  503. title: '提示',
  504. message: resp.msg,
  505. type: 'warning',
  506. duration: 3000
  507. })
  508. }
  509. }).catch(error => {
  510. this.$notify({
  511. title: '提示',
  512. message: error.message,
  513. type: 'error',
  514. duration: 3000
  515. })
  516. })
  517. },
  518. submitAccessCodeWrapper() {
  519. this.$message.info('创建播放列表')
  520. },
  521. displayErrorReportDialog() {
  522. this.errorReportForm.videoId = this.video.videoId
  523. this.showErrorReportDialog = true
  524. },
  525. submitErrorReport() {
  526. this.showErrorReportDialog = false
  527. videoErrorReport(this.errorReportForm).then(resp => {
  528. if (resp.code === 0) {
  529. this.errorReportForm.errorCode = null
  530. this.$notify({
  531. title: '提示',
  532. message: '视频错误已提交',
  533. type: 'warning',
  534. duration: 3000
  535. })
  536. }
  537. }).catch(error => {
  538. this.$notify({
  539. title: '提示',
  540. message: error.message,
  541. type: 'warning',
  542. duration: 3000
  543. })
  544. })
  545. },
  546. // ****************************************************************************************************************
  547. // 评论
  548. async submit(newComment, parent, add) {
  549. const res = await new Promise((resolve) => {
  550. setTimeout(() => {
  551. resolve({ newComment, parent })
  552. }, 300)
  553. })
  554. add(Object.assign(res.newComment, { postId: this.video.videoId }))
  555. if (res.parent !== null) {
  556. // console.log('parent: ', res.parent)
  557. } else {
  558. this.totalSize += 1
  559. }
  560. // console.log('addComment: ', res)
  561. publishComment(res).then(resp => {
  562. if (resp.code === 0) {
  563. this.$notify.success({
  564. message: '评论已发布',
  565. duration: 3000
  566. })
  567. } else {
  568. this.$notify.warning({
  569. message: '评论发布失败',
  570. duration: 3000
  571. })
  572. }
  573. })
  574. },
  575. async like(comment) {
  576. const res = await new Promise((resolve) => {
  577. setTimeout(() => {
  578. resolve(comment)
  579. }, 0)
  580. })
  581. console.log('likeComment: ', res)
  582. },
  583. async uploadImg({ file, callback }) {
  584. const res = await new Promise((resolve, reject) => {
  585. const reader = new FileReader()
  586. reader.readAsDataURL(file)
  587. reader.onload = () => {
  588. resolve(reader.result)
  589. }
  590. reader.onerror = () => {
  591. reject(reader.error)
  592. }
  593. })
  594. callback(res)
  595. console.log('uploadImg: ', res)
  596. },
  597. async deleteComment(comment, parent) {
  598. const res = await new Promise((resolve) => {
  599. setTimeout(() => {
  600. resolve({ comment, parent })
  601. }, 300)
  602. })
  603. console.log('deleteComment: ', res)
  604. }
  605. // ****************************************************************************************************************
  606. }
  607. }
  608. </script>
  609. <style scoped>
  610. /*处于手机屏幕时*/
  611. @media screen and (max-width: 768px) {
  612. .movie-list {
  613. padding-top: 8px;
  614. padding-left: 0.5%;
  615. padding-right: 0.5%;
  616. }
  617. }
  618. .movie-list {
  619. padding-top: 15px;
  620. padding-left: 5%;
  621. padding-right: 5%;
  622. }
  623. .clearfix:before,
  624. .clearfix:after {
  625. display: table;
  626. content: "";
  627. }
  628. .clearfix:after {
  629. clear: both;
  630. }
  631. .v-tag {
  632. padding-top: 10px;
  633. }
  634. .tag{
  635. margin-right: 3px;
  636. }
  637. </style>