index.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <div class="upload-file">
  3. <el-upload
  4. multiple
  5. :action="uploadFileUrl"
  6. :before-upload="handleBeforeUpload"
  7. :file-list="fileList"
  8. :data="data"
  9. :limit="limit"
  10. :on-error="handleUploadError"
  11. :on-exceed="handleExceed"
  12. :on-success="handleUploadSuccess"
  13. :show-file-list="false"
  14. :headers="headers"
  15. class="upload-file-uploader"
  16. ref="fileUpload"
  17. v-if="!disabled"
  18. >
  19. <!-- 上传按钮 -->
  20. <el-tooltip class="item" effect="dark" :content="'大小不超过' + fileSize +'MB,格式为' + slgegse " placement="top">
  21. <el-button size="mini" type="primary">上传</el-button>
  22. </el-tooltip>
  23. <!-- 上传提示 -->
  24. <!-- <div class="el-upload__tip" slot="tip" v-if="isShowTip">
  25. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  26. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  27. 的文件
  28. </div> -->
  29. </el-upload>
  30. <!-- 文件列表 -->
  31. <transition-group ref="uploadFileList" class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
  32. <li :key="file.fjUrl" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
  33. <el-link :href="`${baseUrl}${file.fjUrl}`" :underline="false" target="_blank">
  34. <span class="el-icon-document"> {{ getFileName(file.fjName) }} </span>
  35. </el-link>
  36. <div class="ele-upload-list__item-content-action">
  37. <el-link :underline="false" @click="handleDelete(index)" type="danger" v-if="!disabled">删除</el-link>
  38. </div>
  39. </li>
  40. </transition-group>
  41. </div>
  42. </template>
  43. <script>
  44. import { getToken } from "@/utils/auth"
  45. import Sortable from 'sortablejs'
  46. export default {
  47. name: "FileUpload",
  48. props: {
  49. // 值
  50. value: [String, Object, Array],
  51. // 上传接口地址
  52. action: {
  53. type: String,
  54. default: "/common/uploadNew"
  55. },
  56. // 上传携带的参数
  57. data: {
  58. type: Object
  59. },
  60. datam:{
  61. type: Object
  62. },
  63. // 数量限制
  64. limit: {
  65. type: Number,
  66. default: 5
  67. },
  68. // 大小限制(MB)
  69. fileSize: {
  70. type: Number,
  71. default: 5
  72. },
  73. // 文件类型, 例如['png', 'jpg', 'jpeg']
  74. fileType: {
  75. type: Array,
  76. default: () => ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "pdf","png","jpg"]
  77. },
  78. // 是否显示提示
  79. isShowTip: {
  80. type: Boolean,
  81. default: true
  82. },
  83. // 禁用组件(仅查看文件)
  84. disabled: {
  85. type: Boolean,
  86. default: false
  87. },
  88. // 拖动排序
  89. drag: {
  90. type: Boolean,
  91. default: true
  92. }
  93. },
  94. data() {
  95. return {
  96. number: 0,
  97. uploadList: [],
  98. baseUrl: process.env.VUE_APP_BASE_API,
  99. uploadFileUrl: process.env.VUE_APP_BASE_API + this.action, // 上传文件服务器地址
  100. headers: {
  101. Authorization: "Bearer " + getToken(),
  102. },
  103. fileList: [],
  104. slgegse:''
  105. }
  106. },
  107. created() {
  108. this.slgegse = this.fileType.join(',')
  109. // this.data = {"xmbh": this.datam.xmbh, "fileName": this.datam.fileName}
  110. console.log(this.datam)
  111. },
  112. mounted() {
  113. if (this.drag && !this.disabled) {
  114. this.$nextTick(() => {
  115. const element = this.$refs.uploadFileList?.$el || this.$refs.uploadFileList
  116. Sortable.create(element, {
  117. ghostClass: 'file-upload-darg',
  118. onEnd: (evt) => {
  119. const movedItem = this.fileList.splice(evt.oldIndex, 1)[0]
  120. this.fileList.splice(evt.newIndex, 0, movedItem)
  121. this.$emit("input", this.listToString(this.fileList))
  122. }
  123. })
  124. })
  125. }
  126. },
  127. watch: {
  128. value: {
  129. handler(val) {
  130. if (val) {
  131. let temp = 1
  132. // 首先将值转为数组
  133. const list = Array.isArray(val) ? val : this.value.split(',')
  134. // 然后将数组转为对象数组
  135. this.fileList = list.map(item => {
  136. if (typeof item === "string") {
  137. item = { fjName: item, fjUrl: item }
  138. }
  139. item.uid = item.uid || new Date().getTime() + temp++
  140. return item
  141. })
  142. } else {
  143. this.fileList = []
  144. return []
  145. }
  146. },
  147. deep: true,
  148. immediate: true
  149. }
  150. },
  151. computed: {
  152. // 是否显示提示
  153. showTip() {
  154. return this.isShowTip && (this.fileType || this.fileSize)
  155. },
  156. },
  157. methods: {
  158. // 上传前校检格式和大小
  159. handleBeforeUpload(file) {
  160. // 校检文件类型
  161. if (this.fileType) {
  162. const fileName = file.name.split('.')
  163. const fileExt = fileName[fileName.length - 1]
  164. const isTypeOk = this.fileType.indexOf(fileExt) >= 0
  165. if (!isTypeOk) {
  166. this.$modal.msgError(`文件格式不正确,请上传${this.fileType.join("/")}格式文件!`)
  167. return false
  168. }
  169. }
  170. // 校检文件名是否包含特殊字符
  171. if (file.name.includes(',')) {
  172. this.$modal.msgError('文件名不正确,不能包含英文逗号!')
  173. return false
  174. }
  175. // 校检文件大小
  176. if (this.fileSize) {
  177. const isLt = file.size / 1024 / 1024 < this.fileSize
  178. if (!isLt) {
  179. this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`)
  180. return false
  181. }
  182. }
  183. this.$modal.loading("正在上传文件,请稍候...")
  184. this.number++
  185. return true
  186. },
  187. // 文件个数超出
  188. handleExceed() {
  189. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`)
  190. },
  191. // 上传失败
  192. handleUploadError(err) {
  193. this.$modal.msgError("上传文件失败,请重试")
  194. this.$modal.closeLoading()
  195. },
  196. // 上传成功回调
  197. handleUploadSuccess(res, file) {
  198. if (res.code === 200) {
  199. this.uploadList.push({ fjName: res.newFileName, fjUrl: res.fileName, xmbh:this.datam.xmbh,bigType:this.datam.bigType,type:this.datam.type })
  200. console.log(this.uploadList)
  201. this.uploadedSuccessfully()
  202. } else {
  203. this.number--
  204. this.$modal.closeLoading()
  205. this.$modal.msgError(res.msg)
  206. this.$refs.fileUpload.handleRemove(file)
  207. this.uploadedSuccessfully()
  208. }
  209. },
  210. // 删除文件
  211. handleDelete(index) {
  212. this.fileList.splice(index, 1)
  213. this.$emit("input", this.listToString(this.fileList))
  214. },
  215. // 上传结束处理
  216. uploadedSuccessfully() {
  217. if (this.number > 0 && this.uploadList.length === this.number) {
  218. this.fileList = this.fileList.concat(this.uploadList)
  219. this.uploadList = []
  220. this.number = 0
  221. this.$emit("input", this.fileList)
  222. console.log(this.fileList)
  223. this.$modal.closeLoading()
  224. }
  225. },
  226. // 获取文件名称
  227. getFileName(name) {
  228. // 如果是url那么取最后的名字 如果不是直接返回
  229. if (name.lastIndexOf("/") > -1) {
  230. return name.slice(name.lastIndexOf("/") + 1)
  231. } else {
  232. return name
  233. }
  234. },
  235. // 对象转成指定字符串分隔
  236. listToString(list, separator) {
  237. let strs = ""
  238. separator = separator || ","
  239. for (let i in list) {
  240. strs += list[i].fjUrl + separator
  241. }
  242. return strs != '' ? strs.substr(0, strs.length - 1) : ''
  243. }
  244. }
  245. }
  246. </script>
  247. <style scoped lang="scss">
  248. .file-upload-darg {
  249. opacity: 0.5;
  250. background: #c8ebfb;
  251. }
  252. .upload-file-uploader {
  253. margin-bottom: 5px;
  254. }
  255. .upload-file-list .el-upload-list__item {
  256. border: 1px solid #e4e7ed;
  257. line-height: 2;
  258. margin-bottom: 10px;
  259. position: relative;
  260. }
  261. .upload-file-list .ele-upload-list__item-content {
  262. display: flex;
  263. justify-content: space-between;
  264. align-items: center;
  265. color: inherit;
  266. }
  267. .ele-upload-list__item-content-action .el-link {
  268. margin-right: 10px;
  269. }
  270. </style>