eventLogUpload.vue 7.3 KB

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