VideoPostPublish.vue 18 KB

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