publish-video.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <template>
  2. <v-row justify="center" align="center">
  3. <v-col>
  4. <v-card
  5. class="mx-auto"
  6. outlined
  7. >
  8. <v-row justify="center">
  9. <v-col>
  10. <uploader
  11. class="uploader-example"
  12. :options="options"
  13. :auto-start="true"
  14. @file-added="onFileAdded"
  15. @file-success="onFileSuccess"
  16. @file-progress="onFileProgress"
  17. @file-error="onFileError"
  18. >
  19. <uploader-unsupport />
  20. <uploader-drop>
  21. <p>拖动视频文件到此处或</p>
  22. <uploader-btn :attrs="attrs">选择视频文件</uploader-btn>
  23. </uploader-drop>
  24. <uploader-list />
  25. </uploader>
  26. </v-col>
  27. </v-row>
  28. <v-row justify="center">
  29. <v-col cols="5">
  30. <v-card outlined>
  31. <v-img :src="videoPost.imageUrl" aspect-ratio="1.77" contain max-height="150" alt="封面图,推荐16:9" />
  32. </v-card>
  33. </v-col>
  34. <v-col cols="5">
  35. <v-file-input
  36. :rules="rules"
  37. accept="image/png, image/jpeg, image/bmp"
  38. prepend-icon="mdi-image"
  39. placeholder="上传视频封面"
  40. label="封面"
  41. @change="setFile"
  42. />
  43. <v-btn color="primary" @click="uploadVideoCover">
  44. 上传
  45. </v-btn>
  46. </v-col>
  47. </v-row>
  48. <v-divider />
  49. <v-row justify="center">
  50. <v-col cols="10">
  51. <h2>稿件信息</h2>
  52. </v-col>
  53. </v-row>
  54. <v-row justify="center">
  55. <v-col cols="5">
  56. <v-select
  57. :items="category"
  58. label="分区"
  59. @change="getCategory"
  60. />
  61. </v-col>
  62. <v-col cols="5">
  63. <v-select
  64. :items="childCategory"
  65. label="子分区"
  66. @change="getChildCategory"
  67. />
  68. </v-col>
  69. </v-row>
  70. <v-row justify="center">
  71. <v-col cols="10">
  72. <v-text-field
  73. v-model="videoPost.title"
  74. placeholder="标题"
  75. label="标题(50字以内)"
  76. clearable
  77. />
  78. </v-col>
  79. </v-row>
  80. <v-row justify="center">
  81. <v-col cols="10">
  82. <v-textarea
  83. v-model="videoPost.description"
  84. label="简介(200字以内)"
  85. clearable
  86. placeholder="填写更全面的视频信息,让更多的人找到你!"
  87. />
  88. </v-col>
  89. </v-row>
  90. <v-row justify="center">
  91. <v-col cols="10">
  92. <v-combobox
  93. v-model="videoPost.tags"
  94. label="添加标签让更多人找到你(最多6个)"
  95. multiple
  96. chips
  97. clearable
  98. />
  99. </v-col>
  100. </v-row>
  101. <!-- <v-row justify="center">
  102. <v-col cols="10">
  103. <v-select
  104. :items="scope"
  105. label="可见范围"
  106. @change="setVideoScope"
  107. />
  108. </v-col>
  109. </v-row>-->
  110. <v-row justify="center">
  111. <v-col cols="10">
  112. <v-btn large color="primary" @click="publish">立即投稿</v-btn>
  113. </v-col>
  114. </v-row>
  115. </v-card>
  116. </v-col>
  117. <v-snackbar
  118. v-model="showMessage"
  119. :top="true"
  120. :timeout="3000"
  121. >
  122. {{ message }}
  123. <template v-slot:action="{ attrs }">
  124. <v-btn
  125. color="pink"
  126. text
  127. v-bind="attrs"
  128. @click="showMessage = false"
  129. >
  130. 关闭
  131. </v-btn>
  132. </template>
  133. </v-snackbar>
  134. </v-row>
  135. </template>
  136. <script>
  137. import { videoCategory, submitVideoPost } from '@/api/media/video'
  138. /* import { hashFile } from '@/utils/hash' */
  139. export default {
  140. data() {
  141. return {
  142. options: {
  143. target: '//api.reghao.cn' + '/api/file/upload/video',
  144. chunkSize: 1024 * 1024 * 1024 * 5, // 分片大小 5GiB
  145. forceChunkSize: true,
  146. fileParameterName: 'file',
  147. maxChunkRetries: 3,
  148. testChunks: true,
  149. checkChunkUploadedByResponse: function(chunk, message) {
  150. const objMessage = JSON.parse(message)
  151. console.log('分片文件检验')
  152. console.log(objMessage)
  153. if (objMessage.skipUpload) {
  154. return true
  155. }
  156. return (objMessage.uploaded || []).indexOf(chunk.offset + 1) >= 0
  157. },
  158. headers: {
  159. Authorization: 'Bearer ' + this.$store.getters.token
  160. }
  161. },
  162. attrs: {
  163. accept: 'video/*'
  164. },
  165. rules: [
  166. value => !value || value.size < 2000000 || 'Avatar size should be less than 2 MB!'
  167. ],
  168. // 提交给后端的数据
  169. videoPost: {
  170. videoFileId: null,
  171. coverFileId: null,
  172. title: null,
  173. description: null,
  174. categoryId: null,
  175. tags: [],
  176. scope: 1
  177. },
  178. categoryMap: {
  179. Set: function(key, value) { this[key] = value },
  180. Get: function(key) { return this[key] },
  181. Contains: function(key) { return this.Get(key) !== null },
  182. Remove: function(key) { delete this[key] }
  183. },
  184. category: [],
  185. childCategory: [],
  186. scope: [
  187. '所有人可见',
  188. '验证码可见',
  189. 'VIP 可见',
  190. '仅自己可见'
  191. ],
  192. nowCategory: {},
  193. coverFile: null,
  194. showMessage: false,
  195. message: ''
  196. }
  197. },
  198. created() {
  199. this.getVideoCategory()
  200. },
  201. methods: {
  202. onFileAdded(file) {
  203. this.setTitle(file.file.name)
  204. /* file.pause()
  205. hashFile(file.file).then(res => {
  206. const formData = new FormData()
  207. formData.append('filename', file.file.name)
  208. formData.append('size', file.file.size)
  209. formData.append('sha256sum', res.sha256sum)
  210. fetch(`//file.reghao.cn` + `/api/file/upload/video/prepare`, {
  211. headers: {
  212. Authorization: 'Bearer ' + this.$store.getters.token
  213. },
  214. method: 'POST',
  215. credentials: 'include',
  216. body: formData
  217. }).then(response => response.json())
  218. .then(json => {
  219. const uploadId = json.data.uploadId
  220. const exist = json.data.exist
  221. if (exist) {
  222. this.message = '视频已存在'
  223. this.showMessage = true
  224. file.cancel()
  225. } else {
  226. file.uniqueIdentifier = uploadId
  227. file.resume()
  228. }
  229. })
  230. .catch(e => {
  231. return null
  232. })
  233. })*/
  234. },
  235. onFileProgress(rootFile, file, chunk) {
  236. },
  237. onFileSuccess(rootFile, file, response, chunk) {
  238. const res = JSON.parse(response)
  239. if (res.code === 0) {
  240. const resData = res.data
  241. if (resData.merged) {
  242. this.message = '视频已上传'
  243. this.showMessage = true
  244. this.videoPost.videoFileId = resData.videoFileId
  245. }
  246. }
  247. },
  248. onFileError(rootFile, file, response, chunk) {
  249. console.log('文件上传错误')
  250. },
  251. publish() {
  252. if (!this.videoPost.videoFileId) {
  253. this.message = '你还没有上传视频'
  254. this.showMessage = true
  255. return
  256. }
  257. if (!this.videoPost.coverFileId) {
  258. this.message = '你还没有上传视频封面'
  259. this.showMessage = true
  260. return
  261. }
  262. if (this.videoPost.title === '' || this.videoPost.categoryId === -1) {
  263. this.message = '分区和稿件标题不能为空'
  264. this.showMessage = true
  265. return
  266. }
  267. /* if (this.videoPost.scope === null) {
  268. this.message = '稿件可见范围不能为空'
  269. this.showMessage = true
  270. return
  271. }*/
  272. if (this.videoPost.tags.length === 0 || this.videoPost.tags.length > 10) {
  273. this.message = '标签最少 1 个, 最多 10 个'
  274. this.showMessage = true
  275. return
  276. }
  277. submitVideoPost(this.videoPost)
  278. .then(res => {
  279. if (res.code === 0) {
  280. this.message = '投稿成功,等待审核通过后其他人就可以看到你的视频了'
  281. this.showMessage = true
  282. this.$router.push('/studio')
  283. } else {
  284. this.message = res.msg
  285. this.showMessage = true
  286. }
  287. })
  288. .catch(error => {
  289. console.error(error.message)
  290. })
  291. },
  292. setFile(value) {
  293. this.coverFile = value
  294. },
  295. setTitle(title) {
  296. if (title.length > 50) {
  297. this.videoPost.title = title.substring(0, 50)
  298. } else {
  299. this.videoPost.title = title
  300. }
  301. },
  302. uploadVideoCover() {
  303. if (this.coverFile === null) {
  304. this.message = '请先选择视频封面,然后上传!'
  305. this.showMessage = true
  306. return
  307. }
  308. if (this.videoPost.videoFileId === null) {
  309. this.message = '等待视频上传完成后再上传封面!'
  310. this.showMessage = true
  311. return
  312. }
  313. const formData = new FormData()
  314. formData.append('videoFileId', this.videoPost.videoFileId)
  315. formData.append('file', this.coverFile)
  316. fetch(`//api.reghao.cn/api/file/upload/video/cover`, {
  317. headers: {
  318. 'Authorization': 'Bearer ' + this.$store.getters.token
  319. },
  320. method: 'POST',
  321. credentials: 'include',
  322. body: formData
  323. }).then(response => response.json())
  324. .then(json => {
  325. if (json.code === 0) {
  326. this.message = '封面已上传'
  327. this.showMessage = true
  328. this.videoPost.coverFileId = json.data.imageFileId
  329. this.videoPost.imageUrl = json.data.imageUrl
  330. } else {
  331. this.message = '上传失败,请重试!' + json.message
  332. this.showMessage = true
  333. }
  334. })
  335. .catch(e => {
  336. return null
  337. })
  338. },
  339. getVideoCategory() {
  340. videoCategory()
  341. .then(res => {
  342. if (res.code === 0) {
  343. for (let i = 0; i < res.data.length; i++) {
  344. const name = res.data[i].name
  345. this.category.push(name)
  346. this.categoryMap.Set(name, res.data[i])
  347. }
  348. } else {
  349. console.error(res.msg)
  350. }
  351. })
  352. .catch(error => {
  353. console.error(error.message)
  354. })
  355. },
  356. getCategory(name) {
  357. // 重置子分区,清除前一次选择分区时留下的缓存
  358. this.childCategory = []
  359. this.currentCategory = this.categoryMap.Get(name)
  360. this.videoPost.categoryId = this.currentCategory.id
  361. const c = this.currentCategory.children
  362. if (c) {
  363. for (let i = 0; i < c.length; i++) {
  364. this.childCategory.push(c[i].name)
  365. }
  366. }
  367. },
  368. getChildCategory(name) {
  369. const c = this.currentCategory.children
  370. for (let i = 0; i < c.length; i++) {
  371. if (c[i].name === name) {
  372. this.videoPost.categoryId = c[i].id
  373. }
  374. }
  375. },
  376. setVideoScope(scope) {
  377. if (scope === '所有人可见') {
  378. this.videoPost.scope = 1
  379. } else if (scope === '验证码可见') {
  380. this.videoPost.scope = 2
  381. } else if (scope === 'VIP 可见') {
  382. this.videoPost.scope = 3
  383. } else if (scope === '仅自己可见') {
  384. this.videoPost.scope = 4
  385. }
  386. }
  387. }
  388. }
  389. </script>
  390. <style>
  391. .uploader-example {
  392. width: 500px;
  393. padding: 15px;
  394. margin: 40px auto 0;
  395. font-size: 12px;
  396. box-shadow: 0 0 10px rgba(0, 0, 0, .4);
  397. }
  398. .uploader-example .uploader-btn {
  399. margin-right: 4px;
  400. }
  401. .uploader-example .uploader-list {
  402. max-height: 440px;
  403. overflow: auto;
  404. overflow-x: hidden;
  405. overflow-y: auto;
  406. }
  407. </style>