PublishVideo.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <template>
  2. <el-row class="movie-list">
  3. <el-col :md="12" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  4. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  5. <el-card class="box-card">
  6. <div slot="header" class="clearfix">
  7. <span>上传视频文件</span>
  8. </div>
  9. <div class="text item">
  10. <uploader
  11. v-if="options !== null"
  12. class="uploader-example"
  13. :options="options"
  14. :auto-start="true"
  15. @file-added="onFileAdded"
  16. @file-success="onFileSuccess"
  17. @file-progress="onFileProgress"
  18. @file-error="onFileError"
  19. >
  20. <uploader-unsupport />
  21. <uploader-drop>
  22. <p>拖动视频文件到此处或</p>
  23. <uploader-btn :attrs="attrs">选择视频文件</uploader-btn>
  24. </uploader-drop>
  25. <uploader-list />
  26. </uploader>
  27. </div>
  28. </el-card>
  29. </el-row>
  30. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  31. <el-card class="box-card">
  32. <div slot="header" class="clearfix">
  33. <span>上传视频封面</span>
  34. </div>
  35. <div class="text item">
  36. <el-tooltip class="item" effect="dark" content="点击上传图片" placement="top-end">
  37. <el-upload
  38. class="avatar-uploader"
  39. :action="imgOssUrl"
  40. :headers="imgHeaders"
  41. :data="imgData"
  42. :with-credentials="false"
  43. :show-file-list="false"
  44. :before-upload="beforeAvatarUpload"
  45. :on-success="handleAvatarSuccess"
  46. :on-error="handleAvatarError"
  47. :on-change="handleOnChange"
  48. >
  49. <img v-if="coverUrl" :src="coverUrl" class="avatar">
  50. <i v-else class="el-icon-plus avatar-uploader-icon" />
  51. </el-upload>
  52. </el-tooltip>
  53. </div>
  54. </el-card>
  55. </el-row>
  56. </el-col>
  57. <el-col :md="12" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  58. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  59. <el-card class="box-card">
  60. <div slot="header" class="clearfix">
  61. <span>稿件信息</span>
  62. <el-button style="float: right; padding: 3px 0" type="text" @click="onSubmit">发布</el-button>
  63. </div>
  64. <div class="text item">
  65. <el-form ref="form" :model="form" label-width="80px">
  66. <el-form-item label="标题">
  67. <el-input v-model="form.title" style="padding-right: 1px" placeholder="标题不能超过 50 个字符" />
  68. </el-form-item>
  69. <el-form-item label="描述">
  70. <el-input v-model="form.description" type="textarea" autosize style="padding-right: 1px;" />
  71. </el-form-item>
  72. <el-form-item label="分区">
  73. <el-select v-model="form.categoryPid" placeholder="请选择分区" @change="getCategory">
  74. <el-option
  75. v-for="item in pCategoryList"
  76. :key="item.value"
  77. :label="item.label"
  78. :value="item.value"
  79. />
  80. </el-select>
  81. <el-select v-model="form.categoryId" placeholder="请选择子分区">
  82. <el-option
  83. v-for="item in categoryList"
  84. :key="item.value"
  85. :label="item.label"
  86. :value="item.value"
  87. />
  88. </el-select>
  89. </el-form-item>
  90. <el-form-item label="标签">
  91. <el-select v-model="form.tags" style="padding-right: 1px" placeholder="输入标签,用回车添加" clearable multiple filterable allow-create default-first-option @change="getRecommendTags">
  92. <el-option v-for="item in rcmdTags" :key="item.value" :label="item.label" :value="item.label" />
  93. </el-select>
  94. </el-form-item>
  95. <el-form-item label="可见范围">
  96. <el-select v-model="form.scope" placeholder="选择稿件的可见范围">
  97. <el-option label="本人可见" value="1" />
  98. <el-option label="所有人可见" value="2" />
  99. <el-option label="VIP 可见" value="3" />
  100. <el-option label="验证码可见" value="4" />
  101. </el-select>
  102. </el-form-item>
  103. <el-form-item label="定时发布">
  104. <el-date-picker
  105. v-model="form.scheduledPubDate"
  106. type="datetime"
  107. placeholder="选择定时发布的时间"
  108. />
  109. </el-form-item>
  110. </el-form>
  111. </div>
  112. </el-card>
  113. </el-row>
  114. </el-col>
  115. </el-row>
  116. </template>
  117. <script>
  118. import { getServerInfo } from '@/api/content'
  119. import { videoRegion, addVideoPost } from '@/api/video'
  120. import { getVideoChannelInfo, getVideoCoverChannelInfo } from '@/api/file'
  121. export default {
  122. name: 'PublishVideo',
  123. data() {
  124. return {
  125. // ****************************************************************************************************************
  126. options: null,
  127. attrs: {
  128. accept: 'video/*'
  129. },
  130. // ****************************************************************************************************************
  131. imgOssUrl: '',
  132. imgHeaders: {
  133. Authorization: ''
  134. },
  135. imgData: {
  136. channelId: process.env.VUE_APP_UPLOAD_COVER_CHANNEL
  137. },
  138. coverUrl: null,
  139. // ****************************************************************************************************************
  140. categoryMap: {
  141. Set: function(key, value) { this[key] = value },
  142. Get: function(key) { return this[key] },
  143. Contains: function(key) { return this.Get(key) !== null },
  144. Remove: function(key) { delete this[key] }
  145. },
  146. pCategoryList: [],
  147. categoryList: [],
  148. rcmdTags: [
  149. /* { label: "知识点1" }*/
  150. ],
  151. form: {
  152. videoFileId: null,
  153. channelId: process.env.VUE_APP_UPLOAD_VIDEO_CHANNEL,
  154. coverFileId: null,
  155. coverChannelId: process.env.VUE_APP_UPLOAD_COVER_CHANNEL,
  156. title: null,
  157. description: null,
  158. categoryPid: null,
  159. categoryId: null,
  160. tags: [],
  161. scope: '1',
  162. scheduledPubDate: null
  163. }
  164. }
  165. },
  166. created() {
  167. getVideoChannelInfo().then(res => {
  168. if (res.code === 0) {
  169. const resData = res.data
  170. this.options = {
  171. target: resData.ossUrl,
  172. chunkSize: resData.maxSize,
  173. fileParameterName: 'file',
  174. testChunks: false,
  175. query: (file, chunk) => {
  176. return {
  177. channelId: this.form.channelId
  178. }
  179. },
  180. headers: {
  181. Authorization: 'Bearer ' + resData.token
  182. },
  183. withCredentials: false
  184. }
  185. } else {
  186. this.$notify({
  187. title: '提示',
  188. message: '获取 OSS 服务器地址失败, 暂时无法上传视频文件',
  189. type: 'error',
  190. duration: 3000
  191. })
  192. }
  193. }).catch(error => {
  194. this.$notify({
  195. title: '提示',
  196. message: '获取 OSS 服务器地址失败, 暂时无法上传视频文件',
  197. type: 'warning',
  198. duration: 3000
  199. })
  200. })
  201. /* getServerInfo(this.form.channelId).then(res => {
  202. if (res.code === 0) {
  203. const resData = res.data
  204. this.options = {
  205. target: resData.ossUrl,
  206. chunkSize: resData.maxSize,
  207. fileParameterName: 'file',
  208. testChunks: false,
  209. query: (file, chunk) => {
  210. return {
  211. channelId: this.form.channelId
  212. }
  213. },
  214. headers: {
  215. Authorization: 'Bearer ' + resData.token
  216. },
  217. withCredentials: false
  218. }
  219. } else {
  220. this.$notify({
  221. title: '提示',
  222. message: '获取 OSS 服务器地址失败, 暂时无法上传视频文件',
  223. type: 'error',
  224. duration: 3000
  225. })
  226. }
  227. }).catch(error => {
  228. this.$notify({
  229. title: '提示',
  230. message: '获取 OSS 服务器地址失败, 暂时无法上传视频文件',
  231. type: 'warning',
  232. duration: 3000
  233. })
  234. })*/
  235. getVideoCoverChannelInfo().then(res => {
  236. if (res.code === 0) {
  237. const resData = res.data
  238. this.imgOssUrl = resData.ossUrl
  239. this.imgHeaders.Authorization = 'Bearer ' + resData.token
  240. } else {
  241. this.$notify({
  242. title: '提示',
  243. message: '获取 OSS 服务器地址失败, 暂时无法上传视频封面',
  244. type: 'error',
  245. duration: 3000
  246. })
  247. }
  248. }).catch(error => {
  249. this.$notify({
  250. title: '提示',
  251. message: '获取 OSS 服务器地址失败, 暂时无法上传视频封面',
  252. type: 'warning',
  253. duration: 3000
  254. })
  255. })
  256. this.getVideoCategory()
  257. },
  258. mounted() {
  259. },
  260. methods: {
  261. // ****************************************************************************************************************
  262. onFileAdded(file) {
  263. if (file.file.size > 1024 * 1024 * 1024 * 10) {
  264. file.cancel()
  265. this.$notify({
  266. title: '提示',
  267. message: '视频文件应小于 10GiB',
  268. type: 'warning',
  269. duration: 3000
  270. })
  271. return
  272. }
  273. this.setTitle(file.file.name)
  274. this.processVideo(file.file)
  275. },
  276. onFileProgress(rootFile, file, chunk) {
  277. },
  278. onFileSuccess(rootFile, file, response, chunk) {
  279. const res = JSON.parse(response)
  280. if (res.code === 0) {
  281. const resData = res.data
  282. this.form.videoFileId = resData.uploadId
  283. this.$notify({
  284. title: '提示',
  285. message: '视频已上传',
  286. type: 'warning',
  287. duration: 3000
  288. })
  289. } else {
  290. this.$notify({
  291. title: '提示',
  292. message: '视频文件上传失败',
  293. type: 'warning',
  294. duration: 3000
  295. })
  296. }
  297. },
  298. onFileError(rootFile, file, response, chunk) {
  299. console.log(response)
  300. // const res = JSON.parse(response)
  301. this.$notify({
  302. title: '提示',
  303. message: '视频文件上传错误',
  304. type: 'warning',
  305. duration: 3000
  306. })
  307. },
  308. // ****************************************************************************************************************
  309. // 选择视频后获取视频的分辨率和时长, 并截取第一秒的内容作为封面
  310. processVideo(file) {
  311. return new Promise((resolve, reject) => {
  312. const canvas = document.createElement('canvas')
  313. const canvasCtx = canvas.getContext('2d')
  314. const videoElem = document.createElement('video')
  315. const dataUrl = window.URL.createObjectURL(file)
  316. // 当前帧的数据是可用的
  317. videoElem.onloadeddata = function() {
  318. resolve(videoElem)
  319. }
  320. videoElem.onerror = function() {
  321. reject('video 后台加载失败')
  322. }
  323. // 设置 auto 预加载数据, 否则会出现截图为黑色图片的情况
  324. videoElem.setAttribute('preload', 'auto')
  325. videoElem.src = dataUrl
  326. // 预加载完成后才会获取到视频的宽高和时长数据
  327. videoElem.addEventListener('canplay', this.onCanPlay(videoElem, canvas, canvasCtx))
  328. })
  329. },
  330. onCanPlay(videoElem, canvas, canvasCtx) {
  331. setTimeout(() => {
  332. // 视频视频分辨率
  333. const videoWidth = videoElem.videoWidth
  334. const videoHeight = videoElem.videoHeight
  335. this.form.width = videoWidth
  336. this.form.height = videoHeight
  337. this.form.duration = videoElem.duration
  338. videoElem.pause()
  339. // 设置画布尺寸
  340. canvas.width = videoWidth
  341. canvas.height = videoHeight
  342. canvasCtx.drawImage(videoElem, 0, 0, canvas.width, canvas.height)
  343. // 把图标base64编码后变成一段url字符串
  344. const urlData = canvas.toDataURL('image/jpeg')
  345. if (typeof urlData !== 'string') {
  346. alert('urlData不是字符串')
  347. return
  348. }
  349. var arr = urlData.split(',')
  350. var bstr = atob(arr[1])
  351. var n = bstr.length
  352. var u8arr = new Uint8Array(n)
  353. while (n--) {
  354. u8arr[n] = bstr.charCodeAt(n)
  355. }
  356. const coverFile = new File([u8arr], 'cover.jpg', { type: 'image/jpeg' })
  357. if (coverFile instanceof File) {
  358. if (coverFile.size === 0) {
  359. this.$notify({
  360. title: '提示',
  361. message: '自动获取视频封面失败,请手动选择!',
  362. type: 'warning',
  363. duration: 3000
  364. })
  365. return
  366. }
  367. const formData = new FormData()
  368. formData.append('file', coverFile)
  369. formData.append('channelId', this.imgData.channelId)
  370. fetch(this.imgOssUrl, {
  371. headers: this.imgHeaders,
  372. method: 'POST',
  373. credentials: 'include',
  374. body: formData
  375. }).then(response => response.json()).then(json => {
  376. if (json.code === 0) {
  377. this.coverUrl = URL.createObjectURL(coverFile)
  378. const resData = json.data
  379. this.form.coverFileId = resData.uploadId
  380. } else {
  381. this.$notify({
  382. title: '提示',
  383. message: '视频封面上传失败,请重试!' + json.msg,
  384. type: 'warning',
  385. duration: 3000
  386. })
  387. }
  388. }).catch(e => {
  389. return null
  390. })
  391. }
  392. }, 1000) // 1000毫秒,就是截取第一秒,2000毫秒就是截取第2秒,视频1秒通常24帧,也可以换算成截取第几帧。
  393. // 防止拖动进度条的时候重复触发
  394. // videoElem.removeEventListener('canplay', arguments.callee)
  395. },
  396. // ****************************************************************************************************************
  397. beforeAvatarUpload(file) {
  398. const isJPG = file.type === 'image/jpeg'
  399. const isLt2M = file.size / 1024 / 1024 < 10
  400. if (!isJPG) {
  401. this.$message.error('封面图片只能是 JPG 格式!')
  402. }
  403. if (!isLt2M) {
  404. this.$message.error('封面图片大小不能超过 10MB!')
  405. }
  406. return isJPG && isLt2M
  407. },
  408. handleAvatarSuccess(res, file) {
  409. if (res.code === 0) {
  410. const resData = res.data
  411. this.coverUrl = URL.createObjectURL(file.raw)
  412. this.form.coverFileId = resData.uploadId
  413. } else {
  414. this.$notify({
  415. title: '提示',
  416. message: '视频封面上传失败,请重试!' + res.msg,
  417. type: 'warning',
  418. duration: 3000
  419. })
  420. }
  421. },
  422. handleAvatarError(error, file) {
  423. this.$notify({
  424. title: '提示',
  425. message: '视频封面上传失败,请重试!' + error,
  426. type: 'warning',
  427. duration: 3000
  428. })
  429. },
  430. handleOnChange(file, fileList) {
  431. },
  432. // ****************************************************************************************************************
  433. setTitle(title) {
  434. if (title.length > 50) {
  435. this.form.title = title.substring(0, 50)
  436. this.form.description = title
  437. } else {
  438. this.form.title = title
  439. }
  440. },
  441. getVideoCategory() {
  442. videoRegion().then(res => {
  443. if (res.code === 0) {
  444. const resData = res.data
  445. for (let i = 0; i < resData.length; i++) {
  446. const name = resData[i].label
  447. const id = resData[i].value
  448. this.pCategoryList.push({ label: name, value: id })
  449. this.categoryMap.Set(id, resData[i].children)
  450. }
  451. } else {
  452. this.$notify({
  453. title: '提示',
  454. message: res.msg,
  455. type: 'warning',
  456. duration: 3000
  457. })
  458. }
  459. }).catch(error => {
  460. this.$notify({
  461. title: '提示',
  462. message: error.message,
  463. type: 'error',
  464. duration: 3000
  465. })
  466. })
  467. },
  468. getCategory(id) {
  469. // 重置子分区,清除前一次选择分区时留下的缓存
  470. this.categoryList = []
  471. for (const item of this.categoryMap.Get(id)) {
  472. this.categoryList.push({ label: item.label, value: item.value })
  473. }
  474. },
  475. // 根据输入的标签获取相似的标签
  476. getRecommendTags(tags) {
  477. for (const tag of tags) {
  478. console.log(tag)
  479. }
  480. },
  481. onSubmit() {
  482. if (!this.form.videoFileId) {
  483. this.$notify({
  484. title: '提示',
  485. message: '你还没有上传视频',
  486. type: 'warning',
  487. duration: 3000
  488. }
  489. )
  490. return
  491. }
  492. if (!this.form.coverFileId) {
  493. this.$notify({
  494. title: '提示',
  495. message: '你还没有上传视频封面',
  496. type: 'warning',
  497. duration: 3000
  498. }
  499. )
  500. return
  501. }
  502. if (this.form.title === '' || this.form.categoryId === -1) {
  503. this.$notify({
  504. title: '提示',
  505. message: '分区和稿件标题不能为空',
  506. type: 'warning',
  507. duration: 3000
  508. }
  509. )
  510. return
  511. }
  512. if (this.form.tags.length === 0 || this.form.tags.length > 10) {
  513. this.$notify({
  514. title: '提示',
  515. message: '标签最少 1 个, 最多 10 个',
  516. type: 'warning',
  517. duration: 3000
  518. }
  519. )
  520. return
  521. }
  522. if (this.form.scheduledPubDate !== null) {
  523. console.log(this.form.scheduledPubDate)
  524. if (this.form.scheduledPubDate !== null) {
  525. this.$notify({
  526. title: '提示',
  527. message: '定时发布的时间必须在当前时间之后',
  528. type: 'warning',
  529. duration: 3000
  530. })
  531. }
  532. }
  533. addVideoPost(this.form).then(res => {
  534. if (res.code === 0) {
  535. this.$notify({
  536. title: '提示',
  537. message: '投稿成功,等待审核通过后其他人就可以看到你的视频了',
  538. type: 'warning',
  539. duration: 3000
  540. })
  541. this.$router.push('/post/list/video')
  542. } else {
  543. this.$notify({
  544. title: '提示',
  545. message: res.msg,
  546. type: 'warning',
  547. duration: 3000
  548. })
  549. }
  550. }).catch(error => {
  551. this.$notify({
  552. title: '提示',
  553. message: error.message,
  554. type: 'warning',
  555. duration: 3000
  556. })
  557. })
  558. }
  559. }
  560. }
  561. </script>
  562. <style>
  563. .uploader-example {
  564. width: 500px;
  565. padding: 15px;
  566. margin: 40px auto 0;
  567. font-size: 12px;
  568. box-shadow: 0 0 10px rgba(0, 0, 0, .4);
  569. }
  570. .uploader-example .uploader-btn {
  571. margin-right: 4px;
  572. }
  573. .uploader-example .uploader-list {
  574. max-height: 440px;
  575. overflow: auto;
  576. overflow-x: hidden;
  577. overflow-y: auto;
  578. }
  579. .avatar-uploader .el-upload {
  580. border: 1px dashed #d9d9d9;
  581. border-radius: 6px;
  582. cursor: pointer;
  583. position: relative;
  584. overflow: hidden;
  585. }
  586. .avatar-uploader .el-upload:hover {
  587. border-color: #409EFF;
  588. }
  589. .avatar-uploader-icon {
  590. font-size: 28px;
  591. color: #8c939d;
  592. width: 320px;
  593. height: 240px;
  594. line-height: 178px;
  595. text-align: center;
  596. }
  597. .avatar {
  598. width: 320px;
  599. height: 240px;
  600. display: block;
  601. }
  602. </style>