PublishVideo.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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: null
  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: 0,
  154. coverFileId: null,
  155. coverChannelId: 0,
  156. title: null,
  157. description: null,
  158. categoryPid: null,
  159. categoryId: null,
  160. tags: [],
  161. scope: '2',
  162. scheduledPubDate: null
  163. }
  164. }
  165. },
  166. created() {
  167. getVideoChannelInfo().then(res => {
  168. if (res.code === 0) {
  169. const resData = res.data
  170. this.form.channelId = resData.channelId
  171. this.options = {
  172. target: resData.ossUrl,
  173. chunkSize: resData.maxSize,
  174. fileParameterName: 'file',
  175. testChunks: false,
  176. query: (file, chunk) => {
  177. return {
  178. channelId: resData.channelId
  179. }
  180. },
  181. headers: {
  182. Authorization: 'Bearer ' + resData.token
  183. },
  184. withCredentials: false
  185. }
  186. } else {
  187. this.$notify({
  188. title: '提示',
  189. message: '获取 OSS 服务器地址失败, 暂时无法上传视频文件',
  190. type: 'error',
  191. duration: 3000
  192. })
  193. }
  194. }).catch(error => {
  195. this.$notify({
  196. title: '提示',
  197. message: '获取 OSS 服务器地址失败, 暂时无法上传视频文件',
  198. type: 'warning',
  199. duration: 3000
  200. })
  201. })
  202. getVideoCoverChannelInfo().then(res => {
  203. if (res.code === 0) {
  204. const resData = res.data
  205. this.form.coverChannelId = resData.channelId
  206. this.imgData.channelId = resData.channelId
  207. this.imgOssUrl = resData.ossUrl
  208. this.imgHeaders.Authorization = 'Bearer ' + resData.token
  209. } else {
  210. this.$notify({
  211. title: '提示',
  212. message: '获取 OSS 服务器地址失败, 暂时无法上传视频封面',
  213. type: 'error',
  214. duration: 3000
  215. })
  216. }
  217. }).catch(error => {
  218. this.$notify({
  219. title: '提示',
  220. message: '获取 OSS 服务器地址失败, 暂时无法上传视频封面',
  221. type: 'warning',
  222. duration: 3000
  223. })
  224. })
  225. this.getVideoCategory()
  226. },
  227. mounted() {
  228. },
  229. methods: {
  230. // ****************************************************************************************************************
  231. onFileAdded(file) {
  232. if (file.file.size > 1024 * 1024 * 1024 * 10) {
  233. file.cancel()
  234. this.$notify({
  235. title: '提示',
  236. message: '视频文件应小于 10GB',
  237. type: 'warning',
  238. duration: 3000
  239. })
  240. return
  241. }
  242. this.setTitle(file.file.name)
  243. this.processVideo(file.file)
  244. },
  245. onFileProgress(rootFile, file, chunk) {
  246. },
  247. onFileSuccess(rootFile, file, response, chunk) {
  248. const res = JSON.parse(response)
  249. if (res.code === 0) {
  250. const resData = res.data
  251. this.form.videoFileId = resData.uploadId
  252. this.$notify({
  253. title: '提示',
  254. message: '视频已上传',
  255. type: 'warning',
  256. duration: 3000
  257. })
  258. } else {
  259. this.$notify({
  260. title: '提示',
  261. message: '视频文件上传失败',
  262. type: 'warning',
  263. duration: 3000
  264. })
  265. }
  266. },
  267. onFileError(rootFile, file, response, chunk) {
  268. console.log(response)
  269. // const res = JSON.parse(response)
  270. this.$notify({
  271. title: '提示',
  272. message: '视频文件上传错误',
  273. type: 'warning',
  274. duration: 3000
  275. })
  276. },
  277. // ****************************************************************************************************************
  278. // 选择视频后获取视频的分辨率和时长, 并截取第一秒的内容作为封面
  279. processVideo(file) {
  280. return new Promise((resolve, reject) => {
  281. const canvas = document.createElement('canvas')
  282. const canvasCtx = canvas.getContext('2d')
  283. const videoElem = document.createElement('video')
  284. const dataUrl = window.URL.createObjectURL(file)
  285. // 当前帧的数据是可用的
  286. videoElem.onloadeddata = function() {
  287. resolve(videoElem)
  288. }
  289. videoElem.onerror = function() {
  290. reject('video 后台加载失败')
  291. }
  292. // 设置 auto 预加载数据, 否则会出现截图为黑色图片的情况
  293. videoElem.setAttribute('preload', 'auto')
  294. videoElem.src = dataUrl
  295. // 预加载完成后才会获取到视频的宽高和时长数据
  296. videoElem.addEventListener('canplay', this.onCanPlay(videoElem, canvas, canvasCtx))
  297. })
  298. },
  299. onCanPlay(videoElem, canvas, canvasCtx) {
  300. setTimeout(() => {
  301. // 视频视频分辨率
  302. const videoWidth = videoElem.videoWidth
  303. const videoHeight = videoElem.videoHeight
  304. this.form.width = videoWidth
  305. this.form.height = videoHeight
  306. this.form.duration = videoElem.duration
  307. videoElem.pause()
  308. // 设置画布尺寸
  309. canvas.width = videoWidth
  310. canvas.height = videoHeight
  311. canvasCtx.drawImage(videoElem, 0, 0, canvas.width, canvas.height)
  312. // 把图标base64编码后变成一段url字符串
  313. const urlData = canvas.toDataURL('image/jpeg')
  314. if (typeof urlData !== 'string') {
  315. alert('urlData不是字符串')
  316. return
  317. }
  318. var arr = urlData.split(',')
  319. var bstr = atob(arr[1])
  320. var n = bstr.length
  321. var u8arr = new Uint8Array(n)
  322. while (n--) {
  323. u8arr[n] = bstr.charCodeAt(n)
  324. }
  325. const coverFile = new File([u8arr], 'cover.jpg', { type: 'image/jpeg' })
  326. if (coverFile instanceof File) {
  327. if (coverFile.size === 0) {
  328. this.$notify({
  329. title: '提示',
  330. message: '自动获取视频封面失败,请手动选择!',
  331. type: 'warning',
  332. duration: 3000
  333. })
  334. return
  335. }
  336. const formData = new FormData()
  337. formData.append('file', coverFile)
  338. formData.append('channelId', this.imgData.channelId)
  339. fetch(this.imgOssUrl, {
  340. headers: this.imgHeaders,
  341. method: 'POST',
  342. credentials: 'include',
  343. body: formData
  344. }).then(response => response.json()).then(json => {
  345. if (json.code === 0) {
  346. this.coverUrl = URL.createObjectURL(coverFile)
  347. const resData = json.data
  348. this.form.coverFileId = resData.uploadId
  349. } else {
  350. this.$notify({
  351. title: '提示',
  352. message: '视频封面上传失败,请重试!' + json.msg,
  353. type: 'warning',
  354. duration: 3000
  355. })
  356. }
  357. }).catch(e => {
  358. return null
  359. })
  360. }
  361. }, 1000) // 1000毫秒,就是截取第一秒,2000毫秒就是截取第2秒,视频1秒通常24帧,也可以换算成截取第几帧。
  362. // 防止拖动进度条的时候重复触发
  363. // videoElem.removeEventListener('canplay', arguments.callee)
  364. },
  365. // ****************************************************************************************************************
  366. beforeAvatarUpload(file) {
  367. const isJPG = file.type === 'image/jpeg'
  368. const isLt2M = file.size / 1024 / 1024 < 10
  369. if (!isJPG) {
  370. this.$message.error('封面图片只能是 JPG 格式!')
  371. }
  372. if (!isLt2M) {
  373. this.$message.error('封面图片大小不能超过 10MB!')
  374. }
  375. return isJPG && isLt2M
  376. },
  377. handleAvatarSuccess(res, file) {
  378. if (res.code === 0) {
  379. const resData = res.data
  380. this.coverUrl = URL.createObjectURL(file.raw)
  381. this.form.coverFileId = resData.uploadId
  382. } else {
  383. this.$notify({
  384. title: '提示',
  385. message: '视频封面上传失败,请重试!' + res.msg,
  386. type: 'warning',
  387. duration: 3000
  388. })
  389. }
  390. },
  391. handleAvatarError(error, file) {
  392. this.$notify({
  393. title: '提示',
  394. message: '视频封面上传失败,请重试!' + error,
  395. type: 'warning',
  396. duration: 3000
  397. })
  398. },
  399. handleOnChange(file, fileList) {
  400. },
  401. // ****************************************************************************************************************
  402. setTitle(title) {
  403. if (title.length > 50) {
  404. this.form.title = title.substring(0, 50)
  405. this.form.description = title
  406. } else {
  407. this.form.title = title
  408. }
  409. },
  410. getVideoCategory() {
  411. videoRegion().then(res => {
  412. if (res.code === 0) {
  413. const resData = res.data
  414. for (let i = 0; i < resData.length; i++) {
  415. const name = resData[i].label
  416. const id = resData[i].value
  417. this.pCategoryList.push({ label: name, value: id })
  418. this.categoryMap.Set(id, resData[i].children)
  419. }
  420. } else {
  421. this.$notify({
  422. title: '提示',
  423. message: res.msg,
  424. type: 'warning',
  425. duration: 3000
  426. })
  427. }
  428. }).catch(error => {
  429. this.$notify({
  430. title: '提示',
  431. message: error.message,
  432. type: 'error',
  433. duration: 3000
  434. })
  435. })
  436. },
  437. getCategory(id) {
  438. // 重置子分区,清除前一次选择分区时留下的缓存
  439. this.categoryList = []
  440. for (const item of this.categoryMap.Get(id)) {
  441. this.categoryList.push({ label: item.label, value: item.value })
  442. }
  443. },
  444. // 根据输入的标签获取相似的标签
  445. getRecommendTags(tags) {
  446. for (const tag of tags) {
  447. console.log(tag)
  448. }
  449. },
  450. onSubmit() {
  451. if (!this.form.videoFileId) {
  452. this.$notify({
  453. title: '提示',
  454. message: '你还没有上传视频',
  455. type: 'warning',
  456. duration: 3000
  457. }
  458. )
  459. return
  460. }
  461. if (!this.form.coverFileId) {
  462. this.$notify({
  463. title: '提示',
  464. message: '你还没有上传视频封面',
  465. type: 'warning',
  466. duration: 3000
  467. }
  468. )
  469. return
  470. }
  471. if (this.form.title === '' || this.form.categoryId === -1) {
  472. this.$notify({
  473. title: '提示',
  474. message: '分区和稿件标题不能为空',
  475. type: 'warning',
  476. duration: 3000
  477. }
  478. )
  479. return
  480. }
  481. if (this.form.tags.length === 0 || this.form.tags.length > 10) {
  482. this.$notify({
  483. title: '提示',
  484. message: '标签最少 1 个, 最多 10 个',
  485. type: 'warning',
  486. duration: 3000
  487. }
  488. )
  489. return
  490. }
  491. if (this.form.scheduledPubDate !== null) {
  492. console.log(this.form.scheduledPubDate)
  493. if (this.form.scheduledPubDate !== null) {
  494. this.$notify({
  495. title: '提示',
  496. message: '定时发布的时间必须在当前时间之后',
  497. type: 'warning',
  498. duration: 3000
  499. })
  500. }
  501. }
  502. addVideoPost(this.form).then(res => {
  503. if (res.code === 0) {
  504. this.$notify({
  505. title: '提示',
  506. message: '投稿成功,等待审核通过后其他人就可以看到你的视频了',
  507. type: 'warning',
  508. duration: 3000
  509. })
  510. this.$router.push('/post/list/video')
  511. } else {
  512. this.$notify({
  513. title: '提示',
  514. message: res.msg,
  515. type: 'warning',
  516. duration: 3000
  517. })
  518. }
  519. }).catch(error => {
  520. this.$notify({
  521. title: '提示',
  522. message: error.message,
  523. type: 'warning',
  524. duration: 3000
  525. })
  526. })
  527. }
  528. }
  529. }
  530. </script>
  531. <style>
  532. .uploader-example {
  533. width: 500px;
  534. padding: 15px;
  535. margin: 40px auto 0;
  536. font-size: 12px;
  537. box-shadow: 0 0 10px rgba(0, 0, 0, .4);
  538. }
  539. .uploader-example .uploader-btn {
  540. margin-right: 4px;
  541. }
  542. .uploader-example .uploader-list {
  543. max-height: 440px;
  544. overflow: auto;
  545. overflow-x: hidden;
  546. overflow-y: auto;
  547. }
  548. .avatar-uploader .el-upload {
  549. border: 1px dashed #d9d9d9;
  550. border-radius: 6px;
  551. cursor: pointer;
  552. position: relative;
  553. overflow: hidden;
  554. }
  555. .avatar-uploader .el-upload:hover {
  556. border-color: #409EFF;
  557. }
  558. .avatar-uploader-icon {
  559. font-size: 28px;
  560. color: #8c939d;
  561. width: 320px;
  562. height: 240px;
  563. line-height: 178px;
  564. text-align: center;
  565. }
  566. .avatar {
  567. width: 320px;
  568. height: 240px;
  569. display: block;
  570. }
  571. </style>