eventLogUpload.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <div class="upload-file">
  3. <el-dialog title="文件上传" :visible.sync="showEventLogUpload" v-if="showEventLogUpload" width="770px"
  4. style="height: 700px;">
  5. <el-upload
  6. multiple
  7. :action="uploadFileUrl"
  8. :before-upload="handleBeforeUpload"
  9. :file-list="fileList"
  10. :limit="limit"
  11. :on-error="handleUploadError"
  12. :on-exceed="handleExceed"
  13. :on-success="handleUploadSuccess"
  14. :show-file-list="false"
  15. :headers="headers"
  16. class="upload-file-uploader"
  17. ref="upload"
  18. >
  19. <!-- 上传按钮 -->
  20. <el-button size="mini" type="primary">选取文件</el-button>
  21. <!-- 上传提示 -->
  22. <div class="el-upload__tip" slot="tip" v-if="showTip">
  23. 请上传
  24. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b></template>
  25. <!-- <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join('/') }}</b></template>-->
  26. 的文件
  27. </div>
  28. </el-upload>
  29. <!-- 文件列表 -->
  30. <transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
  31. <li :key="file.url" class="el-upload-list__item ele-upload-list__item-content"
  32. v-for="(file, index) in fileList">
  33. <el-link :href="file.url" :underline="false" target="_blank">
  34. <span class="el-icon-document"> {{ getFileName(file.name) }} </span>
  35. </el-link>
  36. <div class="ele-upload-list__item-content-action">
  37. <el-link :underline="false" @click="handleDelete(index)" type="danger">删除</el-link>
  38. </div>
  39. </li>
  40. </transition-group>
  41. <!-- 确认上传 -->
  42. <el-button size="mini" type="primary" @click="submitUpload()">确认上传</el-button>
  43. </el-dialog>
  44. </div>
  45. </template>
  46. <script>
  47. import { getToken } from '@/utils/auth'
  48. import {
  49. eventLogUpload
  50. } from '@/api/forest'
  51. export default {
  52. name: 'FileUpload',
  53. props: {
  54. // 值
  55. value: [String, Object, Array],
  56. // 数量限制
  57. limit: {
  58. type: Number,
  59. default: 5
  60. },
  61. // 大小限制(MB)
  62. fileSize: {
  63. type: Number,
  64. default: 5
  65. },
  66. // // 文件类型, 例如['png', 'jpg', 'jpeg']
  67. // fileType: {
  68. // type: Array,
  69. // default: () => []
  70. // },
  71. // 是否显示提示
  72. isShowTip: {
  73. type: Boolean,
  74. default: true
  75. }
  76. },
  77. data() {
  78. return {
  79. eventCode:null,
  80. showEventLogUpload: false,
  81. number: 0,
  82. uploadList: [],
  83. uploadFileUrl: process.env.VUE_APP_BASE_API + '/file/upload', // 上传的图片服务器地址
  84. headers: {
  85. Authorization: 'Bearer ' + getToken()
  86. },
  87. fileList: []
  88. }
  89. },
  90. watch: {
  91. value: {
  92. handler(val) {
  93. if (val) {
  94. let temp = 1
  95. // 首先将值转为数组
  96. const list = Array.isArray(val) ? val : this.value.split(',')
  97. // 然后将数组转为对象数组
  98. this.fileList = list.map(item => {
  99. if (typeof item === 'string') {
  100. item = { name: item, url: item }
  101. }
  102. item.uid = item.uid || new Date().getTime() + temp++
  103. return item
  104. })
  105. } else {
  106. this.fileList = []
  107. return []
  108. }
  109. },
  110. deep: true,
  111. immediate: true
  112. }
  113. },
  114. computed: {
  115. // 是否显示提示
  116. showTip() {
  117. return this.isShowTip
  118. }
  119. },
  120. methods: {
  121. submitUpload() {
  122. let fileUrl=[]
  123. if (this.fileList!=null&&this.fileList.length>0)
  124. {
  125. if(this.fileList!=null&&this.fileList.length>0){
  126. for (let i = 0; i < this.fileList.length; i++) {
  127. let file={attachPath:this.fileList[i].url,fileName:this.fileList[i].webName,busIndx: "bus_indx_forest",busSource: "PC"}
  128. fileUrl.push(file)
  129. }
  130. }
  131. let param={ eventCode:this.eventCode,operation:"bus_oper_type_2",operationType:"log_oper_type_1",fileList: fileUrl }
  132. //日志文件上传
  133. eventLogUpload(param).then(res => {
  134. if(res.code==200){
  135. this.$message.success(`上传成功!`);
  136. this.showEventLogUpload = false
  137. this.cancelEventLogUploadShow();
  138. this.$parent.refreshEventDialog(this.eventCode)
  139. }
  140. })
  141. }else {
  142. this.$message.warning(`请先上传文件!`);
  143. }
  144. },
  145. eventLogUpload(eventCode) {
  146. this.eventCode=eventCode
  147. //上传页面弹出
  148. this.showEventLogUpload = true
  149. },
  150. cancelEventLogUploadShow() {
  151. //关闭页面
  152. this.fileList = []
  153. this.uploadList = []
  154. },
  155. // 上传前校检格式和大小
  156. handleBeforeUpload(file) {
  157. // 校检文件类型
  158. if (false) {
  159. let fileExtension = ''
  160. if (file.name.lastIndexOf('.') > -1) {
  161. fileExtension = file.name.slice(file.name.lastIndexOf('.') + 1)
  162. }
  163. const isTypeOk = this.fileType.some((type) => {
  164. if (file.type.indexOf(type) > -1) return true
  165. if (fileExtension && fileExtension.indexOf(type) > -1) return true
  166. return false
  167. })
  168. if (!isTypeOk) {
  169. this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join('/')}格式文件!`)
  170. return false
  171. }
  172. }
  173. // 校检文件大小
  174. if (this.fileSize) {
  175. const isLt = file.size / 1024 / 1024 < this.fileSize
  176. if (!isLt) {
  177. this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`)
  178. return false
  179. }
  180. }
  181. this.$modal.loading('正在上传文件,请稍候...')
  182. this.number++
  183. return true
  184. },
  185. // 文件个数超出
  186. handleExceed() {
  187. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`)
  188. },
  189. // 上传失败
  190. handleUploadError(err) {
  191. this.$modal.msgError('上传图片失败,请重试')
  192. this.$modal.closeLoading()
  193. },
  194. // 上传成功回调
  195. handleUploadSuccess(res) {
  196. this.uploadList.push({ name: res.data.url, url: res.data.url, webName: res.data.webName })
  197. if (this.uploadList.length === this.number) {
  198. this.fileList = this.fileList.concat(this.uploadList)
  199. this.uploadList = []
  200. this.number = 0
  201. this.$emit('input', this.listToString(this.fileList))
  202. this.$modal.closeLoading()
  203. }
  204. },
  205. // 删除文件
  206. handleDelete(index) {
  207. this.fileList.splice(index, 1)
  208. this.$emit('input', this.listToString(this.fileList))
  209. },
  210. // 获取文件名称
  211. getFileName(name) {
  212. if (name.lastIndexOf('/') > -1) {
  213. return name.slice(name.lastIndexOf('/') + 1)
  214. } else {
  215. return ''
  216. }
  217. },
  218. // 对象转成指定字符串分隔
  219. listToString(list, separator) {
  220. let strs = ''
  221. separator = separator || ','
  222. for (let i in list) {
  223. strs += list[i].url + separator
  224. }
  225. return strs != '' ? strs.substr(0, strs.length - 1) : ''
  226. }
  227. }
  228. }
  229. </script>
  230. <style scoped lang="scss">
  231. .upload-file-uploader {
  232. margin-bottom: 5px;
  233. }
  234. .upload-file-list .el-upload-list__item {
  235. border: 1px solid #e4e7ed;
  236. line-height: 2;
  237. margin-bottom: 10px;
  238. position: relative;
  239. }
  240. .upload-file-list .ele-upload-list__item-content {
  241. display: flex;
  242. justify-content: space-between;
  243. align-items: center;
  244. color: inherit;
  245. }
  246. .ele-upload-list__item-content-action .el-link {
  247. margin-right: 10px;
  248. }
  249. </style>