PublishVideo.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <template>
  2. <el-col>
  3. <el-card>
  4. <el-row>
  5. <h2>上传视频</h2>
  6. <uploader
  7. class="uploader-example"
  8. :options="options"
  9. :auto-start="true"
  10. @file-added="onFileAdded"
  11. @file-success="onFileSuccess"
  12. @file-progress="onFileProgress"
  13. @file-error="onFileError"
  14. >
  15. <uploader-unsupport />
  16. <uploader-drop>
  17. <p>拖动视频文件到此处或</p>
  18. <uploader-btn :attrs="attrs">选择视频文件</uploader-btn>
  19. </uploader-drop>
  20. <uploader-list />
  21. </uploader>
  22. </el-row>
  23. <el-divider />
  24. <el-row style="position: center">
  25. <h2>上传封面</h2>
  26. <el-upload
  27. class="avatar-uploader"
  28. action="//api.reghao.cn/api/file/upload/image"
  29. :show-file-list="false"
  30. :before-upload="beforeAvatarUpload"
  31. :on-success="handleAvatarSuccess"
  32. >
  33. <img v-if="imageUrl" :src="imageUrl" class="avatar">
  34. <i v-else class="el-icon-plus avatar-uploader-icon" />
  35. </el-upload>
  36. </el-row>
  37. <el-divider />
  38. <el-row>
  39. <h2>稿件信息</h2>
  40. </el-row>
  41. <el-form ref="form" :model="form" label-width="80px">
  42. <el-form-item label="标题">
  43. <el-input v-model="form.name" style="width: 70%; padding-right: 2px" placeholder="标题不能超过 50 个字符" />
  44. </el-form-item>
  45. <el-form-item label="描述">
  46. <el-input v-model="form.desc" type="textarea" style="width: 70%; padding-right: 2px" />
  47. </el-form-item>
  48. <el-form-item label="分区">
  49. <el-select v-model="category" placeholder="请选择分区">
  50. <el-option label="区域一" value="shanghai" />
  51. <el-option label="区域二" value="beijing" />
  52. </el-select>
  53. <el-select v-model="childCategory" placeholder="请选择子分区">
  54. <el-option label="区域一" value="shanghai" />
  55. <el-option label="区域二" value="beijing" />
  56. </el-select>
  57. </el-form-item>
  58. <el-form-item label="标签">
  59. <el-input v-model="form.tags" style="width: 70%; padding-right: 2px" placeholder="多个标签之间使用英文逗号分隔" />
  60. </el-form-item>
  61. <el-form-item label="可见范围">
  62. <el-select v-model="scope" placeholder="选择可见范围">
  63. <el-option label="区域一" value="shanghai" />
  64. <el-option label="区域二" value="beijing" />
  65. </el-select>
  66. </el-form-item>
  67. <el-form-item>
  68. <el-button type="primary" @click="onSubmit">立即投稿</el-button>
  69. <el-button>取消</el-button>
  70. </el-form-item>
  71. </el-form>
  72. </el-card>
  73. </el-col>
  74. </template>
  75. <script>
  76. import { videoCategory, submitVideoPost } from '@/api/video'
  77. import { getVideoId } from '@/api/file'
  78. export default {
  79. name: 'PublishVideo',
  80. data() {
  81. return {
  82. activeName: 'first',
  83. options: {
  84. target: '//api.reghao.cn/api/file/upload/video',
  85. chunkSize: 1024 * 1024 * 1024 * 5, // 5GiB
  86. fileParameterName: 'file',
  87. testChunks: false,
  88. query: (file, chunk) => {
  89. this.videoPost.urlObjectName = 'video/playback/' + this.videoUrlId
  90. return { key: this.videoPost.urlObjectName }
  91. },
  92. headers: {
  93. }
  94. },
  95. attrs: {
  96. accept: 'video/*'
  97. },
  98. rules: [
  99. value => !value || value.size < 2000000 || 'Avatar size should be less than 2 MB!'
  100. ],
  101. coverUrl: null,
  102. videoUrlId: null,
  103. // 提交给后端的数据
  104. videoPost: {
  105. videoFileId: null,
  106. urlObjectName: null,
  107. coverFileId: null,
  108. title: null,
  109. description: null,
  110. categoryId: null,
  111. tags: [],
  112. scope: 1,
  113. width: null,
  114. height: null,
  115. duration: null
  116. },
  117. categoryMap: {
  118. Set: function(key, value) { this[key] = value },
  119. Get: function(key) { return this[key] },
  120. Contains: function(key) { return this.Get(key) !== null },
  121. Remove: function(key) { delete this[key] }
  122. },
  123. category: [],
  124. childCategory: [],
  125. scope: [
  126. '所有人可见',
  127. '验证码可见',
  128. 'VIP 可见',
  129. '仅自己可见'
  130. ],
  131. nowCategory: {},
  132. coverFile: null,
  133. showMessage: false,
  134. message: '',
  135. dialogImageUrl: '',
  136. dialogVisible: false,
  137. disabled: false,
  138. imageList: [],
  139. imageUrl: '',
  140. form: {
  141. videoFileId: null,
  142. urlObjectName: null,
  143. coverFileId: null,
  144. title: null,
  145. desc: null,
  146. categoryId: 0,
  147. tags: null,
  148. scope: 1,
  149. width: 0,
  150. height: 0,
  151. duration: 0
  152. }
  153. }
  154. },
  155. created() {
  156. this.getVideoIdWrapper()
  157. this.getVideoCategory()
  158. },
  159. methods: {
  160. beforeAvatarUpload(file) {
  161. const isJPG = file.type === 'image/jpeg'
  162. const isLt2M = file.size / 1024 / 1024 < 2
  163. if (!isJPG) {
  164. this.$message.error('上传头像图片只能是 JPG 格式!')
  165. }
  166. if (!isLt2M) {
  167. this.$message.error('上传头像图片大小不能超过 2MB!')
  168. }
  169. return isJPG && isLt2M
  170. },
  171. handleAvatarSuccess(res, file) {
  172. this.imageList.push(file)
  173. console.log(this.imageList)
  174. this.imageUrl = URL.createObjectURL(file.raw)
  175. },
  176. // 选择视频后获取视频的分辨率和时长, 并截取第一秒的内容作为封面
  177. processVideo(file) {
  178. return new Promise((resolve, reject) => {
  179. const canvas = document.createElement('canvas')
  180. const canvasCtx = canvas.getContext('2d')
  181. const videoElem = document.createElement('video')
  182. const dataUrl = window.URL.createObjectURL(file)
  183. // 当前帧的数据是可用的
  184. videoElem.onloadeddata = function() {
  185. resolve(videoElem)
  186. }
  187. videoElem.onerror = function() {
  188. reject('video 后台加载失败')
  189. }
  190. // 设置 auto 预加载数据, 否则会出现截图为黑色图片的情况
  191. videoElem.setAttribute('preload', 'auto')
  192. videoElem.src = dataUrl
  193. // 预加载完成后才会获取到视频的宽高和时长数据
  194. videoElem.addEventListener('canplay', this.onCanPlay(videoElem, canvas, canvasCtx))
  195. })
  196. },
  197. onCanPlay(videoElem, canvas, canvasCtx) {
  198. setTimeout(() => {
  199. // 视频视频分辨率
  200. const videoWidth = videoElem.videoWidth
  201. const videoHeight = videoElem.videoHeight
  202. this.videoPost.width = videoWidth
  203. this.videoPost.height = videoHeight
  204. this.videoPost.duration = videoElem.duration
  205. videoElem.pause()
  206. /* const ratio = window.devicePixelRatio || 1
  207. canvasCtx.scale(ratio, ratio)*/
  208. // 设置画布尺寸
  209. canvas.width = videoWidth
  210. canvas.height = videoHeight
  211. canvasCtx.drawImage(videoElem, 0, 0, canvas.width, canvas.height)
  212. // 把图标base64编码后变成一段url字符串
  213. const urlData = canvas.toDataURL('image/jpeg')
  214. if (typeof urlData !== 'string') {
  215. alert('urlData不是字符串')
  216. return
  217. }
  218. var arr = urlData.split(',')
  219. var bstr = atob(arr[1])
  220. var n = bstr.length
  221. var u8arr = new Uint8Array(n)
  222. while (n--) {
  223. u8arr[n] = bstr.charCodeAt(n)
  224. }
  225. const coverFile = new File([u8arr], 'cover.jpg', { type: 'image/jpeg' })
  226. if (coverFile instanceof File) {
  227. const formData = new FormData()
  228. formData.append('file', coverFile)
  229. fetch(`//api.reghao.cn/api/file/upload/image`, {
  230. headers: {},
  231. method: 'POST',
  232. credentials: 'include',
  233. body: formData
  234. }).then(response => response.json())
  235. .then(json => {
  236. if (json.code === 0) {
  237. this.videoPost.coverFileId = json.data.imageFileId
  238. this.imageUrl = json.data.imageUrl
  239. } else {
  240. this.message = '视频封面上传失败,请重试!' + json.message
  241. this.showMessage = true
  242. }
  243. })
  244. .catch(e => {
  245. return null
  246. })
  247. }
  248. }, 1000) // 1000毫秒,就是截取第一秒,2000毫秒就是截取第2秒,视频1秒通常24帧,也可以换算成截取第几帧。
  249. // 防止拖动进度条的时候重复触发
  250. // videoElem.removeEventListener('canplay', arguments.callee)
  251. },
  252. onFileAdded(file) {
  253. this.setTitle(file.file.name)
  254. this.processVideo(file.file)
  255. /* file.pause()
  256. hashFile(file.file).then(res => {
  257. const formData = new FormData()
  258. formData.append('filename', file.file.name)
  259. formData.append('size', file.file.size)
  260. formData.append('sha256sum', res.sha256sum)
  261. fetch(`//file.reghao.cn` + `/api/file/upload/video/prepare`, {
  262. headers: {
  263. Authorization: 'Bearer ' + this.$store.getters.token
  264. },
  265. method: 'POST',
  266. credentials: 'include',
  267. body: formData
  268. }).then(response => response.json())
  269. .then(json => {
  270. const uploadId = json.data.uploadId
  271. const exist = json.data.exist
  272. if (exist) {
  273. this.message = '视频已存在'
  274. this.showMessage = true
  275. file.cancel()
  276. } else {
  277. file.uniqueIdentifier = uploadId
  278. file.resume()
  279. }
  280. })
  281. .catch(e => {
  282. return null
  283. })
  284. })*/
  285. },
  286. onFileProgress(rootFile, file, chunk) {
  287. },
  288. onFileSuccess(rootFile, file, response, chunk) {
  289. const res = JSON.parse(response)
  290. if (res.code === 0) {
  291. this.message = '视频已上传'
  292. this.showMessage = true
  293. }
  294. },
  295. onFileError(rootFile, file, response, chunk) {
  296. console.log('文件上传错误')
  297. },
  298. publish() {
  299. if (!this.videoPost.videoFileId) {
  300. this.message = '你还没有上传视频'
  301. this.showMessage = true
  302. return
  303. }
  304. if (!this.videoPost.coverFileId) {
  305. this.message = '你还没有上传视频封面'
  306. this.showMessage = true
  307. return
  308. }
  309. if (this.videoPost.title === '' || this.videoPost.categoryId === -1) {
  310. this.message = '分区和稿件标题不能为空'
  311. this.showMessage = true
  312. return
  313. }
  314. /* if (this.videoPost.scope === null) {
  315. this.message = '稿件可见范围不能为空'
  316. this.showMessage = true
  317. return
  318. }*/
  319. if (this.videoPost.tags.length === 0 || this.videoPost.tags.length > 10) {
  320. this.message = '标签最少 1 个, 最多 10 个'
  321. this.showMessage = true
  322. return
  323. }
  324. submitVideoPost(this.videoPost)
  325. .then(res => {
  326. if (res.code === 0) {
  327. this.message = '投稿成功,等待审核通过后其他人就可以看到你的视频了'
  328. this.showMessage = true
  329. this.$router.push('/studio')
  330. } else {
  331. this.message = res.msg
  332. this.showMessage = true
  333. }
  334. })
  335. .catch(error => {
  336. console.error(error.message)
  337. })
  338. },
  339. setFile(value) {
  340. this.coverFile = value
  341. },
  342. setTitle(title) {
  343. if (title.length > 50) {
  344. this.videoPost.title = title.substring(0, 50)
  345. this.videoPost.description = title
  346. } else {
  347. this.videoPost.title = title
  348. }
  349. },
  350. /* uploadVideoCover() {
  351. if (this.coverFile === null) {
  352. this.message = '请先选择视频封面,然后上传!'
  353. this.showMessage = true
  354. return
  355. }
  356. if (this.videoPost.videoFileId === null) {
  357. this.message = '等待视频上传完成后再上传封面!'
  358. this.showMessage = true
  359. return
  360. }
  361. const formData = new FormData()
  362. formData.append('videoFileId', this.videoPost.videoFileId)
  363. formData.append('file', this.coverFile)
  364. fetch(`//api.reghao.cn/api/file/upload/video/cover`, {
  365. headers: {
  366. 'Authorization': 'Bearer ' + this.$store.getters.token
  367. },
  368. method: 'POST',
  369. credentials: 'include',
  370. body: formData
  371. }).then(response => response.json())
  372. .then(json => {
  373. if (json.code === 0) {
  374. this.message = '封面已上传'
  375. this.showMessage = true
  376. this.videoPost.coverFileId = json.data.imageFileId
  377. this.videoPost.imageUrl = json.data.imageUrl
  378. } else {
  379. this.message = '上传失败,请重试!' + json.message
  380. this.showMessage = true
  381. }
  382. })
  383. .catch(e => {
  384. return null
  385. })
  386. },*/
  387. getVideoCategory() {
  388. videoCategory()
  389. .then(res => {
  390. if (res.code === 0) {
  391. for (let i = 0; i < res.data.length; i++) {
  392. const name = res.data[i].name
  393. this.category.push(name)
  394. this.categoryMap.Set(name, res.data[i])
  395. }
  396. } else {
  397. console.error(res.msg)
  398. }
  399. })
  400. .catch(error => {
  401. console.error(error.message)
  402. })
  403. },
  404. getCategory(name) {
  405. // 重置子分区,清除前一次选择分区时留下的缓存
  406. this.childCategory = []
  407. this.currentCategory = this.categoryMap.Get(name)
  408. this.videoPost.categoryId = this.currentCategory.id
  409. const c = this.currentCategory.children
  410. if (c) {
  411. for (let i = 0; i < c.length; i++) {
  412. this.childCategory.push(c[i].name)
  413. }
  414. }
  415. },
  416. getChildCategory(name) {
  417. const c = this.currentCategory.children
  418. for (let i = 0; i < c.length; i++) {
  419. if (c[i].name === name) {
  420. this.videoPost.categoryId = c[i].id
  421. }
  422. }
  423. },
  424. setVideoScope(scope) {
  425. if (scope === '所有人可见') {
  426. this.videoPost.scope = 1
  427. } else if (scope === '验证码可见') {
  428. this.videoPost.scope = 2
  429. } else if (scope === 'VIP 可见') {
  430. this.videoPost.scope = 3
  431. } else if (scope === '仅自己可见') {
  432. this.videoPost.scope = 4
  433. }
  434. },
  435. getVideoIdWrapper() {
  436. getVideoId(this.videoPost)
  437. .then(res => {
  438. if (res.code === 0) {
  439. console.log(res.data)
  440. this.videoPost.videoFileId = res.data.videoFileId
  441. this.videoUrlId = res.data.videoUrlId
  442. } else {
  443. this.message = res.msg
  444. this.showMessage = true
  445. }
  446. })
  447. .catch(error => {
  448. console.error(error.message)
  449. })
  450. },
  451. onSubmit() {
  452. console.log('submit!')
  453. }
  454. }
  455. }
  456. </script>
  457. <style>
  458. .uploader-example {
  459. width: 500px;
  460. padding: 15px;
  461. margin: 40px auto 0;
  462. font-size: 12px;
  463. box-shadow: 0 0 10px rgba(0, 0, 0, .4);
  464. }
  465. .uploader-example .uploader-btn {
  466. margin-right: 4px;
  467. }
  468. .uploader-example .uploader-list {
  469. max-height: 440px;
  470. overflow: auto;
  471. overflow-x: hidden;
  472. overflow-y: auto;
  473. }
  474. .avatar-uploader .el-upload {
  475. border: 1px dashed #d9d9d9;
  476. border-radius: 6px;
  477. cursor: pointer;
  478. position: relative;
  479. overflow: hidden;
  480. }
  481. .avatar-uploader .el-upload:hover {
  482. border-color: #409EFF;
  483. }
  484. .avatar-uploader-icon {
  485. font-size: 28px;
  486. color: #8c939d;
  487. width: 320px;
  488. height: 240px;
  489. line-height: 178px;
  490. text-align: center;
  491. }
  492. .avatar {
  493. width: 320px;
  494. height: 240px;
  495. display: block;
  496. }
  497. </style>