PublishFile.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. 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. </div>
  27. </el-card>
  28. </el-row>
  29. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  30. <el-card class="box-card">
  31. <div slot="header" class="clearfix">
  32. <span>上传用户头像</span>
  33. </div>
  34. <div class="text item">
  35. <el-tooltip class="item" effect="dark" content="点击上传图片" placement="top-end">
  36. <el-upload
  37. class="avatar-uploader"
  38. action="//oss.reghao.cn/"
  39. :headers="imgHeaders"
  40. :data="imgData"
  41. :with-credentials="true"
  42. :show-file-list="false"
  43. :before-upload="beforeAvatarUpload"
  44. :on-success="handleAvatarSuccess"
  45. :on-change="handleOnChange"
  46. >
  47. <img v-if="coverUrl" :src="coverUrl" class="avatar">
  48. <i v-else class="el-icon-plus avatar-uploader-icon" />
  49. </el-upload>
  50. </el-tooltip>
  51. </div>
  52. </el-card>
  53. </el-row>
  54. </el-col>
  55. <el-col :md="12" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  56. <el-row style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
  57. <el-card class="box-card">
  58. <div slot="header" class="clearfix">
  59. <span>文件地址</span>
  60. </div>
  61. <div class="text item">
  62. <el-form ref="form" :model="form" label-width="80px">
  63. <el-form-item label="文件地址">
  64. <el-input v-model="form.fileUrl" style="padding-right: 1px" readonly />
  65. </el-form-item>
  66. <el-form-item label="头像地址">
  67. <el-input v-model="form.imageUrl" style="padding-right: 1px" readonly />
  68. </el-form-item>
  69. </el-form>
  70. </div>
  71. </el-card>
  72. </el-row>
  73. </el-col>
  74. </el-row>
  75. </template>
  76. <script>
  77. import { getServerInfo } from '@/api/content'
  78. export default {
  79. name: 'PublishFile',
  80. data() {
  81. return {
  82. // ****************************************************************************************************************
  83. options: {
  84. target: '//oss.reghao.cn/',
  85. chunkSize: 1024 * 1024 * 1024 * 10, // 10GiB
  86. fileParameterName: 'file',
  87. testChunks: false,
  88. query: (file, chunk) => {
  89. return {
  90. channelId: 2
  91. }
  92. },
  93. headers: {
  94. Authorization: ''
  95. },
  96. withCredentials: true
  97. },
  98. attrs: {
  99. accept: 'video/*'
  100. },
  101. // ****************************************************************************************************************
  102. imgOssUrl: null,
  103. imgHeaders: {
  104. Authorization: ''
  105. },
  106. imgData: {
  107. channelId: 4
  108. },
  109. coverUrl: null,
  110. // ****************************************************************************************************************
  111. form: {
  112. fileUrl: null,
  113. imageUrl: null
  114. }
  115. }
  116. },
  117. created() {
  118. getServerInfo(2).then(res => {
  119. if (res.code === 0) {
  120. const resData = res.data
  121. this.options.target = resData.ossUrl
  122. this.options.chunkSize = resData.maxSize
  123. this.options.headers.Authorization = 'Bearer ' + resData.token
  124. } else {
  125. this.$notify({
  126. title: '提示',
  127. message: '获取 OSS 服务器地址失败, 暂时无法上传文件',
  128. type: 'error',
  129. duration: 3000
  130. })
  131. }
  132. }).catch(error => {
  133. this.$notify({
  134. title: '提示',
  135. message: error.message,
  136. type: 'warning',
  137. duration: 3000
  138. })
  139. })
  140. getServerInfo(4).then(res => {
  141. if (res.code === 0) {
  142. const resData = res.data
  143. this.imgOssUrl = resData.ossUrl
  144. this.imgHeaders.Authorization = 'Bearer ' + resData.token
  145. } else {
  146. this.$notify({
  147. title: '提示',
  148. message: '获取 OSS 服务器地址失败, 暂时无法上传文件',
  149. type: 'error',
  150. duration: 3000
  151. })
  152. }
  153. }).catch(error => {
  154. this.$notify({
  155. title: '提示',
  156. message: error.message,
  157. type: 'warning',
  158. duration: 3000
  159. })
  160. })
  161. },
  162. mounted() {
  163. },
  164. methods: {
  165. // ****************************************************************************************************************
  166. onFileAdded(file) {
  167. if (file.file.size > 1024 * 1024 * 1024 * 5) {
  168. file.cancel()
  169. this.$notify({
  170. title: '提示',
  171. message: '视频文件应小于 5GiB',
  172. type: 'warning',
  173. duration: 3000
  174. })
  175. return
  176. }
  177. },
  178. onFileProgress(rootFile, file, chunk) {
  179. },
  180. onFileSuccess(rootFile, file, response, chunk) {
  181. const res = JSON.parse(response)
  182. if (res.code === 0) {
  183. const resData = res.data
  184. this.form.fileUrl = resData.uploadId
  185. this.$notify({
  186. title: '提示',
  187. message: '视频已上传',
  188. type: 'warning',
  189. duration: 3000
  190. })
  191. } else {
  192. this.$notify({
  193. title: '提示',
  194. message: '视频文件上传失败',
  195. type: 'warning',
  196. duration: 3000
  197. })
  198. }
  199. },
  200. onFileError(rootFile, file, response, chunk) {
  201. const res = JSON.parse(response)
  202. console.log(res.msg)
  203. this.$notify({
  204. title: '提示',
  205. message: '视频文件上传错误',
  206. type: 'warning',
  207. duration: 3000
  208. })
  209. },
  210. // ****************************************************************************************************************
  211. beforeAvatarUpload(file) {
  212. const isJPG = file.type === 'image/jpeg'
  213. const isLt2M = file.size / 1024 / 1024 < 10
  214. if (!isJPG) {
  215. this.$message.error('封面图片只能是 JPG 格式!')
  216. }
  217. if (!isLt2M) {
  218. this.$message.error('封面图片大小不能超过 10MB!')
  219. }
  220. return isJPG && isLt2M
  221. },
  222. handleAvatarSuccess(res, file) {
  223. if (res.code === 0) {
  224. const resData = res.data
  225. this.coverUrl = URL.createObjectURL(file.raw)
  226. this.form.imageUrl = resData.url
  227. } else {
  228. this.$notify({
  229. title: '提示',
  230. message: '视频封面上传失败,请重试!' + res.msg,
  231. type: 'warning',
  232. duration: 3000
  233. })
  234. }
  235. },
  236. handleOnChange(file, fileList) {
  237. }
  238. // ****************************************************************************************************************
  239. }
  240. }
  241. </script>
  242. <style>
  243. .uploader-example {
  244. width: 500px;
  245. padding: 15px;
  246. margin: 40px auto 0;
  247. font-size: 12px;
  248. box-shadow: 0 0 10px rgba(0, 0, 0, .4);
  249. }
  250. .uploader-example .uploader-btn {
  251. margin-right: 4px;
  252. }
  253. .uploader-example .uploader-list {
  254. max-height: 440px;
  255. overflow: auto;
  256. overflow-x: hidden;
  257. overflow-y: auto;
  258. }
  259. .avatar-uploader .el-upload {
  260. border: 1px dashed #d9d9d9;
  261. border-radius: 6px;
  262. cursor: pointer;
  263. position: relative;
  264. overflow: hidden;
  265. }
  266. .avatar-uploader .el-upload:hover {
  267. border-color: #409EFF;
  268. }
  269. .avatar-uploader-icon {
  270. font-size: 28px;
  271. color: #8c939d;
  272. width: 320px;
  273. height: 240px;
  274. line-height: 178px;
  275. text-align: center;
  276. }
  277. .avatar {
  278. width: 320px;
  279. height: 240px;
  280. display: block;
  281. }
  282. </style>