hw_obs_upload.vue 6.9 KB

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