player.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <div id="dplayer" ref="dplayer" />
  3. </template>
  4. <script>
  5. import { videoUrl } from '@/api/media/video'
  6. import DPlayer from 'dplayer'
  7. const hls = require('hls.js')
  8. export default {
  9. name: 'Play',
  10. data() {
  11. return {
  12. hls,
  13. videoId: ''
  14. }
  15. },
  16. mounted() {
  17. const userInfo = this.$store.state.userInfo
  18. if (userInfo != null) {
  19. this.userId = userInfo.id.toString()
  20. } else {
  21. this.userId = 111222333
  22. }
  23. this.videoId = this.$route.params.id
  24. this.getVideoUrl(this.videoId)
  25. },
  26. methods: {
  27. // TODO 获取弹幕配置,将 videoUrl 作为本函数的回调
  28. danmakuConfig() {
  29. },
  30. /* videoUrl() {
  31. fetch(`/api/media/video/url/${this.videoId}`, {
  32. headers: {
  33. 'Content-Type': 'application/json; charset=UTF-8',
  34. 'X-XSRF-TOKEN': this.$cookies.get('XSRF-TOKEN')
  35. },
  36. method: 'GET',
  37. credentials: 'include'
  38. }).then(response => response.json())
  39. .then(json => {
  40. if (json.code === 0) {
  41. var coverUrl = json.data.coverUrl
  42. var videoUrl = json.data.videoUrl
  43. this.initDplayer(this.videoId, coverUrl, videoUrl)
  44. } else {
  45. // TODO 显示错误信息
  46. // this.$router.push('/')
  47. }
  48. })
  49. .catch(e => {
  50. return null
  51. })
  52. },*/
  53. getVideoUrl(videoId) {
  54. videoUrl(videoId)
  55. .then(res => {
  56. if (res.code === 0) {
  57. var coverUrl = res.data.coverUrl
  58. var videoUrl = res.data.videoUrl
  59. this.initDplayer(this.videoId, coverUrl, videoUrl)
  60. } else {
  61. this.$notify({
  62. title: res.code,
  63. message: res.msg,
  64. type: 'warning',
  65. duration: 500
  66. })
  67. }
  68. })
  69. .catch(error => {
  70. this.$message.error(error.message)
  71. })
  72. },
  73. initDplayer(videoId, coverUrl, videoUrl) {
  74. new DPlayer({
  75. container: document.querySelector('#dplayer'),
  76. lang: 'zh-cn',
  77. screenshot: true,
  78. video: {
  79. pic: coverUrl,
  80. url: videoUrl,
  81. type: 'auto'
  82. },
  83. logo: '/logo.png',
  84. danmaku: {
  85. id: videoId,
  86. maximum: 10000,
  87. api: '/api/media/danmaku/',
  88. token: 'hertube',
  89. user: this.userId,
  90. bottom: '15%',
  91. unlimited: true
  92. }
  93. })
  94. }
  95. }
  96. }
  97. </script>
  98. <style>
  99. #dplayer {
  100. height: 500px;
  101. }
  102. </style>