VideoPostPublish.vue 18 KB

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