importFile.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <template>
  2. <div>
  3. <!-- 按钮类型 -->
  4. <el-button type="success" size="small" @click="dialogUploadVisible = true" icon="el-icon-upload">导 入</el-button>
  5. <el-dialog title="文件上传" class="import-panel" :visible.sync="dialogUploadVisible" width="500px" append-to-body :close-on-click-modal="false" :show-close="false" @close="clearFiles">
  6. <div slot="title"></div>
  7. <div style="background:#f1f2f3" class="my-tabs" >
  8. <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
  9. <!-- <el-tab-pane label="导入经销商线索" name="first"></el-tab-pane> -->
  10. <el-tab-pane label="导入" name="second"></el-tab-pane>
  11. </el-tabs>
  12. </div>
  13. <div style="padding:20px">
  14. <el-upload
  15. style="width:100%"
  16. ref="my-upload"
  17. class="upload-demo normal-margin"
  18. :accept="accept"
  19. action="#"
  20. :auto-upload="false"
  21. :show-file-list="false"
  22. :on-change="handleChange"
  23. drag
  24. multiple>
  25. <i class="el-icon-upload"></i>
  26. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  27. </el-upload>
  28. <div class="progress_panel" v-for="file in filelist" :key="file.uid">
  29. <img v-if="file.type === 'DOC' || file.type === 'DOCX'" width="30" src="@/assets/file_icons/word.png"
  30. alt="">
  31. <img v-else-if="file.type === 'PDF'" width="30" src="@/assets/file_icons/PDF.png" alt="">
  32. <img v-else-if="file.type === 'MP4' || file.type === 'AVI'" width="30" src="@/assets/file_icons/video.png"
  33. alt="">
  34. <img v-else-if="file.type === 'XLS' || file.type === 'XLSX'" width="30" src="@/assets/file_icons/excel.png"
  35. alt="">
  36. <img v-else-if="file.type === 'PNG' || file.type === 'JPG'|| file.type === 'JPEG'" width="30"
  37. src="@/assets/file_icons/image.png" alt="">
  38. <img v-else-if="file.type === 'PPT' || file.type === 'PPTX'" width="30" src="@/assets/file_icons/PPT.png"
  39. alt="">
  40. <img v-else width="30" src="@/assets/file_icons/unknow.png" alt="">
  41. <div>
  42. <p v-if="file.progress === 100" style="float:right"><span style="color:#67C23A">●</span>上传成功</p>
  43. <p>{{file.raw?file.raw.name:'暂无上传文件'}}</p>
  44. <el-progress :percentage="file.progress" :show-text="false"></el-progress>
  45. </div>
  46. </div>
  47. <slot name="errorFile"></slot>
  48. <p class="tips">• 为保证数据导入顺利,推荐您下载并使用<a :href="modelurl">《Excel标准模板》</a></p>
  49. <p class="tips">• 文件中数据不能超过5000行</p>
  50. <a :href="errorUrl" class="tips" style="color:red" v-if="errorUrl">•下载错误数据</a>
  51. <div class="dialog-footer">
  52. <el-button size="small" @click="cancel" class="normal-btn-width">取 消</el-button>
  53. <el-button size="small" type="warning" @click="dialogUploadVisible = false" class="normal-btn-width btn-warning">确 定</el-button>
  54. </div>
  55. </div>
  56. </el-dialog>
  57. </div>
  58. </template>
  59. <script>
  60. export default {
  61. /*
  62. folderid:文件夹id; 必填
  63. btntype:展示上传按钮的类型;
  64. accept:限制上传文件类型;
  65. bindData:附件上传成功后对应需要绑定的数据信息
  66. */
  67. props:['btntype','accept','bindData','errorUrl'],
  68. data () {
  69. return {
  70. dialogUploadVisible: false,
  71. params: {
  72. "classname": "system.attachment.huawei.OBS",
  73. "method": "getFileName",
  74. "content": {
  75. "filename": '',
  76. "filetype": '',
  77. "parentid": ""//归属文件夹ID
  78. }
  79. },
  80. file: {},
  81. filelist: [],
  82. CampaignList:[],
  83. activeName:'last',
  84. modelurl:'',
  85. campaignid:'',
  86. folderid:JSON.parse(sessionStorage.getItem('folderid')).appfolderid
  87. }
  88. },
  89. mounted () {
  90. this.getModelUrl()
  91. // this.listData()
  92. },
  93. methods: {
  94. cancel () {
  95. this.dialogUploadVisible = false
  96. this.campaignid = ''
  97. this.$refs['my-upload'].clearFiles()
  98. this.filelist = []
  99. },
  100. handleChange (file, filelist) {
  101. this.filelist = filelist
  102. var index = file.raw.name.lastIndexOf(".");
  103. var ext = file.name.substr(index + 1);
  104. this.params.content.filename = file.raw.name
  105. this.params.content.filetype = ext
  106. this.getUploadUrl(file, ext)
  107. },
  108. handleClick () {
  109. this.getModelUrl()
  110. },
  111. // 获取导入模板
  112. async getModelUrl () {
  113. const res = await this.$api.requested({
  114. "id": 20230227194703,
  115. "content": {
  116. }
  117. })
  118. this.modelurl = res.data
  119. },
  120. // 获取华为云上传地址
  121. async getUploadUrl (file, ext) {
  122. this.params.content.parentid = this.folderid
  123. const res = await this.$api.requested(this.params)
  124. let url = res.data.uploadurl
  125. let obsfilename = res.data.serialfilename
  126. this.upoladFileToServer(url, file, ext, obsfilename)
  127. },
  128. // 上传到华为云
  129. async upoladFileToServer (url, file, ext, obsfilename) {
  130. let THIS = this
  131. let config = {
  132. headers: ext === 'pdf' ? { 'Content-Type': 'application/pdf' } : { 'Content-Type': 'application/octet-stream' },
  133. onUploadProgress: function (progressEvent) {
  134. let percent = progressEvent.loaded / progressEvent.total * 100
  135. THIS.filelist.forEach(e => {
  136. if (e.uid === file.uid) {
  137. THIS.$set(e, 'type', ext.toUpperCase());
  138. THIS.$set(e, 'progress', percent);
  139. }
  140. })
  141. },
  142. }
  143. const res = await this.$upload.hw_upload(url, file.raw, config)
  144. this.createFileRecord(obsfilename)
  145. },
  146. // 上传成功以后生成附件记录
  147. async createFileRecord (obsfilename) {
  148. let obj = {
  149. "serialfilename": obsfilename
  150. }
  151. obj = Object.assign({},obj,this.bindData)
  152. let param = {
  153. "classname": "system.attachment.huawei.OBS",
  154. "method": "uploadSuccess",
  155. "content":obj
  156. }
  157. const res = await this.$api.requested(param)
  158. this.$emit('onSuccess',res.data.attachmentids[0])
  159. },
  160. clearFiles () {
  161. this.$refs['my-upload'].clearFiles()
  162. this.filelist = []
  163. this.dialogUploadVisible = false
  164. this.$emit('clearUrl')
  165. },
  166. }
  167. }
  168. </script>
  169. <style>
  170. .import-panel .el-dialog__header,.import-panel .el-dialog__body{
  171. padding: 0 !important;
  172. }
  173. .upload-demo > div {
  174. width: 100% !important;
  175. }
  176. .upload-demo .el-upload-dragger {
  177. width: 100% !important;
  178. }
  179. </style>
  180. <style scoped>
  181. .progress_panel {
  182. display: flex;
  183. align-items: center;
  184. padding: 10px;
  185. margin: 10px 0;
  186. border-radius: 5px;
  187. transition: linear 0.2s all;
  188. }
  189. .progress_panel:hover {
  190. box-shadow: 0px 0px 5px #ccc;
  191. }
  192. .progress_panel > div {
  193. flex: 1;
  194. padding: 0 10px;
  195. }
  196. .progress_panel > div > p {
  197. line-height: 30px;
  198. }
  199. .tips{
  200. line-height: 30px;
  201. }
  202. </style>