hw_obs_upload.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <div>
  3. <!-- 图片类型 -->
  4. <div v-if="btntype === 'image'" @click="dialogUploadVisible = true" class="image-upload-btn">
  5. <i class="el-icon-plus"></i>
  6. </div>
  7. <!-- 宽图类型 -->
  8. <div v-else-if="btntype === 'limage'" @click="dialogUploadVisible = true" class="image-upload-btn limage-upload-btn">
  9. <i class="el-icon-plus"></i>
  10. </div>
  11. <!-- 按钮类型 -->
  12. <el-button v-else type="primary" size="small" @click="dialogUploadVisible = true" icon="el-icon-upload">上 传</el-button>
  13. <el-dialog title="文件上传" :visible.sync="dialogUploadVisible" width="500px" append-to-body :close-on-click-modal="false" :before-close="clearFiles">
  14. <selectMedia v-if = "!hidemediastock" @onShow="onShow" :accept="accept" :bindData="bindData" @onBindSuccess="onBindSuccess"></selectMedia>
  15. <el-upload
  16. style="width:100%"
  17. ref="my-upload"
  18. class="upload-demo"
  19. :accept="accept"
  20. action="#"
  21. :auto-upload="false"
  22. :show-file-list="false"
  23. :on-change="handleChange"
  24. drag
  25. multiple>
  26. <i class="el-icon-upload"></i>
  27. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  28. </el-upload>
  29. <div class="progress_panel" v-for="file in filelist" :key="file.uid">
  30. <img v-if="file.type === 'DOC' || file.type === 'DOCX'" width="30" src="../../assets/file_icons/word.png"
  31. alt="">
  32. <img v-else-if="file.type === 'PDF'" width="30" src="../../assets/file_icons/PDF.png" alt="">
  33. <img v-else-if="file.type === 'MP4' || file.type === 'AVI'" width="30" src="../../assets/file_icons/video.png"
  34. alt="">
  35. <img v-else-if="file.type === 'XLS' || file.type === 'XLSX'" width="30" src="../../assets/file_icons/excel.png"
  36. alt="">
  37. <img v-else-if="file.type === 'PNG' || file.type === 'JPG'|| file.type === 'JPEG'" width="30"
  38. src="../../assets/file_icons/image.png" alt="">
  39. <img v-else-if="file.type === 'PPT' || file.type === 'PPTX'" width="30" src="../../assets/file_icons/PPT.png"
  40. alt="">
  41. <img v-else width="30" src="../../assets/file_icons/unknow.png" alt="">
  42. <div>
  43. <p v-if="file.progress === 100" style="float:right"><span style="color:#67C23A">●</span>上传成功</p>
  44. <p>{{file.raw?file.raw.name:'暂无上传文件'}}</p>
  45. <el-progress :percentage="file.progress" :show-text="false"></el-progress>
  46. </div>
  47. </div>
  48. </el-dialog>
  49. </div>
  50. </template>
  51. <script>
  52. import selectMedia from '@/components/uploadBindMediaStock/index.vue'
  53. export default {
  54. /*
  55. folderid:文件夹id; 必填
  56. btntype:展示上传按钮的类型;
  57. accept:限制上传文件类型;
  58. bindData:附件上传成功后对应需要绑定的数据信息,
  59. hidemediastock:是否隐藏媒体库选择
  60. */
  61. props:['folderid','btntype','accept','bindData','hidemediastock'],
  62. components:{
  63. selectMedia
  64. },
  65. data () {
  66. return {
  67. dialogUploadVisible: false,
  68. params: {
  69. "classname": "system.attachment.huawei.OBS",
  70. "method": "getFileName",
  71. "content": {
  72. "filename": '',
  73. "filetype": '',
  74. "parentid": ""//归属文件夹ID
  75. }
  76. },
  77. file: {},
  78. filelist: []
  79. }
  80. },
  81. methods: {
  82. handleChange (file, filelist) {
  83. this.filelist = filelist
  84. var index = file.raw.name.lastIndexOf(".");
  85. var ext = file.name.substr(index + 1);
  86. this.params.content.filename = file.raw.name
  87. this.params.content.filetype = ext
  88. this.getUploadUrl(file, ext)
  89. },
  90. // 获取华为云上传地址
  91. async getUploadUrl (file, ext) {
  92. this.params.content.parentid = this.folderid
  93. const res = await this.$api.requested(this.params)
  94. let url = res.data.uploadurl
  95. let obsfilename = res.data.serialfilename
  96. this.upoladFileToServer(url, file, ext, obsfilename)
  97. },
  98. // 上传到华为云
  99. async upoladFileToServer (url, file, ext, obsfilename) {
  100. let THIS = this
  101. let config = {
  102. headers: ext === 'pdf' ? { 'Content-Type': 'application/pdf' } : { 'Content-Type': 'application/octet-stream' },
  103. onUploadProgress: function (progressEvent) {
  104. let percent = progressEvent.loaded / progressEvent.total * 100
  105. THIS.filelist.forEach(e => {
  106. if (e.uid === file.uid) {
  107. THIS.$set(e, 'type', ext.toUpperCase());
  108. THIS.$set(e, 'progress', percent);
  109. }
  110. })
  111. },
  112. }
  113. const res = await this.$upload.hw_upload(url, file.raw, config)
  114. this.createFileRecord(obsfilename)
  115. },
  116. // 上传成功以后生成附件记录
  117. async createFileRecord (obsfilename) {
  118. let obj = {
  119. "serialfilename": obsfilename
  120. }
  121. obj = Object.assign({},obj,this.bindData)
  122. let param = {
  123. "classname": "system.attachment.huawei.OBS",
  124. "method": "uploadSuccess",
  125. "content":obj
  126. }
  127. const res = await this.$api.requested(param)
  128. this.$emit('onSuccess',res)
  129. },
  130. clearFiles () {
  131. this.$refs['my-upload'].clearFiles()
  132. this.filelist = []
  133. this.dialogUploadVisible = false
  134. },
  135. // 监听打开媒体库的时候关闭上传弹窗
  136. onShow () {
  137. this.dialogUploadVisible = false
  138. },
  139. onBindSuccess (res) {
  140. this.$emit('onSuccess',res)
  141. }
  142. }
  143. }
  144. </script>
  145. <style>
  146. .upload-demo > div {
  147. width: 100% !important;
  148. }
  149. .upload-demo .el-upload-dragger {
  150. width: 100% !important;
  151. }
  152. </style>
  153. <style scoped>
  154. .progress_panel {
  155. display: flex;
  156. align-items: center;
  157. padding: 10px;
  158. margin: 10px 0;
  159. border-radius: 5px;
  160. transition: linear 0.2s all;
  161. }
  162. .progress_panel:hover {
  163. box-shadow: 0px 0px 5px #ccc;
  164. }
  165. .progress_panel > div {
  166. flex: 1;
  167. padding: 0 10px;
  168. }
  169. .progress_panel > div > p {
  170. line-height: 30px;
  171. }
  172. .image-upload-btn{
  173. display: flex;
  174. align-items: center;
  175. justify-content: space-around;
  176. width: 150px;
  177. height: 150px;
  178. background: #FFFFFF;
  179. border-radius: 4px 4px 4px 4px;
  180. opacity: 1;
  181. border: 1px solid #CCCCCC;
  182. color: #CCCCCC;
  183. font-size: 4rem;
  184. cursor: pointer;
  185. }
  186. .limage-upload-btn{
  187. width: 384px !important;
  188. }
  189. </style>