eventLogUpload.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. for (let i = 0; i < this.fileList.length; i++) {
  125. let file={attachPath:this.fileList[i].url,fileName:this.fileList[i].webName,busIndx: "bus_indx_forest",busSource: "PC"}
  126. fileUrl.push(file)
  127. }
  128. }
  129. let param={ eventCode:this.eventCode,operation:"bus_oper_type_2",operationType:"log_oper_type_1",fileList: fileUrl }
  130. //日志文件上传
  131. eventLogUpload(param).then(res => {
  132. if(res.code==200){
  133. this.$message.success(`上传成功!`);
  134. this.showEventLogUpload = false
  135. this.cancelEventLogUploadShow();
  136. this.$parent.refreshEventDialog(this.eventCode)
  137. }
  138. })
  139. },
  140. eventLogUpload(eventCode) {
  141. this.eventCode=eventCode
  142. //上传页面弹出
  143. this.showEventLogUpload = true
  144. },
  145. cancelEventLogUploadShow() {
  146. //关闭页面
  147. this.fileList = []
  148. this.uploadList = []
  149. },
  150. // 上传前校检格式和大小
  151. handleBeforeUpload(file) {
  152. // 校检文件类型
  153. if (false) {
  154. let fileExtension = ''
  155. if (file.name.lastIndexOf('.') > -1) {
  156. fileExtension = file.name.slice(file.name.lastIndexOf('.') + 1)
  157. }
  158. const isTypeOk = this.fileType.some((type) => {
  159. if (file.type.indexOf(type) > -1) return true
  160. if (fileExtension && fileExtension.indexOf(type) > -1) return true
  161. return false
  162. })
  163. if (!isTypeOk) {
  164. this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join('/')}格式文件!`)
  165. return false
  166. }
  167. }
  168. // 校检文件大小
  169. if (this.fileSize) {
  170. const isLt = file.size / 1024 / 1024 < this.fileSize
  171. if (!isLt) {
  172. this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`)
  173. return false
  174. }
  175. }
  176. this.$modal.loading('正在上传文件,请稍候...')
  177. this.number++
  178. return true
  179. },
  180. // 文件个数超出
  181. handleExceed() {
  182. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`)
  183. },
  184. // 上传失败
  185. handleUploadError(err) {
  186. this.$modal.msgError('上传图片失败,请重试')
  187. this.$modal.closeLoading()
  188. },
  189. // 上传成功回调
  190. handleUploadSuccess(res) {
  191. this.uploadList.push({ name: res.data.url, url: res.data.url, webName: res.data.webName })
  192. if (this.uploadList.length === this.number) {
  193. this.fileList = this.fileList.concat(this.uploadList)
  194. this.uploadList = []
  195. this.number = 0
  196. this.$emit('input', this.listToString(this.fileList))
  197. this.$modal.closeLoading()
  198. }
  199. },
  200. // 删除文件
  201. handleDelete(index) {
  202. this.fileList.splice(index, 1)
  203. this.$emit('input', this.listToString(this.fileList))
  204. },
  205. // 获取文件名称
  206. getFileName(name) {
  207. if (name.lastIndexOf('/') > -1) {
  208. return name.slice(name.lastIndexOf('/') + 1)
  209. } else {
  210. return ''
  211. }
  212. },
  213. // 对象转成指定字符串分隔
  214. listToString(list, separator) {
  215. let strs = ''
  216. separator = separator || ','
  217. for (let i in list) {
  218. strs += list[i].url + separator
  219. }
  220. return strs != '' ? strs.substr(0, strs.length - 1) : ''
  221. }
  222. }
  223. }
  224. </script>
  225. <style scoped lang="scss">
  226. .upload-file-uploader {
  227. margin-bottom: 5px;
  228. }
  229. .upload-file-list .el-upload-list__item {
  230. border: 1px solid #e4e7ed;
  231. line-height: 2;
  232. margin-bottom: 10px;
  233. position: relative;
  234. }
  235. .upload-file-list .ele-upload-list__item-content {
  236. display: flex;
  237. justify-content: space-between;
  238. align-items: center;
  239. color: inherit;
  240. }
  241. .ele-upload-list__item-content-action .el-link {
  242. margin-right: 10px;
  243. }
  244. </style>